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

2017/2/16

Removing Object

joshuadswa joshuadswa

2017/2/16

#
Hello, i have some problems here here the code first public void act() { checkHit(); checkProjectile(); hitActor(); } public void checkHit() { Actor projectile; projectile = getOneObjectAtOffset(0, 0, Projectile1.class); if(getWorld().getObjects(Minion1.class).isEmpty()) { getWorld().removeObject(projectile); } } what i want is, when the Projectile hit an actor, and actor gone and then the projectile who chasing the actor got removed too @_@ but thats not happening, instead theres error : java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 @_@ help sorry for my english
danpost danpost

2017/2/16

#
joshuadswa wrote...
< Code Omitted > what i want is, when the Projectile hit an actor, and actor gone and then the projectile who chasing the actor got removed too @_@ but thats not happening, instead theres error : java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
I honestly do not see where you would get that error from the given code. However, there are a couple of other methods that are called from the act method that you are not showing. Also, please specify which class this code is located (the actor being chased or the projectile). Just seems strange to have those particular set of method names being called from the act method to be in either of the classes.
joshuadswa joshuadswa

2017/2/16

#
ah yes i forgot this one in Projectile1 class .-.
danpost danpost

2017/2/16

#
joshuadswa wrote...
ah yes i forgot this one in Projectile1 class .-.
Then, remove these lines:
Actor projectile;
projectile = getOneObjectAtOffset(0, 0, Projectile1.class);
and change the following line:
getWorld().removeObject(projectile);
to this:
getWorld().removeObject(this);
joshuadswa joshuadswa

2017/2/16

#
import greenfoot.*; import java.util.List; /** * Write a description of class Projectile1 here. * * @author (your name) * @version (a version number or a date) */ public class Projectile1 extends Actor { /** * Act - do whatever the Projectile1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkHit(); checkProjectile(); hitActor(); } public void checkHit() { if(getWorld().getObjects(Minion1.class).isEmpty()) { getWorld().removeObject(this); } } public void checkProjectile() { Minion1 minion = (Minion1) getWorld().getObjects(Minion1.class).get(0); turnTowards(minion.getX(), minion.getY()); move(1); } public void hitActor() { Actor minion; minion = getOneObjectAtOffset(0, 0, Minion1.class); if(minion!=null) { getWorld().removeObject(minion); Greenfoot.delay(5); } } } theres and error in public void hitActor() >.< well gonna check it one on one
joshuadswa joshuadswa

2017/2/16

#
Thx :D
danpost danpost

2017/2/16

#
If there is any possibility that there is no Minion1 object in the world, then you cannot execute the following line:
Minion1 minion = (Minion1) getWorld().getObjects(Minion1.class).get(0);
without first making sure that one is indeed in the world. Replace the 'checkProjectile' method with the following:
public void checkProjectile()
{
    if (getWorld().getObjects(Minion1.class).isEmpty()) return;
    Actor minion = (Actor) getWorld().getObjects(Minion1.class).get(0);
    turnTowards(minion.getX(), minion.getY());
    move(1);
}
Then, change your act method to this:
public void act()
{
    checkProjectile();
    hitActor();
    checkHit();
}
Since 'checkHit' could potentially remove the projectile from the world and you cannot execute either of the other methods called from act unless the actor is in the world, it makes sense to call 'checkHit' last.
joshuadswa joshuadswa

2017/2/16

#
It works thanks :) i guess i made a mistake on my code, i move the refactoring of hitActor(); to minion1 class, and change the minion => projectile anyway thanks >.< its really help a lot i can finally sleep peacefully, good night :)
You need to login to post a reply.