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

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?


1 decade ago by stahlmanDesign

I would also like to know how to access an entity from Main.js

I want to do something like"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'someName') from anywhere else. Remember: 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 ); = this;

// After that you can refer to from anywhere in your code:
var 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().

1 decade ago by stahlmanDesign

I named my player by putting this in player init: = "player";

When I put
in Main.js I get
TypeError: Result of expression 'this.getEntityByName' [undefined] is not a function.

When I put console.log('player')); I get
TypeError: Result of expression '' [null] is not an object.

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 »