2016-05-29 10 views
0

私はフラッフィーな鳥クローンを作ろうとしていますが、私はこのメッセージを受け取っています... "ゴースト"の未解決の識別子の使用 "タッチ開始機能でエラーが発生しています。私はすべてのことを知っているので、何が起こっているのか分かりません。私はスイフト2.1でコーディングされたチュートリアルに従っていたので、それが問題であるかどうかはわかりませんが、私はそれをライン用にコピーしたと確信しています。Swift:未解決の識別子を使用

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 */ 
} 
} 

答えて

2

Ghostはデータ型ではありません。これは、didMoveToView()メソッドで作成する1つの変数の名前です。あなたはそれがまだdidMoveToView()で初期化されるが、touchesBegan()のような他のメソッドで使用されるように、クラスのプロパティにする必要があります。 var Ghost宣言をメソッド外に移動し、Ghost = SKSpriteNode(...)の位置に置いてください。

また、ローカル変数またはプロパティにenumケースの1つと同じ名前を付けることは本当に悪い考えです。コンパイラに関する限り完全に合法であっても、コードを読んだ人は分かりやすく混乱する可能性があります。また、変数名やプロパティ名を大文字にするのはあまり速くない。

最後に、別のFlappy Birdクローンを作成するために、ではなく、をお勧めします。私たちは生涯続くものが十分あります。

関連する問題