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
}
Related
I have an ARKit app that uses SpriteKit to display SKNodes that correspond to ARAnchors in the AR session. I am using the ARSKViewDelegate method
func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode?
to display the nodes. This works for nodes I create and return in that method, but I have some more complicated nodes that I would like to design in an .sks file and then display for some anchors. When I return a node instantiated from an .sks file in that method the app crashes with an error saying the node is already in the scene (if the node is in the .sks file for the current scene), or already has a parent (if it is in an .sks file for a different scene).
How can I display sprites in ARKit that are drawn in an .sks file?
Try this approach:
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "mySpriteKitScene") {
guard let clone = scene.childNode(withName: "myRedSprite")?
.copy() as? SKSpriteNode
else { return }
clone.name = "mySecondRedSprite"
clone.position.x += 250
clone.physicsBody = SKPhysicsBody()
scene.addChild(clone)
view.presentScene(scene)
}
}
I am making a zombie apocalypse-style cheese game and I am having trouble knowing where/how to declare zombies. I thought that since I had them made in my Gamescene.sks file with each the name 'zombie,' then they would be declared like that. I am trying to make the zombies(which I made 4 in the .sks file) chase the player(which I also set up the .sks file and coded in the upper lines of code.)
override func didMove(to view: SKView) {
let player = self.childNode(withName: "player") as? SKSpriteNode
for child in self.children{
if child.name == "zombie"{
if let child = child as? SKSpriteNode {
zombies.append(child)
}
}
}
}
After a while of trying, I found out that my .sks and .swift files were not communicating and I had to set up the view.
if let view = self.view as! SKView?
if let scene = GameScene(fileNamed: "GameScene")
scene.scaleMode = .resizeFill
So I have a "Ball" (player) that you control, when the player hits a "colorOrb" the Ball changes to that colour all at random, so that part works, but I'm stuck on how to detect if the player changes to a random colour on how to detect if once it goes through a wall also of a random colour if they are both the same colour and add +1 or whatever to score.
I've tried multiple ways but can't seem to figure it out. thanks in advance :)
Given a Ball and a Wall class
class Ball: SKSpriteNode { }
class Wall: SKSpriteNode { }
In your didBegin(contact:) just compare the color property.
class GameScene: SKScene, SKPhysicsContactDelegate {
func didBegin(_ contact: SKPhysicsContact) {
let nodeA = contact.bodyA.node
let nodeB = contact.bodyB.node
guard let nodes: (ball:Ball, wall:Wall) = (nodeA, nodeB) as? (Ball, Wall) ?? (nodeB, nodeA) as? (Ball, Wall) else { return }
if nodes.ball.color == nodes.wall.color {
// same color
// TODO...
}
}
}
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 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")