For example I am trying to make some SKSpriteNode, and they can only last for 10 secs. I want to create a custom property called "bornTime" for the node, so that in update() if currentTime - bornTime > 10
, the node will be removed.
You need to subclass an SKSpriteNode to a custom Object. In there you can set the properties that you wish:
import UIKit
import SpriteKit
class mySpriteNode: SKSpriteNode {
let bornTime = NSDate()
}
Then, you can compare that date with current date and see the difference.
an alternative way to do this would be to add an SKAction with an delay to the node which would remove itself from the parent node:
mynode.run(SKAction.sequence([
SKAction.wait(forDuration: 10),
SKAction.run {
mynode.removeFromParent()
}
]))
this method has the advantage that you dont have to check or even care about the time
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With