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

2017/5/4

Help

alinasomcutean alinasomcutean

2017/5/4

#
I have a girl who must be moved when the "space" bar is press (the die is stopped at a random number between 1 and 6) and I wrote all the code and it has the syntax right, but the girl doesn't move and I don't know why. nr = Die.x; while(isTouching(Start.class)||isTouching(Box_2.class)||isTouching(Box_4.class)||isTouching(Box_6.class)||isTouching(Box_8.class)) { if( nr%2 == 1) { getWorld().removeObjects(getWorld().getObjects(Personaj.class)); getWorld().addObject( new Personaj (), 50, 590); } else { move_1(); } nr = Die.x; } public void move_1() { if(isTouching(Start.class)) { getWorld().removeObjects(getWorld().getObjects(Personaj.class)); getWorld().addObject( new Personaj (), 480, 580); } if(isTouching(Box_2.class)) { getWorld().removeObjects(getWorld().getObjects(Personaj.class)); getWorld().addObject( new Personaj (), 880, 580); } if(isTouching(Box_4.class)) { getWorld().removeObjects(getWorld().getObjects(Personaj.class)); getWorld().addObject( new Personaj (), 1050, 450); } if(isTouching(Box_6.class)) { getWorld().removeObjects(getWorld().getObjects(Personaj.class)); getWorld().addObject( new Personaj (), 680, 450); } if(isTouching(Box_8.class)) { getWorld().removeObjects(getWorld().getObjects(Personaj.class)); getWorld().addObject( new Personaj (), 290, 450); } } the girl should move from 0 to 2, for exemple, or 2 to 4 if the number is even, and move back to 0 if the number is odd. What do I do wrong?
danpost danpost

2017/5/4

#
What line of code is trying to move the girl? All I see are objects being removed and others being added to the world. What class is the code given in?
alinasomcutean alinasomcutean

2017/5/4

#
This is in the "Personaj" class, the girl class. I was trying to move the girl by removed her from where she is and add her again in the right place. And in the nr variable I memorated the number generate by die every time when the space is press from Die class.
danpost danpost

2017/5/4

#
Instead of removing an adding the girl,, just set her location. For example
if (isTouching(Start.class))
{
    setLocation(480, 580);
}
You need to login to post a reply.