I just started working on SpriteKit, today.
I tried to paste the above function into my GameScene file and I get an error.
The error states that “override can only be specified on class members”
“Fix it - Delete override”
So, what am I doing wrong?
Here’s my code:
import SpriteKit
import GameplayKit
class GameScene: SKScene
{
override func didMove(to view: SKView)
{
backgroundColor = SKColor.black
let background = SKSpriteNode(imageNamed: “background1”)
// background.position = CGPoint(x: size.width/2, y: size.height/2)
// background.anchorPoint = CGPoint.zero
// background.position = CGPoint.zero
background.anchorPoint = CGPoint(x: 0.5, y: 0.5) // default
background.position = CGPoint(x: size.width/2, y: size.height/2)
// background.zRotation = CGFloat(M_PI) / 8
addChild(background)
let mySize = background.size
print(“Size: (mySize)”)
background.zPosition = -1
let zombie = SKSpriteNode(imageNamed:"zombie1")
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)
}
}
}
Thanks,
Gary
Never mind!
I figured out my own error. I had defined the constant variable
"let zombie = SKSpriteNode(imageNamed:“zombie1”)
locally in the previous function.
I’ve now made it a global constant and all is working great again.
Gary
1 Like
Hey elearner,
I am getting the same errors as you were. Can you tell me how you fixed it ?
I dont know how to define ‘zombie1’ as a global constant.
also the book suggests adding the below code inside GameScene.Swift, which i took to mean anywhere in the file (and not inside the DidMove(to:) as you have done above.
override func update(_ currentTime: TimeInterval) {
zombie.position = CGPoint (x: zombie.position.x + 8,
y: zombie.position.y)
}
Thanks in advance.
Hello muzafar,
Here’s my code that shows where to declare the “zombie1” constant.
It’s in bold so you can see where it’s declared.
import SpriteKit
import GameplayKit
class GameScene: SKScene
{
var lastTouchLocation: CGPoint?
let playableRect: CGRect
var lastUpdateTime: TimeInterval = 0
var dt: TimeInterval = 0
let zombieRotateRadiansPerSec:CGFloat = 4.0 * π
let zombie = SKSpriteNode(imageNamed: “zombie1”) //declared above your first function
let zombieMovePointsPerSec: CGFloat = 480.0
var velocity = CGPoint.zero
override func didMove(to view: SKView)
{
debugDrawPlayableArea()
let background = SKSpriteNode(imageNamed: “background1”)
background.position = CGPoint(x: size.width/2, y: size.height/2)
background.anchorPoint = CGPoint(x: 0.5, y: 0.5) //default
background.zPosition = -1
addChild(background)
zombie.position = CGPoint(x:400,y:400)
// zombie.anchorPoint = CGPoint(x: 0.0, y: 0.0) //default
zombie.zPosition = 100
addChild(zombie) //this is where you load the zombie image
}
Let me know if that helped.
Gary