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

2020/12/5

Hello everyone. i have a little problem with my codes.

Xarloz Xarloz

2020/12/5

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Projectile here. * * @author * @version (a version number or a date) */ public class Projectile extends Mover { /** * Act - do whatever the Projectile wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ int speed = 10; int timer=23; public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { getWorld().removeObject(actor); } } public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); return actor != null; } public Projectile() { getImage().scale(30,30); } public void act() { // turnToMouse(); setLocation(getX()-10,getY()); if(timer>0){timer--;} if(timer==0){getWorld().removeObject(this);} lookForZombie1(); } public void turnToMouse() { turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY()); } public void lookForZombie1() { if ( canSee(Zombie1.class) ) { eat(Zombie1.class); } } }
Xarloz Xarloz

2020/12/5

#
everytime i shoot with my Soldier, the Projectile flies like 30 meters or so. But when it has reached 30 meters the game stops and it shows me a mistake in the code.
Xarloz Xarloz

2020/12/5

#
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:713) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:917) at Projectile.canSee(Projectile.java:29) at Projectile.lookForZombie1(Projectile.java:52) at Projectile.act(Projectile.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) 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:713) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:917) at Projectile.canSee(Projectile.java:29) at Projectile.lookForZombie1(Projectile.java:52) at Projectile.act(Projectile.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) 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:713) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:917) at Projectile.canSee(Projectile.java:29) at Projectile.lookForZombie1(Projectile.java:52) at Projectile.act(Projectile.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) 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:713) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:917) at Projectile.canSee(Projectile.java:29) at Projectile.lookForZombie1(Projectile.java:52) at Projectile.act(Projectile.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2020/12/5

#
Move the last line in the act method up two lines (so you do not look for zombies after being removed from the world).
You need to login to post a reply.