1

I have had a recurring problem which has continued to surface the past couple of years and I'd like to finally resolve it.

So I have an Android game which is a bit like Space Invaders (enemies in a line move across the screen). The issue I have is with supporting different screen sizes with Surface View. Clearly the distance the enemy moves will be dependent on the screen size to keep the same difficulty level. So I have a ratio for width and height that I multiply everything by to make sure everything is drawn in the correct locations proportional to the screen size. Such as the following:

posX += (int)(GameScreen.wRatio * speed + 0.5f); 

The obvious problem here is that posX is an int and I am casting a float (since drawing a bitmap only takes int parameters). So what ends up happening, is that in some cases the position will be rounded up to the next int making the game faster than it should be. Or, in some cases posX is rounded to 0.

What would be the best approach to avoid this? I can assume that always incrementing by the smallest I can ( by 1 ) and then skipping increments for particular enemies on each update of the game loop. But, this would require a lot of changes to my code and I would like to hear what other suggestions there may be before I do that.

4

0 に答える 0