I have a question in game development. I am building a game for iPhone. At the game play, there is a damage bar, that displays what the player has damage the enemies. The technique I use is using 2 pictures. One is a damage bar which is fill with full damage, and one is jus a black bar but same size with the damage bar. It is like masking. I will scale the black bar shorter whenever it receives the value of the damage. But the problem here is the method i use to calculate the damage is different class from the method i use to draw the black bar. How do I draw it frequently everytime it gets a damage value? please help me , thank you开发者_JS百科 so much.
-(void)damageTaken
{
totalDamage = 0;
for (uint i = 0 ; i< [bodyParts count]; i++) {
RagdollBodypart *part = (RagdollBodypart *)[bodyParts objectAtIndex:i];
totalDamage += part.damage;
}
[[NSNotificationCenter defaultCenter] postNotificationName:DAMAGE_TAKEN object:nil];
}
above is the method I used to calculate the damage. It will get called everytime the player hits something. It is created in a class called BodyDamage
This is the code i use to draw the mask for the damage bar. it just runs once, how can I run this with the number of time same as the damageTaken?
float damage = [[GameController instance].engine.enemies totalDamage];
float scale = (float)(1400 -damage)/(float)1400;
[damageSprMask setScaleY:scale];
//[damageSprMask setPosition:ccp(20,150)];
[damageSprMask setPosition:ccp(20,150 +((1- damageSprMask.scaleY)*damageSpr.contentSize.height)/2)];
To cause a view to redraw itself you can use [view setNeedsDisplay] this will trigger the drawRect method for the view.
So you can either call directly in damageTaken or in the method that listens for the notification you're sending.
Thanks for ur reply. I solved the problem now. What I did was just create another tick function. I will call that function 60 times in a second same as my framerate then draw the mask in there. My damage bar looks fine now. But thank you too, I think your way works as swell.
精彩评论