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

2013/10/21

STOP MOVING OBJECTS AT THE SAME TIME

LuMC91 LuMC91

2013/10/21

#
Hey guys, i need some help here: How can i do to make 2 or more objects from the same class stop at the same time when one of them the reaches a limit? for example: i have a row of players moving at the same time but when one of them reaches a limit of the world everyone should stop moving.
danpost danpost

2013/10/21

#
You can add a 'static boolean' field to the class, call it 'inactive' and in the act method, when an actor reaches a limit set it to 'false'. Put everything related to the actor moving in the conditional block 'if (!inactive) { /* move code here */ }'. You will need another trigger to re-start the actors (change the boolean field value back to 'true'). BTW: I sense another Space Invaders remake (am I right?).
LuMC91 LuMC91

2013/10/21

#
danpost .. when i do that , what it actually does is that the players stop individually.. i mean they don't stop at the same time, if the first player reaches the limit the other ones just keep going until they reach the limit , just when each one of them reaches the limit then their respective field 'inactive' turns false. I hope i can make me understand. BTW: Your sense is wrong xD
danpost danpost

2013/10/21

#
Please show the code in the class of the players Sense is off! Good! Would like to see something different.
LuMC91 LuMC91

2013/10/21

#
It's just one player, several instances of the same class public class Player extends Actor { private boolean inactive; public Player() { inactive=false; } public void act() { if(reachesTopOfTheWorld()) inactive=true; if(!inactive) movement(); } public void movement() { if(Greenfoot.isKeyDown("right")) setLocation(getX()+1,getY()); if(Greenfoot.isKeyDown("left")) setLocation(getX()-1,getY()); if(Greenfoot.isKeyDown("up")) setLocation(getX(),getY()-1); if(Greenfoot.isKeyDown("down")) setLocation(getX(),getY()+1); } }
danpost danpost

2013/10/21

#
Where is your 'reachesTopOfWorld' method?
LuMC91 LuMC91

2013/10/21

#
if( getY()-getImage().getHeight() == 0 ) // if the player reaches the top of the world
danpost danpost

2013/10/21

#
LuMC91 wrote...
if( getY()-getImage().getHeight() == 0 ) // if the player reaches the top of the world
Try
if (getY()-getImage().getHeight() <= 0)
using 'less than or equal to'.
LuMC91 LuMC91

2013/10/21

#
I did.. nothing changed.. i don't think that is the problem.. the player does detect the top. It's a row of players, one below the other, and when the first one reaches the top, then all the row should stop moving and stay in their position and with the code i posted everyone keeps moving until they reach the top.
danpost danpost

2013/10/21

#
I see, you did not add the 'static' keyword to the 'inactive' field. It should be 'private static boolean inactive;'
LuMC91 LuMC91

2013/10/21

#
It works! I can't believe that was all the problem, I thought it didn't matter wether it was static or not.. thanks man.
danpost danpost

2013/10/21

#
I makes a big difference. You could say a difference between 'All for one' and 'One for all' -- meaning non-static fields are for each instance (one for each instance ), while static fields are for all instances (the one field is shared by all instances).
You need to login to post a reply.