2016-02-19 119 views
0

我的遊戲應用程序有隨機移動的對象。我想停止並在觸摸/捕捉時在屏幕上打印一些文字。但我不知道我能在gamescene.swift頁面中做什麼。我應該在touchesBegan寫什麼?這裏是我的代碼:當我觸碰物體時,如何停止移動物體?

import SpriteKit 

let BallCategoryName = "ball" 

class GameScene: SKScene { 
    override func didMoveToView(view: SKView) { 
     super.didMoveToView(view) 

     let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame) 


     borderBody.friction = 0 


     self.physicsBody = borderBody 


     physicsWorld.gravity = CGVectorMake(15, 15) 

     let ball = childNodeWithName(BallCategoryName) as! SKSpriteNode 
     ball.physicsBody!.applyImpulse(CGVectorMake(10, -10)) 

} 
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

     let touch = touches.first as! UITouch 


     } 

} 

回答

0

首先,你需要把變量球didMoveToView外:

class GameScene: SKScene { 

    let ball = childNodeWithName(BallCategoryName) as! SKSpriteNode 

    override func didMoveToView(view: SKView) { 
     [...] 
    } 
} 

的touchesBegan:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch:AnyObject in touches{ 
     let location = (touch as! UITouch).locationInNode(self) 
     let nodeTouched = nodeAtPoint(location) 

     //Detect if the ball is touched 
     if(nodeTouched == ball){ 
      //Do something 
     } 
    } 
} 
+0

當我把變量球didMoveToView外,還有一個問題。問題說'''不能將'String'類型的值轉換爲期望的參數類型'SKNode'。 「」 – uzaylibeyb