How to change spriteKit SKSpriteNode texture - swift

I have a Swift 4.2 SpriteKit scene and I want to programatically change the texture of an SKSpriteNode.
How do I access the SKSpriteNode contained in the SKNode to change the texture?
Here is my code:
#IBOutlet weak var spritekitsceneFG: WKInterfaceSKScene!
var SpriteTick1 = SKNode()
SpriteTick1 = (spritekitsceneFG.scene?.childNode(withName: "SKSpriteTick1"))!
Want to change texture here!
print(SpriteTick1)
Printing out the SKNode gives the following output:
<SKSpriteNode> name:'SKSpriteTick1' texture:[<SKTexture> 'tick' (64 x 64)] position:{-48.000137, -56.000198} scale:{1.00, 1.00} size:{48, 48} anchor:{0.5, 0.5} rotation:0.00
I want to be able to programatically change the texture here, the texture is called 'tick'

Ok I found A way to do it like this. Any better ideas much welcomed.
var SpriteTick1 = SKSpriteNode()
SpriteTick1 = (spritekitsceneFG.scene?.childNode(withName: "SKSpriteTick1"))! as! SKSpriteNode
let textureCross = SKTexture(imageNamed: "cross")
SpriteTick1.texture = textureTick

Related

Assigning texture to SKSpriteNode is not working

Changing the texture of a SKSpriteNode is not working
I'm trying to learn SpriteKit and am running into something that is puzzling me. I am trying to display a sprite. I try two methods that should work, but only one is actually causing the sprite to be displayed on the screen! I would really like to actually understand why. Can anybody help?
This works:
func loadLevel() {
let point: CGPoint = CGPoint(x:300,y:300)
var block = SKSpriteNode(imageNamed: "Block_blue")
block.position = point
self.addChild(block)
}
This does not:
func loadLevel() {
let point: CGPoint = CGPoint(x:300,y:300)
var block = SKSpriteNode()
block.position = point
block.texture = SKTexture(imageNamed: "Block_blue")
self.addChild(block)
}
I would expect both functions to result in displaying the sprite, but only the first works? I would like to understand what is going on here.

Init subclass of SKSpriteNode using .sks file Xcode 8

In my GameScene.sks I have a SKSpriteNode which represents a "ball".
I need to subclass SKSpriteNode as Ball:
class Ball:SKSpriteNode {
//custom init
}
In my scene, I would like init my Ball using the SKSpriteNode in the .sks.
As I'm using Xcode 8, I tried to use the custom class in my SKSpriteNode:
self.ball = self.childNode(withName: "ball") as! Ball
But my app crashes at this line...
Also I'm not sure how to create a custom initializer for my subclass.
FYI I would prefer to avoid having something like:
class Ball:SKNode {
var sprite:SKSpriteNode!
}
let ball = Ball()
ball.sprite = self.childNode(withName: "ball") as? SKSpriteNode
I think the problem here is that you are not assigning a name to the sprite you add in the scene editor.
You could retrieve your sprite searching for type instead that searching for name
Try this
self.ball = self.children.flatMap { $0 as? Ball }.first!

Change texture of SKNode that is an SKSpriteNode

In a game I'm making I can get the node the user is touching with
var node = self.nodeAtPoint(positionInScene)
However, this gets me an SKNode (even if the node the user touches is an SKSpriteNode) and I cannot use node.texture to change its texture. How can I change that SKNode to an SKSpriteNode or change its texture? (Actions won't work here because I need to pause the scene and they won't work when the scene is paused).
Casting is what you are looking for.
For instance:
If you are sure that the casting will be successful and your SKNode() is actually a SKSpriteNode, you can do the following:
let s = SKNode()
let ss: SKSpriteNode = (s as? SKSpriteNode)!
However, it is always better to use the optional specially in casting:
let ss2: SKSpriteNode? = s as? SKSpriteNode

Unable to add SKSpriteNode to scene

I'm unable to add a SKSpriteNode to a scene in Spritekit with the folling code:
func addScoreNode(){
scoreNode = SKSpriteNode(color: SKColor.blueColor(), size: CGSizeMake(100,100))
scoreNode!.position = CGPointMake(50, 450)
addChild(scoreNode!)
}
When I create the node with a texture, it gets added to the screen.
Any help would be greatly appreciated.
Im not sure if you are just trying to display text which would be a SKLabelNode or if you are trying to apply a picture to the node But try this at the top of your scene under the class Gamescene : SKScene add
var scoreNode = SKSpriteNode()
And then instead of the func addScoreNode() put this in the didMoveToView
scoreNode.color = UIColor.blueColor()
scoreNode.size = CGSizeMake(100,100)
scoreNode.position = CGPointMake(50, 450)
scoreNode.zPosition = 5 //this brings it to the front of the screen if there are multiple things at this position
and either in the didMoveToView or where ever you want it to be called and added to screen
self.addChild(scoreNode)

Add a texture to an existing SKSpriteNode in Swift

How can I add a texture to an existing SKSpriteNode?
In the docs all I see is a way to initialize a SKNode with a texture, but not a way to add a texture to it.
// Creating with a texture is easy.
var mySprite = SKSpriteNode(texture: myTexture)
// But no method for adding a texture.
var anotherSprite = SKSpriteNode()
anotherSprite.texture = myTexture // This is not valid.
You need to update the size of the sprite.
var anotherSprite = SKSpriteNode()
anotherSprite.texture = myTexture
anotherSprite.size = myTexture.size()
It seems SKSpriteNode() initialized the size of the sprite as width:0, height:0. So you can't see the sprite in the scene.