Пример #1
0
        //This event will fire when the user releases the tile.
        //It will be used when the user clicks on the block to move it automatically.
        //It will be blocked if the ManipulateMe_ManipulationCompleted happened because the user was dragging the tile.
        private new void PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            if (!DontClickSnap)
            {
                //stop any storyboard actions that are happening
                theStoryBoard.Stop();

                //set the RepositionThemeAnimation to the current
                Storyboard.SetTarget(RTA, (UIElement)sender);

                //get the tile info
                Image  tempBoarder = (Image)sender;
                myTile temp        = myTile.GetFromName(tempBoarder.Name.ToString(), theTiles);

                //save the original position of the tile
                int oldX = temp.chrdX;
                int oldY = temp.chrdY;

                bool blockMoved = false;

                //check if the tile is able to move and in what direction
                if (temp.Move == 2 && temp.Direct)
                {
                    //set the RepositionThemeAnimation movement for the tile
                    RTA.FromVerticalOffset   = Constants.BlockHeight;
                    RTA.FromHorizontalOffset = 0;

                    //begin the animation
                    theStoryBoard.Begin();
                    Canvas.SetTop(tempBoarder, myTile.LogicToBoard(temp.chrdY - 1, 'h'));

                    //set that the block moved
                    blockMoved = true;
                }
                else if (temp.Move == 2 && !temp.Direct)
                {
                    //set the RepositionThemeAnimation movement for the tile
                    RTA.FromVerticalOffset   = Constants.BlockHeight * -1;
                    RTA.FromHorizontalOffset = 0;

                    //begin the animation
                    theStoryBoard.Begin();
                    Canvas.SetTop(tempBoarder, myTile.LogicToBoard(temp.chrdY + 1, 'h'));

                    //set that the block moved
                    blockMoved = true;
                }
                else if (temp.Move == 1 && temp.Direct)
                {
                    //set the RepositionThemeAnimation movement for the tile
                    RTA.FromVerticalOffset   = 0;
                    RTA.FromHorizontalOffset = Constants.BlockWidth * -1;

                    //begin the animation
                    theStoryBoard.Begin();
                    Canvas.SetLeft(tempBoarder, myTile.LogicToBoard(temp.chrdX + 1, 'w'));

                    //set that the block moved
                    blockMoved = true;
                }
                else if (temp.Move == 1 && !temp.Direct)
                {
                    //set the RepositionThemeAnimation movement for the tile
                    RTA.FromVerticalOffset   = 0;
                    RTA.FromHorizontalOffset = Constants.BlockWidth;

                    //begin the animation
                    theStoryBoard.Begin();
                    Canvas.SetLeft(tempBoarder, myTile.LogicToBoard(temp.chrdX - 1, 'w'));

                    //set that the block moved
                    blockMoved = true;
                }

                //if the block was actually moved
                if (blockMoved)
                {
                    //update the logic of the tile
                    theTiles = TileHandler.UpdateTile(theTiles, oldX, oldY);

                    //check how many tiles are in the correct position
                    int correctTiles = TileHandler.CheckWin(theTiles);

                    //update the status bar
                    if (correctTiles == 15)
                    {
                        StatusBar.Text = "You Win";
                    }
                    else
                    {
                        StatusBar.Text = "You have " + correctTiles + " in the correct place";
                    }
                }
            }

            DontClickSnap = false;
        }
Пример #2
0
        //this event fires when a drag action is complete
        //it will be used to snap the tile into place, then call the methods used to update the game logic
        void ManipulateMe_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            //set this true so that the PointerReleased will not activate
            DontClickSnap = true;

            Image tempImage = (Image)sender;

            //get the mytile object tied to this image
            myTile temp = myTile.GetFromName(tempImage.Name.ToString(), theTiles);

            //save its original position
            int oldX = temp.chrdX;
            int oldY = temp.chrdY;

            //get the actual position of the tile on the canvas
            double top  = Canvas.GetTop(tempImage);
            double left = Canvas.GetLeft(tempImage);

            double DistanceOriginal = 0;
            double DistanceNew      = 0;
            bool   blockMoved       = false;

            //get the direction that the tile is supposed to move in
            if (temp.Move == 2 && temp.Direct)
            {
                //get the distances of between where the tile was coming from, and where it was going
                DistanceOriginal = myTile.LogicToBoard(temp.chrdY, 'h') - top;
                DistanceNew      = top - myTile.LogicToBoard(temp.chrdY - 1, 'h');

                //determine if it is closer the new spot or the original one.
                if (DistanceOriginal <= DistanceNew)
                {
                    //put it back to were it came from
                    Canvas.SetTop(tempImage, myTile.LogicToBoard(temp.chrdY, 'h'));
                }
                else
                {
                    //snap it to the new position and set that it a block was moved
                    Canvas.SetTop(tempImage, myTile.LogicToBoard(temp.chrdY - 1, 'h'));
                    blockMoved = true;
                }
            }
            else if (temp.Move == 2 && !temp.Direct)
            {
                //get the distances of between where the tile was coming from, and where it was going
                DistanceNew      = myTile.LogicToBoard(temp.chrdY + 1, 'h') - top;
                DistanceOriginal = top - myTile.LogicToBoard(temp.chrdY, 'h');

                if (DistanceOriginal <= DistanceNew)
                {
                    //put it back to were it came from
                    Canvas.SetTop(tempImage, myTile.LogicToBoard(temp.chrdY, 'h'));
                }
                else
                {
                    //snap it to the new position and set that it a block was moved
                    Canvas.SetTop(tempImage, myTile.LogicToBoard(temp.chrdY + 1, 'h'));
                    blockMoved = true;
                }
            }
            else if (temp.Move == 1 && temp.Direct)
            {
                //get the distances of between where the tile was coming from, and where it was going
                DistanceNew      = myTile.LogicToBoard(temp.chrdX + 1, 'w') - left;
                DistanceOriginal = left - myTile.LogicToBoard(temp.chrdX, 'w');

                if (DistanceOriginal <= DistanceNew)
                {
                    //put it back to were it came from
                    Canvas.SetLeft(tempImage, myTile.LogicToBoard(temp.chrdX, 'w'));
                }
                else
                {
                    //snap it to the new position and set that it a block was moved
                    Canvas.SetLeft(tempImage, myTile.LogicToBoard(temp.chrdX + 1, 'w'));
                    blockMoved = true;
                }
            }
            else if (temp.Move == 1 && !temp.Direct)
            {
                //get the distances of between where the tile was coming from, and where it was going
                DistanceNew      = left - myTile.LogicToBoard(temp.chrdX - 1, 'w');
                DistanceOriginal = myTile.LogicToBoard(temp.chrdX, 'w') - left;

                if (DistanceOriginal <= DistanceNew)
                {
                    //put it back to were it came from
                    Canvas.SetLeft(tempImage, myTile.LogicToBoard(temp.chrdX, 'w'));
                }
                else
                {
                    //snap it to the new position and set that it a block was moved
                    Canvas.SetLeft(tempImage, myTile.LogicToBoard(temp.chrdX - 1, 'w'));
                    blockMoved = true;
                }
            }

            //if a block was moved to a new position
            if (blockMoved)
            {
                //update the list of tile
                theTiles = TileHandler.UpdateTile(theTiles, oldX, oldY);

                //update the win sate
                int correctTiles = TileHandler.CheckWin(theTiles);
                if (correctTiles == 15)
                {
                    StatusBar.Text = "You Win!";
                }
                else
                {
                    StatusBar.Text = "You have " + correctTiles + " in the correct place.";
                }
            }
        }