2016-02-12 22 views
0

嗨,我遵循我的Xcode遊戲教程。當我運行模擬器時,它會成功啓動,但只要點擊下一頁就會崩潰。然後錯誤的 GameScene.swift彈出右鍵上述xcode sprite遊戲錯誤EXC_BAD_INSTRUCKION(code = EXC_1386,subcode = 0x0),啓動模擬器後

override func didMoveToView(view: SKView) { 
     /* Setup your scene here */ 
     cannon = self.childNodeWithName("cannon") as! SKSpriteNode 

是我有一個錯誤的部分。從

cannon = self.childNodeWithName("cannon") as! SKSpriteNode 

和錯誤這樣表示

thread1:EXC_BAD_INSTRUCKION(code=EXC_1386, subcode=0x0) 

這是我的代碼

進口SpriteKit

class GameScene: SKScene { 
    var cannon: SKSpriteNode! 
    var touchLocation:CGPoint = CGPointZero 


    **override func didMoveToView(view: SKView) { 
     /* Setup your scene here */ 
     cannon = self.childNodeWithName("cannon") as! SKSpriteNode** 

    } 


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     /* Called when a touch begins */ 
     touchLocation = touches.first!.locationInNode(self) 
    } 

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     touchLocation = touches.first!.locationInNode(self) 

    } 

    override func update(currentTime: CFTimeInterval) { 
     /* Called before each frame is rendered */ 
     let percent = touchLocation.x/size.width 
     let newAngle = percent * 180 - 180 
     cannon.zRotation = CGFloat(newAngle) * CGFloat(M_PI); 180.0 

請讓我知道什麼是錯在這裏。非常沮喪。謝謝

+0

您是否嘗試將cannon = self.childNodeWithName(「cannon」)註釋掉! SKSpriteNode ** – helloGo

回答

0

我剛剛在一個新項目中測試了你的代碼,並且它在childNodeWithName正確的情況下工作。我認爲你已經在GameScene.sks中創建了大炮,現在正在嘗試在代碼中引用它。

如果.sks文件中的精靈名爲「cannon」,它可以正常工作,但是如果我將其更改爲其他東西,我也會崩潰。

總之,您的大炮精靈名稱不是「加農炮」,因此必須在某處出現拼寫錯誤。

0

不知道你是否已經解決了它,但我正在看同一個教程,發現同樣的問題。

問題在於cannon節點是如何管理到cannon_full節點的。 self.childNodeWithName("cannon")不會查看嵌套對象。

要解決此問題,請將cannon精靈節點代入SKScene或使用self.childNodeWithName("cannon_full")?.self.childNodeWithName("cannon")找到它,其中cannon_full是父級名稱。兩種方法都是有效的方法,但第二種方法會在這個特定情況下引起定位問題。

相關問題