0
votes

I'm new to android programming and have some questions. I'm trying to create a tile puzzle game where I have to move squared tiles into a grid and the tiles would automatically clip onto place. For example, I have a 3x3 squared grid with 9 position. Each position measures 100x100 pixel. If I drag a tile(also 100x100 pixel) to any of the 9 positions inside the grid, the tile would automatically clip in place inside the 100x100 pixel area even if it's a bit off. How do I do this?

1
One of my ideas is for each of the 9 area in the 3x3 grid(300x300pixel), I would have 9 points in the middle of each area((50,50) for 1st area, (150,50) for 2nd, (250,50)for 3rd, (50,150) for 4th, .... , (250,250) for 9th). For my tile, I have a position of it in the center of my tile. When I drag my tile inside the grid and let go, it would calculate the distance between my tile and the 9 points inside the tile. For the closest position, set the position my tile equal to that position(which clips my tile into the 100x100 area). I think this is a bad way to do it cause it's not efficient.iamarnold

1 Answers

1
votes
x=((int)(x/100))*100
y=((int)(y/100))*100

for example if you have x=140 y=260 then

((int)(140/100))*100=100 
((int)(260/100))*100=200

Because of the (int) the ((int)(140/100)) is 1 not 1.4 so the point of this is to round the number