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

2015/5/2

dynamic moving object?

Allhard Allhard

2015/5/2

#
Hello again, i got a problem with a moving object in my shooting game development. the problem is whenever i move (with keypress) my actors and press the shoot button both at the same time, the game keeps crashing and give this notif error
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:681) at greenfoot.Actor.getX(Actor.java:157) at AimDisplay.move(AimDisplay.java:147) at AimDisplay.act(AimDisplay.java:94) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
i used this basic code:
public void move()
    {
        int speed = 1;
        {
        if(Greenfoot.isKeyDown("left"))
        setLocation(getX() - speed, getY());
        if(Greenfoot.isKeyDown("right"))
        setLocation(getX() + speed, getY());
        }
    }
lordhershey lordhershey

2015/5/2

#
from the error message it looks as if this method is being called after the actor has been removed from the world - can we see you act() Method?
Allhard Allhard

2015/5/2

#
here
 public void act() 
    {
        MouseInfo info = Greenfoot.getMouseInfo();
        if (info != null)
        {
            int xDir = info.getX() - getX();
            int yDir = info.getY() - getY();
            
            if (yDir >= -1)
            {
                yDir = -1;
            }
            
            double angle = Math.atan2(yDir, xDir);
            
            if (angle >= -Math.toRadians(MonkeyWorld.LOWEST_ANGLE))
            {
                angle = -Math.toRadians(MonkeyWorld.LOWEST_ANGLE);
            }
            if (angle <= -Math.toRadians(180 - MonkeyWorld.LOWEST_ANGLE))
            {
                angle = -Math.toRadians(180 - MonkeyWorld.LOWEST_ANGLE);
            }
            
            double distProp = Math.hypot(xDir, yDir) / (double)radius;
            
            if (distProp > 1)
            {
                distProp = 1;
            }
            else if (distProp < MIN_PROP)
            {
                distProp = MIN_PROP;
            }

            
            
            double forceFactor = (distProp - MIN_PROP) / (1 - MIN_PROP);
                        
            redraw(angle, forceFactor);
            
            moveTarget(angle, forceFactor);
            
            if (Greenfoot.mouseClicked(null))
            {
                prevX = curX;
                prevY = curY;
                prevValid = true;
                player.fire(angle, forceFactor);
                getWorld().removeObject(this);
            }
        }
        move();
    }    
lordhershey lordhershey

2015/5/2

#
change this
if (Greenfoot.mouseClicked(null))
           {
               prevX = curX;
               prevY = curY;
               prevValid = true;
               player.fire(angle, forceFactor);
               getWorld().removeObject(this);
           }
to this
if (Greenfoot.mouseClicked(null))
           {
               prevX = curX;
               prevY = curY;
               prevValid = true;
               player.fire(angle, forceFactor);
               getWorld().removeObject(this);
               return;
           }
Allhard Allhard

2015/5/2

#
@lordhershey thanks a lot, i missed that line XD
You need to login to post a reply.