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

2014/12/16

Need Help!

xJesterPROx xJesterPROx

2014/12/16

#
im trying to get my bullet to disapear when it hits an enemy, but right now all i have been able to do is get it to disappear when it hits a world edge, please help this poor noob!
danpost danpost

2014/12/16

#
Please show the class code of the bullet.
xJesterPROx xJesterPROx

2014/12/16

#
/** * Write a description of class Lazer here. * * @author (your name) * @version (a version number or a date) */ public class Lazer extends Good_Guys { /** * Act - do whatever the Lazer wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(7); kill(); if (atWorldEdge()) { getWorld().removeObject(this); } } public void kill() { Actor Alien; Alien = getOneObjectAtOffset(0, 0, Alien.class); if (Alien != null) { World My_Way; My_Way = getWorld(); My_Way.removeObject(Alien); } } public Lazer() { turn(-90); } }
danpost danpost

2014/12/16

#
Change your kill method to this:
public boolean kill()
{
    Actor alien = getOneObjectAtOffset(0, 0, Alien.class);
    if (alien != null)
    {
        getWorld.removeObject(alien);
        return true;
    }
    return false;
}
The only thing I really changed was having it return a boolean indicating whether a kill was made or not. Now, you can change your act method to this:
public void act()
{
    move(7);
    if (atWorldEdge() || kill())
        getWorld().removeObject(this);
}
Question: is a Lazer object a Good_Guys objects? I do not mean 'does it belong to, or come from, a good guy object'; but, I mean 'as a bee is an insect, is a lazer a good-guys?
xJesterPROx xJesterPROx

2014/12/16

#
to answer your question, yes, and i put in your code, but i don't have getWorld as a variable, how would i add that in? (what would i write for it?)
Super_Hippo Super_Hippo

2014/12/16

#
He only forgot the brackets. Change line 6 to this:
getWorld().removeObject(alien);
xJesterPROx xJesterPROx

2014/12/17

#
Thanks Guys!!!
You need to login to post a reply.