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

2012/10/26

Disappear problem

BradH BradH

2012/10/26

#
Actor fly; fly = getOneObjectAtOffset(0, 0, fly.class); if (fly != null) { World world; world = getWorld(); world.removeObject(fly); world.removeObject(this); Greenfoot.playSound("popbomb2.wav"); } I have this code to remove a projectile from the world once it destroys an enemy, but whenever it destroys an enemy an error there was an attempt to use the actor's location when it was not in the world, I have it in the projectile class under the act method any suggestions
Gevater_Tod4711 Gevater_Tod4711

2012/10/27

#
This error means that you try to get the position of your actor later on in your code but if you remove the object it hasen't got any x or y coordinates. You could write the code for removing at the end of your act class or add a return statement after removing your object:
Actor fly = getOneObjectAtOffset(0, 0, fly.class);
if (fly != null) {
    Greenfoot.playSound("popbomb2.wav");
    getWorld().removeObject(fly);
    getWorld().removeObject(this);
    return;
} 
the return will always stop the executing of the method. So the object doesn't try to get it's x or y coordinates.
BradH BradH

2012/10/27

#
Ok i will try it out thanks
BradH BradH

2012/10/27

#
The return; works with that, but It causes a problem with the (atWorldEdge()) method i have in the same class /** * atWorldEdge() method that checks if the Longshot is at the end of the world, if it is, return true, otherwise false */ public void disappear() { if (atWorldEdge()) {World world; world = getWorld(); points++; world.removeObject(this); } }
danpost danpost

2012/10/27

#
Explain, please ("it causes a problem" does tell us much). Show your act() method.
BradH BradH

2012/10/27

#
The error is the same as before (actor not in this world) but below (below the error message it shows a list of problem areas that say the problem occurs at ) before I had the return; in the actor fly class the problem areas were just in the projectile class under the moveAndTurn(); method, but now it is in the disappear(); method(atworldedge)) this is the code for the projectile class(the disappear class is where the terminal says the problem is at) which probably means that in the mover class I might have to change something with the (atworldedge) method import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class longshot here. * * @author (your name) * @version (a version number or a date) */ public class longshot extends Mover { private int points = 0; /** * Act - do whatever the Longshot wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveAndTurn(); disappear(); } public void moveAndTurn() { move(2); Actor fly; fly = getOneObjectAtOffset(0, 0, fly.class); if (fly != null) { World world; world = getWorld(); world.removeObject(fly); world.removeObject(this); Greenfoot.playSound("popbomb2.wav"); return; } Actor bee; bee = getOneObjectAtOffset(0, 0, bee.class); if (bee != null) { World world; world = getWorld(); world.removeObject(bee); Greenfoot.playSound("boom2.wav"); } Actor alienfire; alienfire = getOneObjectAtOffset(0, 0, alienfire.class); if (alienfire != null) { World world; world = getWorld(); world.removeObject(alienfire); Greenfoot.playSound("boom2.wav"); } Actor warrior; warrior = getOneObjectAtOffset(0, 0, warrior.class); if (warrior != null) { World world; world = getWorld(); world.removeObject(warrior); Greenfoot.playSound("boom2.wav"); } Actor mother; mother = getOneObjectAtOffset(0, 0, mother.class); if (mother != null) { World world; world = getWorld(); world.removeObject(mother); Greenfoot.playSound("boom2.wav"); } Actor shooter; shooter = getOneObjectAtOffset(0, 0, shooter.class); if (shooter != null) { World world; world = getWorld(); world.removeObject(shooter); Greenfoot.playSound("boom2.wav"); } } /** * atWorldEdge() method that checks if the Longshot is at the end of the world, if it is, return true, otherwise false */ public void disappear() { if (atWorldEdge()) {World world; world = getWorld(); points++; world.removeObject(this); } } }
BradH BradH

2012/10/27

#
This is the atworldedge in the Mover class /** * Test if we are close to one of the edges of the world. Return true is we are. */ public boolean atWorldEdge() { if(getX() < 1 || getX() > getWorld().getWidth() - 1) return true; if(getY() < 1 || getY() > getWorld().getHeight() - 1) return true; else return false; } }
danpost danpost

2012/10/27

#
In the 'act' method of the longShot class you have two method calls:. one to 'moveAndTurn' and the other to 'disappear'. In the 'moveAndTurn' method, it is possible that 'this' object is removed from the world; so, there are no world edges to check location against in the call to 'disappear'; hence, the error. Easiest fix: between the two method calls in the 'act' method, add the following line:
if (getWorld() == null) return;
This will ensure that 'disappear' will not be executed if the object is not, at this point', in the world.
danpost danpost

2012/10/27

#
In the Mover class, change your 'atWorldEdge' method to the following:
public boolean atWorldEdge()
{
    return getX() == 0 || 
               getX() == getWorld().getWidth() - 1 ||
               getY() == 0 ||
               getY() == getWorld().getHeight() - 1;
}
Unless your world is unbounded, getX() will never be greater than getWorld().getWidth() - 1 and getY() will never be greater than getWorld().getHeight() - 1.
You need to login to post a reply.