【问题标题】:Swift : use of unresolved identifierSwift:使用未解析的标识符
【发布时间】:2016-05-29 12:33:51
【问题描述】:

我正在尝试制作一个飞扬的鸟克隆,我收到了这条消息……“使用未解析的标识符来表示‘幽灵’。”错误发生在触摸开始功能中。这一切我都知道,所以我真的不知道发生了什么。我正在学习用 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 */
}
}

【问题讨论】:

    标签: swift swift2


    【解决方案1】:

    您的Ghost 不是数据类型;它是您在 didMoveToView() 方法中创建的单个变量的名称。您需要将其设置为您的类的属性,以便它仍然可以在didMoveToView() 中初始化,但可以在其他方法中使用,例如touchesBegan()。只需将您的 var Ghost 声明移到方法之外,并将 Ghost = SKSpriteNode(...) 保留在原处即可。

    还应该提到的是,让局部变量或属性与您的enum 案例之一同名是一个非常糟糕的主意;任何阅读代码的人都会感到困惑,尽管就编译器而言它们是完全合法的。将变量或属性名大写也不太像 Swift。

    最后,我必须恳求您不要创建另一个 Flappy Bird 克隆!我们有足够的时间来维持一生。

    【讨论】:

      猜你喜欢
      • 2016-04-29
      • 2015-05-13
      • 2016-11-28
      • 2015-05-25
      • 2017-01-28
      • 2015-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多