artG
artG

Reputation: 235

Objective c SpriteKit node

i want to make simple game in SpriteKit, basically what i want do do is make node what shows up on touch and disappear after like 5 sec. here are sample of code that is for board:

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;

Upvotes: 0

Views: 147

Answers (2)

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

There are many ways you can do it. One way is to call [board removeFromParent] after 5 seconds. This could be a call by NSTimer or you can use the update method which runs continuously,and check for the flag if node is added on the screen or not, and then remove the node after 5 seconds. You might need to set the flag in the touchesBegan method.

Edit:

You can also make use of SKAction waitForDuration

Sample code:

  SKAction *delay = [SKAction waitForDuration:5];
  SKAction *remove = [SKAction removeFromParent];
  SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
  [board runAction:actionSequence];

So that after the wait is done, the node will be removed.

Upvotes: 1

Knight0fDragon
Knight0fDragon

Reputation: 16827

Mr T does present 1 option, but personally I would use the SKActions sequence, waitForDuration, and removeFromParent() when dealing with nodes, like this:

let waitDuration = SKAction.waitForDuration(5)
let killAction = SKAction.removeFromParent()
let seqAction = SKAction.sequence([waitDuration,killAction])
....
board.runAction(seqAction) 

Upvotes: 3

Related Questions