Reputation: 1
I'm trying to make a flappy bird clone and I am getting this message... "use of unresolved identifier for 'Ghost'." The error is occuring in the touches began function. I'm knew to all this so I don't really know what's going. I was following a tutorial coded in swift 2.1 so I'm not sure if that may be a problem, but I'm almost sure I copied it line for line.
import SpriteKit
struct PhysicsCategory {
static var Ghost : UInt32 = 0x1 << 1
static var Ground : UInt32 = 0x1 << 2
static var Wall : UInt32 = 0x1 << 3
}
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
var Ground = SKSpriteNode()
var Ghost = SKSpriteNode()
Ground = SKSpriteNode(imageNamed: "Ground")
Ground.setScale(0.5)
Ground.position = CGPoint(x: self.frame.width/2, y:0 + Ground.frame.height/2)
Ground.physicsBody = SKPhysicsBody(rectangleOfSize: Ground.size)
Ground.physicsBody?.categoryBitMask = PhysicsCategory.Ground
Ground.physicsBody?.collisionBitMask = PhysicsCategory.Ghost
Ground.physicsBody?.contactTestBitMask = PhysicsCategory.Ghost
Ground.physicsBody?.affectedByGravity = false
Ground.physicsBody?.dynamic = false
self.addChild(Ground)
Ghost = SKSpriteNode(imageNamed: "Ghost")
Ghost.size = CGSize(width:60, height: 70)
Ghost.position = CGPoint(x: self.frame.width/2 - Ghost.frame.width, y: self.frame.height/2)
Ghost.physicsBody = SKPhysicsBody(circleOfRadius: Ghost.frame.height/2)
Ghost.physicsBody?.categoryBitMask = PhysicsCategory.Ghost
Ghost.physicsBody?.collisionBitMask = PhysicsCategory.Ground | PhysicsCategory.Wall
Ghost.physicsBody?.contactTestBitMask = PhysicsCategory.Ground | PhysicsCategory.Wall
Ghost.physicsBody?.affectedByGravity = true
Ghost.physicsBody?.dynamic = true
self.addChild(Ghost)
createWalls()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
Ghost.physicsBody?.velocity = CGVectorMake(0,0)
Ghost.physicsBody?.applyImpulse(CGVectorMake(0, 60))
}
func createWalls() {
let wallPair = SKNode()
let topWall = SKSpriteNode(imageNamed: "Wall")
let bottomWall = SKSpriteNode(imageNamed: "Wall")
topWall.position = CGPoint(x: self.frame.width/2, y:self.frame.height/2 + 350)
bottomWall.position = CGPoint(x: self.frame.width/2, y:self.frame.height/2 - 350)
topWall.setScale(0.5)
bottomWall.setScale(0.5)
topWall.zRotation = CGFloat(M_PI)
wallPair.addChild(topWall)
wallPair.addChild(bottomWall)
self.addChild(wallPair)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
Upvotes: 0
Views: 697
Reputation: 13893
Your Ghost
is not a data type; it's the name of a single variable that you create in your didMoveToView()
method. You need to make it a property of your class so that it can still be initialized in didMoveToView()
, but used in other methods, like touchesBegan()
. Just move your var Ghost
declaration outside of the method, and keep the Ghost = SKSpriteNode(...)
where it is.
It should also be mentioned that it's a really bad idea to have a local variable or a property have the same name as one of your enum
cases; anybody who reads the code could get understandably confused, even though they're perfectly legal as far as the compiler is concerned. It's also not very Swift-like to capitalize your variable or property names.
And lastly, I have to implore you not to create another Flappy Bird clone! We have enough of those to last a lifetime.
Upvotes: 2