This site requires JavaScript, please enable it in your browser!
Greenfoot back
fastslash8
fastslash8 wrote ...

2014/2/17

Most efficient way to move 100+ actors?

fastslash8 fastslash8

2014/2/17

#
Hello! I'm trying to make a game in which many tiles move on the screen at the same time. At the moment, I'm using
if(Greenfoot.isKeyDown("right"){
    setLocation(getX() + 1, getY());
}
to move every tile right 1 pixel. However, the tiles sometimes seperate from each other a little bit. Is there a way to do this so that there are no gaps between the tiles? Thank you!
bourne bourne

2014/2/17

#
Getting gaps might have to do with that if the code you provided above is within the Tile class, Greenfoot.isKeyDown("right") might be returning false for the later Tiles being executed. Instead, to still move Actors around by the masses, do this once - inside the World's act method. Something like the following:
if (Greenfoot.isKeyDown("right")) {
    for (Tile tile : (java.util.List<Tile>)getObjects(Tile.class))
        tile.setLocation(tile.getX() + 1, tile.getY())
}
fastslash8 fastslash8

2014/2/17

#
Thank you so much! This works perfectly!
You need to login to post a reply.