I defined SKSpriteNode under SKScene class, how can i change the SKSpriteNode image afterwards, for example:
class GameScene: SKScene {
var player = SKSpriteNode(imageNamed: "player")
}
I want to change the image from "player" to "player1" during some action. I do not inherit SKNode class, so i can not use SKTexture method to do it.
SKScene is a subclass of SKNode so you do in fact inherit it. You can use SKTexture to change the image like this:
player.texture = SKTexture(imageNamed: "player1")
Related
I'm new to Xcode 7, how do I add a black node in SpriteKit?
Can anyone point me in the right direction or link me up with any tutorials please? Thank you
You want to create a new SKSpriteNode object, and add it to the view
class GameScene: SKScene {
var node: SKSpriteNode?
override func didMoveToView(view: SKView) {
let node = SKSpriteNode(imageNamed: "BlackNode.png")
node.position.x = self.size.width / 2
node.position.y = self.size.height / 2
addChild(node)
}
}
So in your GameScene class (or whatever SKScene class you are using), this creates a SKSpriteNode that is the image of whatever "BlackNode.png" is, and sets its position to the center of the screen, and adds it to scene
Assuming your question is asking how to add an all black node to the scene, try this:
override func didMoveTo(view: SKView) {
let node = SKSpriteNode(color: UIColor.black, size: CGSize(width: 100, height: 100)) // Declare and initialize node
addChild(node) // Function that adds node to scene
}
This uses the SKSpriteNode initializer init(color: UIColor, size: CGSize), which you would pass a color for the node and size. This is then used to create a rectangle, while the nodes texture property remains nil.
Note that in this example, no position is explicitly chosen, so the default (0,0) is chosen (centre of scene if scene anchorPoint is 0.5,0.5)
To get a further understanding of scenes vs views and SpriteKit vs UIKit (which I think is where you're having trouble) check out this answer: SpriteKit vs UIKit
What is the best way to add a sprite node after a scene has been fully loaded? The sequence looks like this:
1) I build the scene, GameScene().
2) Some time later, I download backend data and I use this info to build a SKSpriteNode in a different class, NodeBuilder().
3) I want to add this node to the instance of my scene that I'd already loaded.
What's the best way to achieve step 3)?
In GameScene:
addChild(yourNode)
For getting and keeping a reference to NodeBuilder:
Creation of NodeBuilder in GameScene:
class GameScene : SKScene {
var nodeBuilder = NodeBuilder() // Create an instance of NodeBuilder
func didMoveToView(skView: SKView) {
nodeBuilder.gameScene = self // Add self as the instance of GameScene that nodeBuilder has reference to
}
}
In NodeBuilder:
class NodeBuilder {
var gameScene : GameScene! // This is how you keep your reference
func addNodeToGameScene(node: SKNode) {
self.gameScene.addChild(node)
}
addNodeToGameScene(aNode) // This is how you would call the method to add a node to GameScene from NodeBuilder
}
I have the player all set up with its action and everything in GameScene.swift. The thing is, I don't want it to be a still image so I removed
var player: SKSpriteNode = SKSpriteNode(imageNamed: "player1.png")
and I want to make it so I can use the animation SKSpriteNode I made in GameScene.sks, as the player. The SKSpriteNode in GameScene.sks is named player.
Help me out here?
You can access items created in GameScene.sks in the custom class function didMoveToView;
var player: SKSpriteNode!
override func didMove(to view: SKView) {
guard let player = childNode(withName: "player") as? SKSpriteNode else {
fatalError("player node not loaded")
}
self.player = player
}
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!
I am working on a game in Xcode with swift/spritekit.
I want to add the background from the GameViewController but I have different scenes with each different backgrounds: GameScene / MenuScene / MapScene.
Now how do I detect in gameviewcontroller which scene is running so that I can add the right background to it(as UIImage)?
And how do I set Z-position(Z-index) of an UIImage?
You'll have to track which scene is present yourself. Something like this:
class GameViewController {
var mapScene: SKScene?
var menuScene: SKScene?
var gameScene: SKScene?
var currentScene: SKScene?
func addBackground() {
if currentScene === mapScene {
// ...
} else if currentScene === menuScene {
// ...
} else if currentScene === gameScene {
// ...
}
}
}
If the UIImage is inside an SKNode, then you can set its zPosition property. If it's just a UIImage in normal UIKit elements, then it's implicit based on when it was added as a subview. If you want it on top, remove it from its parent and re-add it:
view.removeFromSuperview()
self.parentView.addSubview(view)
You don't need to detect which scene is running if you simply add the appropriate background image in the scene's implementation. Also, I recommend you avoid adding a UIImage to your view, since you will need to manually remove it when you transition to a new scene. I suggest you create a sprite node with the background image as its texture and add that to your scene with a negative value for its zPosition. Also, you should consider setting the "background" sprite's anchor point and position to CGPointZero.
For example, in 'didMoveToView'
let backgroundNode = SKSpriteNode(imageNamed:"MenuSceneBackground")
backgroundNode.position = CGPointZero
backgroundNode.anchorPoint = CGPointZero
backgroundNode.zPosition = -1000
addChild(backgroundNode)
EDIT: Add the following to remove the gesture recognizers as needed
override func willMoveFromView(view: SKView) {
view.removeGestureRecognizer(swipeGesture)
}