I have to display the palyers lifes in my game. every time the player hits the wrong enemy the lifes decrease.the default lifes are 5.
My code is as follows
first I declare this in my gamelayer
static int lifes=5;
CCLabel _lifes;
then a method as follows at bottom of the code
public void showLable(CCLabel _lifes){
if(_lifes != null){
this.removeChild(_lifes,true);
}
_lifes = CCLabel.makeLabel("" + lifes, "Verdana", 20);
_lifes.setColor(ccColor3B.ccbrown);
_lifes.setPosition(winSize.width/2,(winSize.height/2));
addChild(_lifes,3)
}
then I wrote this condition where the player hits the wrong enemy
lifes--;
showLable(_lifes);
1) everything works fine, the lable is displayed and label gets decreased but the label does not show up until the player hits the wrong enemy, as you see I gave default value as 5, the label shows up after the player hits wrong enemy ie, from 4.
2) Another major problem is the label displayed is displaying without removing the previous value. For ex. the lifes are 5 as default. 5 is displayed when when game starts. when the life decreases the lifes value should be 4, So here in my game the 4 is placed on 5 itself. And then the lifes are placed on the same number as 3 or 2 or 1. All the nuumbers show on each other. Now I think you understand me
thanks in advance
1) check that your are calling showLable(_lifes);
at the start of your game after its initialized, so the label show up with the value 5.
2) I'm not really sure about it but CCLabel should extends a CCSprite and thus have a setVisible method. you may try something like :
if(_lifes != null){
_lifes.setVisible(false);
this.removeChild(_lifes,true);
}
I only found the c++ api reference version : http://www.cocos2d-x.org/reference/native-cpp/d4/de7/classcocos2d_1_1_c_c_sprite.html