[英]Swift : use of unresolved identifier
我正在嘗試制作一個飛揚的小鳥克隆,並且收到此消息……“對'Ghost'使用未解析的標識符”。 觸摸開始功能中發生錯誤。 我對這一切都知道,所以我真的不知道怎么回事。 我正在按照swift 2.1編碼的教程進行操作,因此不確定是否可能會出現問題,但是我幾乎可以肯定我逐行復制了它。
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 */
}
}
您的Ghost
不是數據類型; 它是您在didMoveToView()
方法中創建的單個變量的名稱。 您需要將其設置為類的屬性,以便仍可以在didMoveToView()
中didMoveToView()
進行初始化,但可以在其他方法中使用它,例如touchesBegan()
。 只需將var Ghost
聲明移到該方法之外,然后將Ghost = SKSpriteNode(...)
保留在該位置即可。
還應該提到的是,讓局部變量或屬性與您的enum
實例之一具有相同的名稱是一個非常糟糕的主意。 任何閱讀代碼的人都可能會感到困惑,即使就編譯器而言,它們是完全合法的。 用大寫的變量名或屬性名也不太像Swift。
最后,我必須懇求您不要創建另一個Flappy Bird克隆! 我們有足夠的這些可以持續一生。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.