1 decade ago
by Nennad
I would like to have an HUD in main.js where i could draw fore example, hero health.
But it is defined in hero.js, and i could not success to write the Health status from Main.js, where HUD is. i guess i am not accessing hero's entity object/property right way.
I got (undefined) at best. Please help!
BTW is it an good idea to have whole HUD in separate file/entity?
Thanks!
I would also like to know how to access an entity from Main.js
I want to do something like
ig.game.getEntityByName("player").showMessage(true);
So I could have a message on the player appear.
1 decade ago
by dominic
.getEntityByName() is actually a good way to refer to a
named entity. Inside your game class in main.js you just call
this.getEntityByName('someName')
or use
ig.game.getEntityByName('someName')
from anywhere else. Remember:
ig.game
is just an "alias" of the current instance of your game class from main.js.
Another way to do this for entities you need to refer to very frequently, is to create such a reference yourself. E.g.
// In the init() method of your player entity:
init: function( x, y, settings ) {
this.parent( x, y, settings );
ig.game.player = this;
}
// After that you can refer to ig.game.player from anywhere in your code:
var health = ig.game.player.health;
Of course this method (as well as
getEntityByName()
) only works if you just have exactly one instance of this entity class. If you have several instances of the same class, try
getEntitiesByType().
I named my player by putting this in player init:
this.name = "player";
When I put
console.log(this.getEntityByName('player'));
in Main.js I get
TypeError: Result of expression 'this.getEntityByName' [undefined] is not a function.
When I put console.log(ig.game.getEntityByName('player'));
I get
TypeError: Result of expression 'ig.game' [null] is not an object.
Even
console.log(ig.game);
outputs null
EDIT: When accessing in the update loop I am able to get to the player by using
var player = this.getEntitiesByType( EntityPlayer )[0];
I guess it wasn't working before because player did not yet exist?
Page 1 of 1
« first
« previous
next ›
last »