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

2015/1/21

removing bullet

howdoesonecode howdoesonecode

2015/1/21

#
when a bullet intersects an enemy the enemy is removed but i need the bullet to get removed also need help!!
danpost danpost

2015/1/21

#
The following statement will remove the bullet from the world if coded and executed from within a non-static method in the class of the bullet:
1
getWorld().removeObject(this);
howdoesonecode howdoesonecode

2015/1/21

#
danpost wrote...
The following statement will remove the bullet from the world if coded and executed from within a non-static method in the class of the bullet:
1
getWorld().removeObject(this);
ive tried that although i dont know if i am using it in the right context i was using it in the intersecting method i cant remember what the actual code i used was
howdoesonecode howdoesonecode

2015/1/21

#
danpost wrote...
The following statement will remove the bullet from the world if coded and executed from within a non-static method in the class of the bullet:
1
getWorld().removeObject(this);
here this is what i used it as
1
2
3
4
5
6
7
8
9
10
11
    public void destroyEnemies()
    {
        //"Enemy" can be any class that you want the bullet to destroy.
        Actor enemy = getOneIntersectingObject(Enemy.class);
        if(enemy != null)
        {
            World myWorld = getWorld();
            getWorld().removeObject(enemy);
            getWorld().removeObject(this);
    }
}
howdoesonecode howdoesonecode

2015/1/27

#
danpost wrote...
The following statement will remove the bullet from the world if coded and executed from within a non-static method in the class of the bullet:
1
getWorld().removeObject(this);
i put this in now im getting a weird error basically saying that the bullet is only supposed to delete at the world edge but it is deleting on a person
danpost danpost

2015/1/27

#
howdoesonecode wrote...
< Quote Omitted > i put this in now im getting a weird error basically saying that the bullet is only supposed to delete at the world edge but it is deleting on a person
Please copy/paste error messages in their entirety so that we understand what is happening. btw, the 'destroyEnemies' method looks fine; although line 7 is not needed (the 'myWorld' reference is not being used after declared and assigned the world)
howdoesonecode howdoesonecode

2015/1/28

#
danpost wrote...
howdoesonecode wrote...
< Quote Omitted > i put this in now im getting a weird error basically saying that the bullet is only supposed to delete at the world edge but it is deleting on a person
Please copy/paste error messages in their entirety so that we understand what is happening. btw, the 'destroyEnemies' method looks fine; although line 7 is not needed (the 'myWorld' reference is not being used after declared and assigned the world)
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:681) at greenfoot.Actor.getX(Actor.java:157) at bullet.atWorldEdge(bullet.java:22) at bullet.act(bullet.java:40) at greenfoot.core.Simulation.actActor(Simulation.java:583) at greenfoot.core.Simulation.runOneLoop(Simulation.java:541) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2015/1/28

#
It looks like (although I cannot be sure because you have not posted what your 'act' method looks like) you have this in your act method:
1
2
destroyEnemies();
if (atWorldEdge()) ...
If the actor is removed from the world in the 'destroyEnemies' method, then the 'getX' call in the 'atWorldEdge' method must fail -- the actor does not have a location in the world, so 'getX' is meaningless. To avoid the error, just make sure the actor is still in the world before calling 'atWorldEdge':
1
if (getWorld() != null && atWorldEdge())
howdoesonecode howdoesonecode

2015/1/28

#
danpost wrote...
It looks like (although I cannot be sure because you have not posted what your 'act' method looks like) you have this in your act method:
1
2
destroyEnemies();
if (atWorldEdge()) ...
If the actor is removed from the world in the 'destroyEnemies' method, then the 'getX' call in the 'atWorldEdge' method must fail -- the actor does not have a location in the world, so 'getX' is meaningless. To avoid the error, just make sure the actor is still in the world before calling 'atWorldEdge':
1
if (getWorld() != null && atWorldEdge())
1
2
3
4
5
6
7
8
9
10
public void act()
{
    setRotation(direction);
    move(19);
    if (getWorld() != null) kill();
    if (this.atWorldEdge())
    {
        getWorld().removeObject(this);
    }
}
howdoesonecode howdoesonecode

2015/1/28

#
danpost wrote...
It looks like (although I cannot be sure because you have not posted what your 'act' method looks like) you have this in your act method:
1
2
destroyEnemies();
if (atWorldEdge()) ...
If the actor is removed from the world in the 'destroyEnemies' method, then the 'getX' call in the 'atWorldEdge' method must fail -- the actor does not have a location in the world, so 'getX' is meaningless. To avoid the error, just make sure the actor is still in the world before calling 'atWorldEdge':
1
if (getWorld() != null && atWorldEdge())
it works thankyou 5 million times over!!!
You need to login to post a reply.