I have a set of objects i'm moving across the screen. The are going to be either 200 x 200 or 120 x 120 and will only move right or left in the x direction. I need to calculate the starting positions of the objects
This should be simple math I think, where for objects of size 120x120: (zero is the leftmost edge of the screen)
space=10 (pixels)
object0 starts at -120 )
object1 starts at -240 + space
object2 starts at -360 + space
object3 starts at -480 + space
... object9 starts at -1080 + space
but this isn't working, they get more and more out of alignment the further the calculation goes. When I move them they stay in their out-of-aligned positions, as I'm adding the same amount of开发者_运维问答 motion to each object.
It would also be nice also to compute the starting x positions from any given point instead of manually entering the x positions, accounting for x can be positive or negative.
getPositionOfNth(int objectWidth, int startPosition, int objectNumber, int space){
return startPosition + objectNumber * (objectWidth + space);
}
Now for your example:
getPositionOfNth(120, 0, 0, 10) == 0
getPositionOfNth(120, 0, 1, 10) == 130
getPositionOfNth(120, 0, 2, 10) == 260
getPositionOfNth(120, 0, 2, 10) == 390
...
精彩评论