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

2013/1/5

Hit twice

TheStop TheStop

2013/1/5

#
Hi, is there a way to detect if an actor gets hit by a projectile say two times and once it gets hit the 2nd time something happens.
 Actor Bullet;
         Bullet = getTwointersectingObjects(Bullet.class);
        if ( Fly!= null)
        {
               World world;
            world = getWorld();
            
            world.addObject(new HealthBar3(),getX() , 300 );
           world.removeObject(Fly);
}

Something like this. I know that getTwoIntersectingObject will not work just making somewhat of an example to try to solve this question, Thanks for your time
danpost danpost

2013/1/5

#
What you need to do is add an instance boolean field to the object being hit twice (being initialized to 'false'). If the object is hit and the value is false, set it to 'true'. If the object is hit and the value is true, something happens.
TheStop TheStop

2013/1/5

#
Okay, thanks for your help.
TheStop TheStop

2013/1/5

#
 public  boolean DoubleHit()  
   {
    Actor shot = getOneIntersectingObject(Bullet1.class)
    }
     

public void CheckHit()
{
if(DoubleHit())
{
 world.addObject(new GuardianHealthBar3(),getX() , 300 );
           world.removeObject(Guardian);
}
else
{

}


}
this is what I have so far but I do not really know what to put for the boolean method, above you wrote "If the object is hit and the value is false, set it to 'true'. If the object is hit and the value is true, something happens" I probably am reading this wrong but I would want the Object to be hit twice before it responds true and then have something happen, so how would I let the Object know if it has been hit twice (reply true).
danpost danpost

2013/1/5

#
Actually, what I was saying was that the boolean tracks whether it has been hit once or not.
boolean hitOnce;

public void checkHit()
{
    Actor shot = getOneIntersectingObject(Bullet1class);
    if(shot == null) return;
    if(hitOnce)
    {
        // do something
        getWorld().removeObject(shot);
        getWorld().removeObject(this);
    }
    else
    {
        hitOnce = true;
        getWorld().removeObject(shot);
    }
}
Just call 'checkHit();' from the 'act' method.
You need to login to post a reply.