Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 651
Antimony Avatar asked Sep 11 '25 14:09

Antimony


2 Answers

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.

like image 161
Tokuriku Avatar answered Sep 13 '25 02:09

Tokuriku


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

like image 24
Stoneburner Avatar answered Sep 13 '25 02:09

Stoneburner