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

2018/3/25

My bullet code is not working

mountwo22 mountwo22

2018/3/25

#
I have a 2 bullet actors and 2 enemy actors. When I shoot the bullet and it touches the zombie, everything works fine. The zombie has health and when it loses it all, it gets deleted from the world. However, when the bullet gets to the end of the screen, it gives this error code
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:711)
	at greenfoot.Actor.getOneObjectAtOffset(Actor.java:913)
	at BulletLeft.killZombie(BulletLeft.java:29)
	at BulletLeft.act(BulletLeft.java:24)
	at greenfoot.core.Simulation.actActor(Simulation.java:604)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
	at greenfoot.core.Simulation.runContent(Simulation.java:221)
	at greenfoot.core.Simulation.run(Simulation.java:211)
This is my bullet code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BulletLeft here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BulletLeft extends Actor
{
    boolean removed = false;
    boolean destroy = false;
    /**
     * Act - do whatever the BulletLeft wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(-6);
        if(getX() <= 1 || getX() >= 1599 || getY() >=822 || getY() <=1 || destroy == true)
        {
            getWorld().removeObject(this);
        }
        killZombie();
    }
    public void killZombie()
    {
        MyWorld lWorld = (MyWorld)getWorld();
        ZombieRight zombieRight = (ZombieRight)getOneObjectAtOffset(0, 0, ZombieRight.class);
        ZombieLeft zombieLeft = (ZombieLeft)getOneObjectAtOffset(0, 0, ZombieLeft.class);
        if(zombieRight != null)
        {
            zombieRight.loseHealth();
            getWorld().removeObject(this);
        }
        else if(zombieLeft != null)
        {
            zombieLeft.loseHealth();
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2018/3/25

#
The BulletLeft object cannot look for a zombie of any kind, using any collision type method, if it is not in a world. In other words, you cannot call killZombie from the act method after it is removed from the world because it reached an edge. Llikewise, it is true that you cannot check for reaching an edge if it is removed from the world because it found a zombie. Fortunately, you can abort further execution of a method by adding a:
return;
statement in your code after the bullet is removed from the world.
Yehuda Yehuda

2018/3/25

#
My computer said you replied "-54 minutes ago", when I reloaded the page it said "-52 minutes ago", it seems we're getting nearer to the time you (will) repl(y/ied). The reason I wanted to check the time you replied is: on the home page it said, "about 1 hour ago", but I thought I remember seeing there be no replies to this discussion less than ten minutes ago, so I was wondering if I just wasn't remembering correctly. It seems I was remembering correctly, just the computer isn't. This reply just happened "-60 minutes ago".
danpost danpost

2018/3/25

#
Yehuda wrote...
This reply just happened "-60 minutes ago".
It is bonkers :\
Super_Hippo Super_Hippo

2018/3/25

#
DST only causes problems...
Yehuda Yehuda

2018/3/25

#
I didn't get notified of these replies, they're "Old Notifications". But, Hippo, you don't have any problems (no negative), maybe that's because you're in a different time zone.(?) My first post is going to happen in 2 minutes.
mountwo22 mountwo22

2018/3/25

#
Thank you so much
You need to login to post a reply.