Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by dmen

In an entity i defined a variable in the defines section like so:

.defines(function () {

LevelOne = ig.Game.extend({

readyToPitch: false,


Then in update I did like so:

if(this.readyToPitch){
//code in here runs... ?
}

Why does setting the variable to false in the defines section not work? I stuck it in init() like so:

this.readyToPitch = false;

and then it works as expected. I was under the assumption I could set variables in the top defines section... but is that not the case?

1 decade ago by drhayes

Yup, that's not the case.

EntityBadGuy = ig.Entity.extend({
  variable: 'catpants'
});

That variable is an instance variable of your class. You can only set it and use it from an instance of your class. When you try and do this:

EntityBadGuy = ig.Entity.extend({
  variable: 'catpants'
});

this.variable = 'doggyhat';

There&039;s no *there* there, if you know what I mean. #this has no value there since you&039;re not within an instance of your #EntityBadGuy class. init is run on an instance of your class and, thus. this.variable actually refers to the variable.

I don't know if it'll help you, but here's a link to Mozilla's documentation about objects in JS.

1 decade ago by dmen

>>
init is run on an instance of your class and, thus. this.variable actually refers to the variable.
<<

Thanks, that's much more clear now. So, if I understand correctly, should I pretty much only use init() to initialize vars?

1 decade ago by SlotGamer

No, you can initialize your variables in either place. But when you use them, you need to have an instance of that class so you can use it properly. Using "this" when you are not already inside a function of that object won't work. Using "this" inside a function of that object will work correctly. I would research usage of "this" with objects to get a better grasp of it.
Page 1 of 1
« first « previous next › last »