I am making the zombie conga game in the book but I can’t move the zombie,. It keeps showing errors. The last three lines keeps showing errors in xcode, it is saying use of unresolved identifier ‘zombie’, and also override func can only be specified on class members.
when i declare the update function inside the GameScene class it gives me the error ‘use of unresolved identifier’ zombie’ ’
how do i fix this ?
below is all my code
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
backgroundColor = SKColor.black //set background colour to black
let background = SKSpriteNode(imageNamed: “background1”) // created sprite (background) (I)
// background.position = CGPoint(x: size.width/2, y: size.height/2) //set the position to center of scene
background.anchorPoint = CGPoint(x: 0.5, y: 0.5) // setting the anchor point for background
background.position = CGPoint(x: size.width/2, y: size.height/2) // positioning the sprite(background) (II)
// background.zRotation = CGFloat(M_PI) / 8 // rotating background(sprite). this will happen around the anchor point
background.zPosition = -1 // setting the prites(background) z position. only really necessary for the background, to ensure the background is the first thing to be drawn on the screen. (III)
addChild(background) // adding the sprite(background) to the scene.(IV)
let mySize = background.size // this and line below are to determine size of the sprite in output box below
print(“size: (mySize)”)
@muzafar It looks like you are missing the instance variable for zombie on the GameScene class.
Here is an updated copy of your source, but I would advise to double check the source from the book to understand what was missing/why it was broken.
import SpriteKit
class GameScene: SKScene {
var zombie: SKSpriteNode! //This line was missing
override func didMove(to view: SKView) {
backgroundColor = SKColor.black //set background colour to black
let background = SKSpriteNode(imageNamed: "background1") // created sprite (background) (I)
// background.position = CGPoint(x: size.width/2, y: size.height/2) //set the position to center of scene
background.anchorPoint = CGPoint(x: 0.5, y: 0.5) // setting the anchor point for background
background.position = CGPoint(x: size.width/2, y: size.height/2) // positioning the sprite(background) (II)
// background.zRotation = CGFloat(M_PI) / 8 // rotating background(sprite). this will happen around the anchor point
background.zPosition = -1 // setting the prites(background) z position. only really necessary for the background, to ensure the background is the first thing to be drawn on the screen. (III)
addChild(background) // adding the sprite(background) to the scene.(IV)
let mySize = background.size // this and line below are to determine size of the sprite in output box below
print("size: (mySize)")
let zombie = SKSpriteNode(imageNamed: "zombie1")//Created Sprite (Zombie)
zombie.position = CGPoint(x: 400, y: 400) // Positioned Sprite (Zombie)
// zombie.xScale = CGFloat(2) // scaled x axis 2 times
// zombie.yScale = CGFloat(2) // scaled y axis 2 times
addChild(zombie)
self.zombie = zombie //This line was missing
let mySizeZombie = zombie.size
print ("zombie size: \(mySizeZombie)")
}
override func update(_ currentTime: TimeInterval) {
zombie.position = CGPoint (x: zombie.position.x + 8,
y: zombie.position.y)
}
}
As i understand it I had defined zombie within DidMove(to_ and not within the class GameScene.
so the update(_ function didnt know what zombie was.
This could be made clearer in the book, but I guess it is an intermediate level which i may not be at.
Thank you - I just bought the book and was stuck on this section also. I don’t know if its because i’m using a newer version of Xcode but as a noob this almost derailed me. Perhaps the authors could check if it needs updating in this section so new buyers don’t also get stuck