views:

169

answers:

1

I'm making a scrolling game on Android and am having a hard time figuring out why the code below does not decrement past 0.

Objects start at the end of the screen (so the x position is equal to the width of the screen) the objects move accross the screen by decrementing their x positions. I want them to scroll off of the screen, but when the x position hits 0, the objects just stay at 0, they do not move into the negatives.

Here is my code to move objects on the screen

private void incrementPositions(long delta) {

    float incrementor = (delta / 1000F) * Globals.MAP_SECTION_SPEED;        

    for(Map.Entry<Integer, HashMap<Integer, MapSection>> column : scrollingMap.entrySet()) {
        for(Map.Entry<Integer, MapSection> row : column.getValue().entrySet()) {
            MapSection section = row.getValue();
            section.x -= incrementor;           
        }
    }       
}

It works ok if I change

section.x -= incrementor;

to

section.x = section.x - (int)incrementor;

but if i do that the scrolling doesn't appear as smooth.

+1  A: 

You could try storing the section as a float value and only converting it to an int if needed at the end of the calculation (only for rendering).

Lavinski
this work out well thank you!
hanesjw