我想在SpriteKit中制作简单的游戏,基本上我想做的是让节点触摸后显示并在5秒后消失。以下是适用于董事会的代码示例:
SKSpriteNode* board = [[SkSpriteNode alloc] initWithImageNamed:@"board"];
board.name = boardCategoryName;
board.position = CGPointMake (CGRectGetMaxX(self.frame),board.frame.seize.height *4.6f);
[self.addChild:board];
board.physicsBody = [SKphysicsBody bodyWithRectangleOFSize:board.frame.size];
board.physicsBody.restitution =0.1f;
board.physicsBody.friction = 0.4f;
board.physicsBody.dynamic = No;
答案 0 :(得分:3)
T先生确实提出了1个选项,但我个人在处理节点时会使用SKAction
s sequence
,waitForDuration
和removeFromParent()
,如下所示:
let waitDuration = SKAction.waitForDuration(5)
let killAction = SKAction.removeFromParent()
let seqAction = SKAction.sequence([waitDuration,killAction])
....
board.runAction(seqAction)
答案 1 :(得分:1)
有很多方法可以做到。一种方法是在5秒后调用[board removeFromParent]
。这可以是NSTimer
的调用,也可以使用连续运行的update
方法,如果节点是否添加到屏幕上则检查标志,然后在5秒后删除节点。您可能需要在touchesBegan
方法中设置标记。
修改强>
您还可以使用SKAction
waitForDuration
示例代码:
SKAction *delay = [SKAction waitForDuration:5];
SKAction *remove = [SKAction removeFromParent];
SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
[board runAction:actionSequence];
因此,等待完成后,节点将被删除。