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

2020/2/27

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.

footpickle footpickle

2020/2/27

#
I am trying to make a bomb that explodes, but when it leaves the world it breaks another line of code. anything wrong with this code?
 if (isAtEdge())
        {
            setImage( myGif2.getCurrentImage() );
            move(-30);
            getWorld().removeObject(this);
        }
        else if(life <= 5)
        {
            setImage( myGif2.getCurrentImage() );
            move(-30);
        }
        if(life <= 0) 
        {
            setImage( myGif2.getCurrentImage() );
            move(-30);
            getWorld().removeObject(this);
            
        }
        if (isTouching(Boss.class))
        {
           setImage( myGif2.getCurrentImage() );
            move(-30);
            getWorld().removeObject(this);
        }
        else
        {
            life--;
            
        }
danpost danpost

2020/2/27

#
Add the following line immediately after each time you remove this (3 times):
return;
footpickle footpickle

2020/2/28

#
danpost wrote...
Add the following line immediately after each time you remove this (3 times):
return;
It didn't work... this is my code now
public void act() 
     {
        if (isAtEdge())
        { setImage( myGif2.getCurrentImage() );
            move(-30);
            getWorld().removeObject(this);
            return;
        }
        else if(life <= 5)
        {
            setImage( myGif2.getCurrentImage() );
            move(-30);
        }
        else if(life <= 0) 
        {
            move(-30);
            getWorld().removeObject(this);
            return;
        }
        else if (isTouching(Boss.class))
        {
           setImage( myGif2.getCurrentImage() );
            move(-30);
            getWorld().removeObject(this);
            return;
        }
        else
        {
            life--;
            
        }
        move(30);
        
     }
danpost danpost

2020/2/28

#
footpickle wrote...
It didn't work... this is my code now << Code Omitted >>
What is the error now (copy/paste)?
footpickle footpickle

2020/2/28

#
There is no error message, it just doesn't work as it should.
footpickle footpickle

2020/2/28

#
I think it is because it removes the object immediately, but I want it to explode for at least 1 second beforehand, is there a way to delay the removal of the bomb?
danpost danpost

2020/2/28

#
An explosion is not a bomb. Use a separate Actor subclass for the explosion.
footpickle footpickle

2020/3/1

#
Do I make the explosion a subclass of the Bomb? Also, it says "method addObject in class greenfoot.World cannot be applied to given types: required: greenfoot.Actor, int, int found: java.lang.Class<Explosion> reason: actual and formal argument lists differ in length." The code I have is this
if (isAtEdge())
        { 
            move(-30);
            getWorld().removeObject(this);
            getWorld().addObject(Explosion.class);
            return;
        }
footpickle footpickle

2020/3/1

#
Now it says java.lang.OutOfMemoryError: Java heap space (in java.awt.image.DataBufferInt)
danpost danpost

2020/3/1

#
footpickle wrote...
Do I make the explosion a subclass of the Bomb?
danpost wrote...
Use a separate Actor subclass for the explosion.
Also, it says "method addObject in class greenfoot.World cannot be applied to given types: << Error Omitted >> The code I have is this << Code Omitted >>
The error message tells you what you need for the arguments of the addObject call on line 5 -- an Actor object and two int values (the coordinates where the actor should be placed; not a class. Also, you better add the explosion before removing the bomb or the bomb will lose its reference to the world and won't be able to place the bomb in it.
footpickle footpickle

2020/3/1

#
Everything works fine now, thanks! But The gif loops over and over instead removing itself. How do I make the explosion remove itself after the first loop of the animation? Thanks for all your help so far, though!
footpickle footpickle

2020/3/2

#
It doesnt matter anymore i figured it out :)
You need to login to post a reply.