I am stuck, I need help

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.

import SpriteKit

class GameScene: SKScene {

let zombie = SKSpriteNode(imageNamed: "zombie1")

override func didMove(to view: SKView) {
    backgroundColor = SKColor.black
    let background = SKSpriteNode(imageNamed: "background1")
    background.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    background.position = CGPoint(x: size.width/2, y: size.height/2)
   
    background.zPosition = -1
    addChild(background)
    
    let mySize = background.size
    print("Size: \(mySize)")
    
    zombie.position = CGPoint(x: 400, y: 400)
    //zombie.setScale(2)
    addChild(zombie)
}

}

override func update(_currentTime: TimeInterval) {
zombie.position = CGPoint(x: zombie.position.x + 8,
y: zombie.position.y)
}

Assuming what you have posted is exactly what your class currently looks like, you need to check your curly braces.

Currently your update function is being declared outside of the GameScene class. It therefore doesn’t know about zombie or how to override update.

3 Likes

hey fusetech

I have the same issue as OP.

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)”)

    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)
    
    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)
}

}

@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) } }

Good luck

@fusetech

thanks this solved my problem.

As i understand it I had defined zombie within DidMove(to_:slight_smile: and not within the class GameScene.

so the update(_:slight_smile: 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.

Thanks again.

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. :slight_smile: Perhaps the authors could check if it needs updating in this section so new buyers don’t also get stuck