Antimony
Antimony

Reputation: 153

How to create a custom property for SKSpriteNode?

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.

Upvotes: 4

Views: 1632

Answers (2)

Tokuriku
Tokuriku

Reputation: 1352

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.

Upvotes: 5

Stoneburner
Stoneburner

Reputation: 230

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

Upvotes: 2

Related Questions