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

3 days ago

crash

jbiser361 jbiser361

3 days ago

#
hello! I am continually receiving this error when the turtle dies(if he doesn't eat enough, he dies) 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 Turtle.moreHealth(Turtle.java:63) at Turtle.act(Turtle.java:45) 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) those are the erors im getting, any help would be great!
danpost danpost

3 days ago

#
jbiser361 wrote...
hello! I am continually receiving this error when the turtle dies(if he doesn't eat enough, he dies) << Error Trace Omitted >> those are the erors im getting, any help would be great!
Please show Turtle class act method.
jbiser361 jbiser361

2 days ago

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Turtle extends SmoothMover { /** * Act - do whatever the Turtle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int velocity = 5; private int delay = 0; public int health = 10; private int randomNumSpawn = (int)(Math.random()*936); public void act() { delay++; if ( Greenfoot.isKeyDown("up") ) { setLocation ( getX(), getY() - 5 ); } if ( Greenfoot.isKeyDown("down") ) { setLocation ( getX(), getY() + 5 ); } if ( Greenfoot.isKeyDown("right") ) { velocity+=10; } if ( Greenfoot.isKeyDown("left") ) { velocity-=10; } move(velocity); velocity*=0.95; if ( velocity > 0 ) { setImage ( "turtleRight.png" ); } if( velocity < 0) { setImage ( "turtleLeft.png" ); } die(); moreHealth(); } private void die(){ if(health == 0){ getWorld().removeObject(this); } if(delay == 30){ health--; delay= 0; } } public void moreHealth(){ // caught error here. if(isTouching(Foof.class)){ health++; } // Actor Foof; // Foof = getOneObjectAtOffset(0,0, Foof.class); // if(isTouching(Foof.class)){ // health += 5; // Foof f = new Foof(); // getWorld().addObject(f, randomNumSpawn,0); // getWorld().removeObjects(getWorld().getObjects(Foof.class)); // } } public Integer hralthValue(){ return health; } } Food class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Foof here. * * @author (your name) * @version (a version number or a date) */ public class Foof extends Actor { String picArray = {"food0", "food1", "food2", "food3", "food4", "food5"}; private int randomNum = (int)(Math.random()*7)-1; private String converter = Integer.toString(randomNum); private int randomNumSpawn = (int)(Math.random()*936); /** * Act - do whatever the Foof wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(randomNum == -1){ randomNum++; converter = Integer.toString(randomNum); //yk idk why i made this more complecated than its already was... } setLocation(getX(), getY()+8); setImage(new GreenfootImage(converter + ".png")); if(isAtEdge() || isTouching(Turtle.class) ){ Foof f = new Foof(); getWorld().addObject(f, randomNumSpawn,0); getWorld().removeObject(this); } } }
danpost danpost

2 days ago

#
jbiser361 wrote...
<< Code Omitted >>
Change the last line in your act method to:
if (getWorld() != null) moreHealth();
or you can insert the following before the last line in the act method:
if (getWorld() == null) return;
or you can switch the last two lines in your act method. Since the die method can potentially remove the turtle from the world, you must ensure no method that requires the actor be in the world is called after calling the die method (such as 'isTouching' in the moreHealth method)..
jbiser361 jbiser361

2 days ago

#
for the turtle right? or for the food
jbiser361 jbiser361

2 days ago

#
because either way it still crashes the project... import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Turtle extends SmoothMover { /** * Act - do whatever the Turtle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int velocity = 5; private int delay = 0; public int health = 10; private int randomNumSpawn = (int)(Math.random()*936); public void act() { delay++; if ( Greenfoot.isKeyDown("up") ) { setLocation ( getX(), getY() - 5 ); } if ( Greenfoot.isKeyDown("down") ) { setLocation ( getX(), getY() + 5 ); } if ( Greenfoot.isKeyDown("right") ) { velocity+=10; } if ( Greenfoot.isKeyDown("left") ) { velocity-=10; } move(velocity); velocity*=0.95; if ( velocity > 0 ) { setImage ( "turtleRight.png" ); } if( velocity < 0) { setImage ( "turtleLeft.png" ); } die(); moreHealth(); if (getWorld() != null) moreHealth(); // you mean it there right? } private void die(){ if(health == 0){ getWorld().removeObject(this); } if(delay == 30){ health--; delay= 0; } } public void moreHealth(){ // caught error here. if(isTouching(Foof.class)){ health++; } // Actor Foof; // Foof = getOneObjectAtOffset(0,0, Foof.class); // if(isTouching(Foof.class)){ // health += 5; // Foof f = new Foof(); // getWorld().addObject(f, randomNumSpawn,0); // getWorld().removeObjects(getWorld().getObjects(Foof.class)); // } } }
danpost danpost

2 days ago

#
danpost wrote...
Change the last line in your act method to:
if (getWorld() != null) moreHealth();
Not "Add as a new last line".
jbiser361 wrote...
for the turtle right? or for the food
Yes -- for the turtle.
jbiser361 jbiser361

2 days ago

#
ooh I see! thank you so much!
You need to login to post a reply.