void TouchTile(object sender, View.TouchEventArgs e) { if (e.Event.Action == MotionEventActions.Up) { MyTextView tile = (MyTextView)sender; Console.WriteLine("tile is at x={0}, y={0}", tile.coordX, tile.coordY); //calculate distance between empty spot and the touched tile float xDif = (float)Math.Pow(tile.coordX - emptySpot.X, 2); float yDif = (float)Math.Pow(tile.coordY - emptySpot.Y, 2); float distance = (float)Math.Sqrt(xDif + yDif); //tile next to empty spot if (distance == 1) { Point currentPoint = new Point(tile.coordX, tile.coordY); GridLayout.Spec rowSpec = GridLayout.InvokeSpec(emptySpot.X); GridLayout.Spec colSpec = GridLayout.InvokeSpec(emptySpot.Y); GridLayout.LayoutParams newLocationParams = new GridLayout.LayoutParams(rowSpec, colSpec); tile.coordX = emptySpot.X; tile.coordY = emptySpot.Y; newLocationParams.Width = tileWidth - 10; newLocationParams.Height = tileWidth - 10; newLocationParams.SetMargins(5, 5, 5, 5); tile.LayoutParameters = newLocationParams; emptySpot = currentPoint; } } }
private void MakeTiles() { tileWidth = gameViewWidth / 4; int tileCount = 1; for (int row = 0; row < 4; row++) { for (int col = 0; col < 4; col++) { MyTextView tileText = new MyTextView(this); GridLayout.Spec rowSpec = GridLayout.InvokeSpec(row); GridLayout.Spec colSpec = GridLayout.InvokeSpec(col); GridLayout.LayoutParams tileLayoutParams = new GridLayout.LayoutParams(rowSpec, colSpec); tileText.Text = tileCount.ToString(); tileText.SetTextColor(Color.Black); tileText.TextSize = 40; tileText.Gravity = GravityFlags.Center; tileLayoutParams.Width = tileWidth - 10; tileLayoutParams.Height = tileWidth - 10; tileLayoutParams.SetMargins(5, 5, 5, 5); tileText.LayoutParameters = tileLayoutParams; tileText.SetBackgroundColor(Color.Green); //save coordinates of tile Point tileLocation = new Point(col, row); coordList.Add(tileLocation); tilesList.Add(tileText); //remember the position of the tile tileText.coordX = tileLocation.X; tileText.coordY = tileLocation.Y; tileText.Touch += TouchTile; mainLayout.AddView(tileText); tileCount = tileCount + 1; } } mainLayout.RemoveView((MyTextView)tilesList[15]); tilesList.RemoveAt(15); }