I try to extend the ProgressTimer, but it doesn’t work.
var HealthBar = cc.ProgressTimer.extend({
ctor: function(baseLife){
this.bar1 = cc.Sprite.create(asset.health_bar1);
this.bar2 = cc.Sprite.create(asset.health_bar2);
this.bar3 = cc.Sprite.create(asset.health_bar3);
this._super(this.bar1);
this.setPercentage(100);
this.currentLife = baseLife;
this.baseLife = baseLife;
this.setType(cc.ProgressTimer.TYPE_BAR);
this.setBarChangeRate(cc.p(1,0));
this.setMidpoint(cc.p(0,0));
this.scheduleUpdate();
},
baseLife: 0,
currentLife: 0,
bar1: null,
bar2: null,
bar3: null,
update: function(){
if(this.percentage < 25){
this.setSprite(this.bar3);
}else if(this.percentage < 55){
this.setSprite(this.bar2);
}
},
takeDamage: function(attack){
this.currentLife-=attack;
var persen = 0;
if(this.currentLife > 0){
var persen = this.currentLife/this.baseLife * 100;
}
var action = cc.ProgressTo.create(1.5, persen);
this.runAction(action);
},
});
And this code in layer :
var healthBar = new HealthBar(400);
healthBar.setPosition(cc.winSize.width/2, cc.winSize.height/2);
healthBar.setVisible(true);
this.addChild(healthBar, 2);
What’s wrong with my code? Thank you.