Objective c SpriteKit节点

时间:2015-12-15 15:03:01

标签: ios objective-c sprite-kit

我想在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;

2 个答案:

答案 0 :(得分:3)

T先生确实提出了1个选项,但我个人在处理节点时会使用SKAction s sequencewaitForDurationremoveFromParent(),如下所示:

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];

因此,等待完成后,节点将被删除。