How to Get Skspritenode Name

How to get SKSpriteNode name?

You are setting the name of the variable, but you are not setting the name of the SKSpriteNode. It should be:

 sprite1.name = @"sprite1";

Follow suit for the rest of your sprites.

Another thing I noticed is that you call self.name. I am pretty sure self refers to the scene, if that is where this code is located. Instead, you should save the selected as an SKSpriteNode and call selected.name.

Getting the filename of an SKSpriteNode in Swift 3

It is stored in the description, so here is a nice little extension I made to rip it out.

extension SKTexture
{
var name : String
{
return self.description.slice(start: "'",to: "'")!
}
}

extension String {
func slice(start: String, to: String) -> String?
{

return (range(of: start)?.upperBound).flatMap
{
sInd in
(range(of: to, range: sInd..<endIndex)?.lowerBound).map
{
eInd in
substring(with:sInd..<eInd)

}
}
}
}
usage:

print(sprite.texture!.name)

Setting a name to an SKSpriteNode?

There may be a better way to do this, but this is the first thing I thought of - make the playerTexture variable an array of tuples containing the texture and the name you want to give it:

 var playerTexture = [(SKTexture, String)]()

playerTexture.append((SKTexture(imageNamed: “red”), "red"))
playerTexture.append((SKTexture(imageNamed: “blue”), "blue"))
playerTexture.append((SKTexture(imageNamed: “green”), "green"))

let rand = Int(arc4random_uniform(UInt32(playerTexture.count)))
let chosenColor = playerTexture[rand].0 as SKTexture
let colorName = playerTexture[rand].1
let playerSkin = chosenColor

player = SKSpriteNode(texture: playerSkin)
player.size = CGSize(width: 50, height: 50)
player.position = location
self.addChild(player)
player.name = colorName

How to get image name from SKSpriteNode?

You don't have direct access to the name of the image. A possible solution is to use the name property of the node for it.

NSString *filename = @"img.png";
SKSpriteNode *button = [SKSpriteNode spriteNodeWithImageNamed:filename];
[button setName:filename];

That way you'd have name by simply printing the description of the object.

NSLog("Touched object %@", [node description]);

Get texture name from a Node - Swift/Spritekit

You probably only have the node as an SKNode, which doesn't have the texture property. You should be able to use an optional binding to cast to SKSpriteNode, like this:

if let spriteNode = node as? SKSpriteNode {
println(spriteNode.texture)
}

SKSpriteNode name to int

You are using Int type construtor Int?(String) which means it needs a String and returns an optional Int (Int?).

node.name is an optional property of SKNode, because nodes can have no name. So you have been suggested to force dereferencing (but this is very dangerous). You can provide a default value as node.name ?? <default> or use the if-let:

if let name = node.name {
// do something with name in the case it exists!
}

Then you tried to store the Int? produced in a variable of type Int, and the IDE suggested you to force dereferencing it (again a bad thing). Again you can provide a default value let i : Int = Int(node.name ?? "0") ?? 0 or use the if-let pattern:

if let name = node.name, let i = Int(name) {
// do want you want with i and name...
} else {
// something bad happened
}

SKTexture get image name

Hmm my strategy would be this:

On your ninja class have a property for your textures. Keep them around

@property (nonatomic, strong) NSArray * hitTextures;

Then store the hit textures (note this is a lazy loading getter method)

-(NSArray *)hitTextures{
if (_hitTextures == nil){
SKTexture *hit1 = [SKTexture textureWithImageNamed:@"Ninja_hit_1"];
SKTexture *hit2 = [SKTexture textureWithImageNamed:@"Ninja_hit_2"];
SKTexture *hit3 = [SKTexture textureWithImageNamed:@"Ninja_hit_3"];
SKTexture *hit4 = [SKTexture textureWithImageNamed:@"Ninja_hit_4"];

_hitTextures = @[hit1, hit2, hit3, hit4];
}
return _hitTextures;
}

Note that we don't need to make the SKTextureAtlas object explicitly:

When loading the texture data, Sprite Kit searches the app bundle for an image file with the specified filename. If a matching image file cannot be found, Sprite Kit searches for the texture in any texture atlases stored in the app bundle. If the specified image does not exist anywhere in the bundle, Sprite Kit creates a placeholder texture image.

Use this texture array to fill in your SKAction

    SKAction *hitAnimation = [SKAction animateWithTextures:self.hitTextures
timePerFrame:0.1];

This allows you do change your -isHit method like so:

- (BOOL)isHit:(SKTexture *)texture
{
NSUInteger index = [self.hitTextures indexOfObject:texture];

if (index == 1 ||
index == 2)
{
return YES;
}

return NO;
}

This way you don't rely on an implementation detail of the -description method that is subject to change.

How do you reference multiple sprites with the same name in sprite kit?

You want to use enumerateChildNodes, this cycles through all nodes and any nodes whose name matches the withName part, the containing code is run.
Any node we do find while searching, we just assign it to a constant called groundNode (can be any name), and make it an SKSpriteNode. From there, you can do anything SKSpriteNode related, in your case we run a moveBy action on it.

  enumerateChildNodes(withName: "ground") {
(node, _) in
let groundNode = node as! SKSpriteNode
let move = SKAction.moveBy(x: 1000, y: 1000, duration: 10)
groundNode.run(move)
}

SpriteKit: find all descendants of SKNode of certain class?

What we did was pass a block to the SKNode function that finds nodes by name, and used * as the search term to avoid assigning a name to desired nodes.

    var descendants = [CustomClass]()
nodeToSearch.enumerateChildNodes(withName: ".//*") { node, stop in
if node is CustomClass {
descendants.append(node as! CustomClass)
}
}


Related Topics



Leave a reply



Submit