How to set variable as changeable number?

Hi, I need to set a variable number that can be changed…

// what would this.speed be?? only this specific
// one works below
this.speed = ?

app.on('game:getready', function () {
this.speed = 10;
}, this);

app.on('game:playing', function () {
this.speed = 18;
}, this);

I don’t know what this would be, please help :slight_smile:

Hi @_Lio3LivioN, could explain a bit more on what you are trying to achieve?

A variable or a property, as this.speed is in your example, in JavaScript can be set to be equal to any number. That number can be set in the start of your app and stay the same, or change as a response to input or other events, or even change on each game.

You are free to do anything you like.

If you’re talking about defining this.speed then all you have to do is leave it at this.speed;.

No, you should assign an initial value. Pick a default. Presumably, the getready state is the initial state, so do:

    this.speed = 10;
1 Like

when I set an initial value, such as 10, that works perfectly. However, I want to speed this up once the playing stage has started to 18, and it does not change :confused:

Have you set a breakpoint inside your event handler function for the game:playing event to see if it’s actually being fired?

I have done this

 app.on('game:playing'); {
       this.speed = 18;
    }

I’m not sure if this is what you mean, sorry :frowning:

Sounds like you need to learn all about debugging JavaScript in Chrome Dev Tools:

Maybe you should try:

this.speed = 10;
var self = this; 

app.on('game:getready', function () {
self.speed = 10;
}, this);

app.on('game:playing', function () {
self.speed = 18;
}, this);

No, @FBplus, you don’t need to do that, because this is passed as the third parameter of the on function, so the value of this will be correct inside the event handler functions.

1 Like

Oh,I forgot this function has a scope parameter :laughing: thanks to point it out.

1 Like