Swift:未解析标识符的使用
我正在尝试做一个flappy bird克隆人,我收到了这个消息。。。“使用“Ghost”的未解析标识符。”函数中出现错误。我知道这一切,所以我真的不知道发生了什么。我在遵循swift 2.1中的一个教程,所以我不确定这是否是一个问题,但我几乎可以肯定我逐行复制了它Swift:未解析标识符的使用,swift,swift2,Swift,Swift2,我正在尝试做一个flappy bird克隆人,我收到了这个消息。。。“使用“Ghost”的未解析标识符。”函数中出现错误。我知道这一切,所以我真的不知道发生了什么。我在遵循swift 2.1中的一个教程,所以我不确定这是否是一个问题,但我几乎可以肯定我逐行复制了它 import SpriteKit struct PhysicsCategory { static var Ghost : UInt32 = 0x1 << 1 static var Ground : UI
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 */
}
}
导入SpriteKit
结构物理分类{
静态变量Ghost:UInt32=0x1您的Ghost
不是一个数据类型;它是您在didMoveToView()方法中创建的单个变量的名称。您需要将其作为类的属性,以便它仍然可以在didMoveToView()
中初始化,但可以在其他方法中使用,如touchesbeated()
。只需将var Ghost
声明移到方法之外,并将Ghost=SKSpriteNode(…)
保留在它所在的位置
还应该提到的是,让局部变量或属性与enum
案例的名称相同是一个非常糟糕的主意;任何阅读代码的人都可能会感到困惑,即使就编译器而言,他们是完全合法的。将变量大写也不是很快或财产名称
最后,我不得不恳求你不要再创建一个Flappy Bird克隆!我们有足够的克隆人可以维持一辈子。你的Ghost
不是一个数据类型;它是你在didMoveToView()中创建的单个变量的名称
方法。您需要将其作为类的属性,这样它仍然可以在didMoveToView()
中初始化,但可以在其他方法中使用,如touchesbeated()
。只需将var Ghost
声明移到方法之外,并将Ghost=SKSpriteNode(…)
保留在它所在的位置即可
还应该提到的是,让局部变量或属性与enum
案例的名称相同是一个非常糟糕的主意;任何阅读代码的人都可能会感到困惑,即使就编译器而言,他们是完全合法的。将变量大写也不是很快或财产名称
最后,我不得不恳求你不要再创造一个飞禽克隆体!我们有足够的克隆体维持一辈子