Notify the player when something changes


(Zouhair Elamrani Abou Elassad) #1

I’m trying to add a shooting bonus to my game, when the player takes the bonus he will be able to fire missiles, what i did is that a created global variable :

public static shootActivated = false ;

once the player collides with the bonus, i set shootActivated to true and i remove the bonus. in tha player update function, i’m testing all time the value of shootActivated, if it’s true then when pressing SPACE bar the player shoots missiles.

i’m thinking that maybe there is a better way to implement this, may be i don’t have to test all the time the value of shootActivated in the player update function, is there a way that only when the player collides with the bonus then he get notified about being able to shoot missiles.


(Martí Angelats i Ribera) #2

To start with: stop using static variable for everything, and when use it be really carefull. This usually makes the code way more complicated becouse of the changes in the worlds and entities it creates tones of problems.

In this case this should be a player variable (private). Probably a timer that counts down the time left.

override public function update():void
{
    if (shootTimer > 0)
    {
        shootTimer -= FP.elapsed;
    }

    if (collide("powerup", x, y))
    {
        shootTimer = 8; //the time in seconds
    }
}

and then to check if the power-up is still up you simply use:

shootTimer > 0

PS: That static variables.

PPS: Realy i don’t understand that uses of the static variables.

PPPS: Static variables suck. :wink:

Edit: change the code a bit… What was i thinking? :confused:


(Zouhair Elamrani Abou Elassad) #3

Oh lord, you scared me about the use of static variables, but i do see what you mean, god welling i’ll try to avoid them in the future, and for the notification, there is no way to implement that ?


(Martí Angelats i Ribera) #4

What do you mean? Sometimes you don’t have other option that use static variables but they usually cause problems. That’s why they suck XD (but sometimes they are necessary).


(Zachary Lewis) #5

I’d implement this at the World level.


(Martí Angelats i Ribera) #6

Why would you do that? I mean, if for some reason you want more than a single player then you’ll have to re-do it. This way it doesn’t matter, every player have its own timer.


(Zouhair Elamrani Abou Elassad) #7

Thanks guys, sorry for taking me so long to answer, i’ve been sick.


(Martí Angelats i Ribera) #8

No problem. Everyone can be sick… you aren’t an alien (I hope).


(Zouhair Elamrani Abou Elassad) #9

ooh surely i’m not :smile:, Thank You