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

2012/9/29

creating a powerup

davemib123 davemib123

2012/9/29

#
hi all, how would I go about creating powerup to give dual missles? I know how to insert the powerup on the screen and how to collect it. But how would I go about firing two missiles for about 20 seconds and then revert the hero back to normal. This is my scenario if you want to take a look which has just single missile fired: http://www.greenfoot.org/scenarios/5799
danpost danpost

2012/9/29

#
To accomplish this, you will need two instance variables: one boolean and one int. The boolean will track whether the powerup is active or not; the int will act as a timer. The code will need to be in the hero's class.
// instance variables
boolean dualMissleActive;
int dualMissleTimer;
// in act method
checkDualMissles();
// add new method
private void checkDualMissles()
{
    if (!dualMissleActive && whateverConditionsRequiredToActivate == true)
    { // activate powerup
        dualMissleActive = true;
        dualMissleTimer = 1000; // adjust to suit
    }
    if (dualMissleActive && dualMissleTimer > 0)
    { // deactivate powerup
        dualMissleTimer--;
        if (dualMissleTimer == 0) dualMissleActive = false;
    }
}
Once this code is set up, you will need to adjust your checkFiring code to see
if (dualMissleActive)
and fire two, instead of one, when firing.
You need to login to post a reply.