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

2019/3/20

Power Up issues

Erasedsword Erasedsword

2019/3/20

#
Hello I’m having one more issue with the game. I’m not sure if this is all the time or when it’s doing this, however when one spawns in via method called in the world class the terminal pops up saying actor is not in world. I asked my instructor about this and I’m pretty sure he didn’t know why either. The code for each class is almost the exact same although they are calling different methods to increase their respective value. I will post the code for my PowerUp class here in about 10 minutes as well as it’s spawner method. The same error is coming up for each of the two power ups so I don’t think that it would do much if I posted both. Although if you need me to post both I will. Thank you for all your help.
danpost danpost

2019/3/20

#
Erasedsword wrote...
The same error is coming up for each of the two power ups
"error"? The way to solving an error begins with the error output in your terminal window and knowing what it is telling you. Clear the terminal window; recreate the error and copy/paste the entire terminal output here.
Erasedsword Erasedsword

2019/3/20

#
I will try, however it occurred when I downloaded the file on the desktop at my school and not on the laptop I normally use
Erasedsword Erasedsword

2019/3/20

#
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.isTouching(Actor.java:943) at DUP.effect(DUP.java:30) at DUP.act(DUP.java:43) 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) is the error produced
Erasedsword Erasedsword

2019/3/20

#
    /**
     * allows the powerup to move twords the ship
     */
    public void movement()
    {
        move(-5);
    }

    /**
     * if the powerup touches the edge of the world, or the ship, its removed
     * if it touches the ship however it increases the ships damage to the enemy
     */
    public void effect()
    {
        Space space = (Space)getWorld();
               if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
        
        if (isTouching(Ship.class))
        {
            space.DamageSP(1);
            getWorld().removeObject(this);
        }
        
    }

    /**
     * Increase damage and move left
     */
    public void act() 
    {
        effect();
        movement();
    }    
is the code for class DUP
Erasedsword Erasedsword

2019/3/20

#
and this is the code for the method being called
    /**
     * spawns in each powerup and gives them their effects
     */
    private void spawner()
    {
        PowerUp powerUp = new PowerUp();
        DUP dup = new DUP();
        if (Greenfoot.getRandomNumber(900) ==2)
        {
            addObject(dup, 599, Greenfoot.getRandomNumber(325)+75);
        }

        if (Greenfoot.getRandomNumber(900)==1)
        {
            addObject(powerUp, 599, Greenfoot.getRandomNumber(325)+75);
        }
    }
danpost danpost

2019/3/20

#
In the DUP class code, add the following line after line 18:
return;
Then, in the act method, add the following line after line 34:
if (getWorld() == null) return;
These lines will immediately cause an exit from the methods so that no further code that require the actor be in the world be executed.
You need to login to post a reply.