Tests.prototype.onClick = function(event)
{
if (this.allLives.length > 1) {
/* Less life */
this.lessLife();
} else {
/* More life */
this.moreLife(event);
}
};
Tests.prototype.lessLife = function(event)
{
var allLives =this.app.root.findByName("healthMenuLives").children;
if (this.allLives.length > 1)
{
var life = allLives.slice(-1)[0].name;
this.app.root.findByName(life).enabled = false;
allLives.slice(-1)[0].destroy();
console.log(life, allLives);
} else {
alert("Ya no te quedan vidas");
/* Call end game method */
}
};
Tests.prototype.moreLife = function(event)
{
var allLives = this.app.root.findByName("healthMenuLives").children;
if (allLives.length > 0 && allLives.length < 5)
{
var newLife = allLives.slice(-1)[0].clone();
var name = parseInt(newLife.name.substr(newLife.name.length - 1), 10) + 1;
newLife.name = "health" + name.toString();
allLives.push(newLife);
console.log("health" + name.toString(), allLives);
} else {
alert("Ya no puedes tener más vida");
/* Do nothing due to you have full life */
}
It isn’t easy to debug this out of context, but the following line looks suspicious:
I think here you are removing one life and at the same time adding a new one, this way you end up with a single life again?
What happens if you put it like this:
var newLife = allLive[0].clone();
And then the thing I think it’s missing here, instead of manually adding it to the children area, use the formal method of adding it to the hierarchy. That will trigger the layout group update:
// remove this line
// allLives.push(newLife);
// replace it with this
this.app.root.findByName('healthMenuLives').addChild(newLife);
Right, so you usually don’t want to rely on the order of the children. The children might also contain a graph node, which is not what you want to clone. The fix you showed is a preferred way.
Also, don’t forget that an entity is a simple Object, and as such you can add your own properties to it. For example, instead of parsing the strings to integers, you can do something like:
var currentLives = 4;
var lifeIndicator = new Entity();
lifeIndicator.livesCount = currentLives;
lifeIndicator.name = currentLives.toString();
// at some point later
lifeIndicator.livesCount--;
lifeIndicator.name = 'lives: ' + lifeIndicator.livesCount.toString();
// "lives: 3"