在我正在制作的游戏中,我的心底数在底部。当用户触摸“炸弹”时,想法是移除一颗心脏。现在,我在节目的开头创造了所有的心,并将每个人命名为“heart1”“heart2”和“heart3”。在doDamage方法中,当用户触摸炸弹时向hitCount添加1。然后我继续去除心脏。我现在有这个代码。有没有更好,更有效的解决方案呢? Heere是我现在正在使用的代码。
- (void) doDamage {
//Sets the isDamaged value to yes and adds a hit point
isDamaged = YES;
hitCount++;
NSLog(@"The hit count is %i ", hitCount);
//calls the damageDone method after a second to avoid being damaged all the frames the bomb touches the basket
[self performSelector:@selector(damageDone) withObject:nil afterDelay:0.5];
//This lengthy code is used to test what heart should be removed each time...
[self enumerateChildNodesWithName:@"heart1" usingBlock:^(SKNode *node, BOOL *stop){
//If the hitCount is the same as the heart we want to remove, then we'll remove it from the parent
if (hitCount == 1) {
[node removeFromParent];
}
}];
[self enumerateChildNodesWithName:@"heart2" usingBlock:^(SKNode *node, BOOL *stop){
if (hitCount == 2) {
[node removeFromParent];
}
}];
[self enumerateChildNodesWithName:@"heart3" usingBlock:^(SKNode *node, BOOL *stop){
if (hitCount == 3) {
[node removeFromParent];
}
}];
}
答案 0 :(得分:1)
我建议隐藏心脏节点而不是移除它们,因为这样你就可以随时带回它们(通电或其他东西)。
SKNode *heartNode;
switch (hitCount)
{
case 1:
heartNode = [self childNodeWithName:@"heart1"];
break;
case 2:
heartNode = [self childNodeWithName:@"heart2"];
break;
case 3:
heartNode = [self childNodeWithName:@"heart3"];
break;
}
if (heartNode)
{
[heartNode setHidden:TRUE];
}
答案 1 :(得分:0)
欣赏这已经有了一个已接受的答案,但如果代码速度很重要,这不会更快......
SKNode *heartNode = [self childNodeWithName:[NSString stringWithFormat:@"heart%d",hitCount]];
[heartNode setHidden:TRUE];
如果heartNode
为nil
,则setHidden
不执行任何操作
不确定stringWithFormat
是否比switch
声明更快