0

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;
rmaddy
  • 314,917
  • 42
  • 532
  • 579
artG
  • 235
  • 3
  • 12

2 Answers2

3

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) 
Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
1

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.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • excuse me, but can you give simple example, for `[board removeFromParent]`, because iam new to this, and what i found in Internet is way to complicated for my knowledge – artG Dec 15 '15 at 15:48
  • if you want to remove a node, you should call [node removeFromParent] thats it – Teja Nandamuri Dec 15 '15 at 15:58
  • @Mr.T In SpriteKit most of the times [using NSTimer is a bad idea](http://stackoverflow.com/a/23978854/3402095) for time related actions. SKAction or update: method is a preferred way. – Whirlwind Dec 15 '15 at 18:59
  • if you are going to change your answer, at least make sure it is correct. After SKAction.waitForDuration, what happens, what do you do. What about the update method, how does that work? – Knight0fDragon Dec 15 '15 at 19:42
  • I was thinking that you provided the code sample,so there is no need to give a detailed explanation about how to use skaction in this case. Do you want me to include the similar code like you provided? :) – Teja Nandamuri Dec 15 '15 at 19:45
  • No, but when writing answers on SO, don't try to write an answer just for the poster, but for others who may also have this problem. If you are going to use another persons answer, then reference them, so that when somebody sees this post as the accepted answer, they don't shake there head and get confused by the answer. You also have another theory now that has no explanation, was this 2nd theory the answer, or was it the first? Who knows. (The actual answer is actually neither), you should put the `NSTimer` code back in, then afterwards note with an edit a better way exists, with changes – Knight0fDragon Dec 15 '15 at 19:54
  • @Knight0fDragon If `NStimer` is bad idea, what then is the good way? P.S sorry for my English – artG Dec 15 '15 at 20:42