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

2013/2/24

stops game

aananon aananon

2013/2/24

#
I'm trying to remove a moving object every time I click on it, but it stops the game every time I click on the object(after it's been removed). Do any of you know why this is happening? This is my code for that method: public void removeM() { if(Greenfoot.mouseClicked(this)) { getWorld().removeObject(this); } }
danpost danpost

2013/2/24

#
The code given is not the problem. Please show the class code.
aananon aananon

2013/2/24

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class M extends Animal { public void act() { removeM(); turnatEdge(); move(); } public void removeM() { if(Greenfoot.mouseClicked(this)) { getWorld().removeObject(this); } } public void turnatEdge() { if ( atWorldEdge() ) { turn(17); } } }
danpost danpost

2013/2/24

#
In your 'act' method, you are calling 'removeM' first; then calling 'turnatEdge' and 'move', both of which require that the object be in the world. If 'removeM' removes the object from the world, 'turnatEdge' will error. An easy fix would be to rearrange the method calls in your act method.
public void act() 
{
   turnatEdge();
   move();
   removeM();
}
aananon aananon

2013/2/24

#
oh I see, thanks! :)
aananon aananon

2013/2/24

#
Also, can you help me with this as well? I'm trying to get my object to move left, if it hits the right edge of the world, move right, if it hits the left edge of the world, move down if it hits the top edge, and move up if it hits the right edge. This is the method:....but when it hits the right edge, for example, it moves left but then right again...because I have move(); in the act method. public void turnatEdge() { if ( atLeftEdge() ) { setLocation(getX()+2, getY()); } if (atRightEdge()) { setLocation(getX()-2, getY()); } if (atBottomEdge()) { setLocation(getX(), getY()-2); } if (atTopEdge()) { setLocation(getX(), getY()+2); } } Thanks, I really appreciate your help.
danpost danpost

2013/2/25

#
What you need to do is either control the rotation of the object so that the call to the 'move' method will move the object in the proper direction or keep track of the speed of the object in both the x and y directions. With either method, the proper adjustments to the rotation or value of the variables are needed when the object hits the walls.
You need to login to post a reply.