Swift Spritekit Set Userdata

Swift spritekit set userdata

The first argument in a method does not have an external parameter name,
therefore it should be

p.userData?.setValue("Hello", forKey: "c")

or better (since userData is a NSMutableDictionary and no Key-Value Coding
magic is required here):

p.userData?.setObject("Hello", forKey: "c")

Note also (as just mentioned in a comment) that you have to create the dictionary
first:

p.userData = NSMutableDictionary()
p.userData?.setObject("Hello", forKey: "c")

or alternatively, assign a dictionary with your keys and values:

p.userData = ["c" : "Hello"]

get userData of SKSpriteNode using the scene editor

Probably also your sprite is nil:

try to search your sprite with the optional binding (if let) and the "//" before sprite.

if let sprite = self.childNode(withName: "//sprite") as? SKSpriteNode {
let numero:Int = sprite.userdata?.valueForKey("numero") as! Int
} else {
print("I cannot find sprite..")
}

It performs a recursive search from its current position.
Check the API reference here.

SpriteKit - TileDefinition and userData being update for all tiles?

The userData that you add in the sks file is applied to every tile of that type used on the map, not per instance of the tile e.g. if you have a mountain tile with userData mountain:true then each mountain tile you place on the map will return true. If you update the userData to false, then all mountain tiles would be false.

Since each tile is uniquely identified by column and row index, you could create a data structure like an array of tuples to store timer info;

var timerInfo:[(column: Int, row: Int, timer: Double)] = []

Saving and Reading information from a Sprites UserData

I believe your issue is that the userData starts out as nil. Try this...

func createSprite() {
let sprite = SKSpriteNode()
//init NSMutableDictionary
sprite.userData = NSMutableDictionary()
sprite.userData?.setValue("100", forKeyPath: "key")
frame.addChild(sprite)
}

Hopefully that helps.

Comparing SKNode's userData gives an error

userData is an NSMutableDictionary. This worked for me:

if hero.userData!.objectForKey("isEvil") as Bool == enemy.userData!.objectForKey("isEvil") as Bool

EDIT: As pointed out by @DonnyP, it can be written even simpler:

if hero.userData!["isEvil"] as Bool == enemy.userData!["isEvil"] as Bool

Swift Spritekit setvalue

setValue(_:forKey:) and related methods aren't for associating arbitrary key/value data with an object. They're part of Key-Value Coding, a Cocoa feature that does all sorts of handy things — but unless a class does something special with KVC, all setValue(_:forKey:) does is let you set one of that class's declared properties using a string instead of by directly accessing a property. In other words, the two lines below are equivalent:

node.name = "foo"
node.setValue("foo", forKey: "name")

If you want to associate arbitrary key-value pairs with your sprites—which can be a great way to keep track of entities in your game—use the userData property.



Related Topics



Leave a reply



Submit