2015-12-25 9 views
5

私はxcodeプロジェクトを使いこなしています。私はそれを触ったときに赤いボールSKShapeNodeを動かそうとしています。私の自身のソースコードは、タッチを開始したクラスはトリックをやっていません。私のコードの欠陥とそれを修正するために何をする必要がありますか?スウィフトスプライトキットタッチ機能開始

import SpriteKit 

class GameScene: SKScene { 

    //Rectangle Skeletions 
    var leftWallRect = CGRect(x: 0, y: 0, width: 40, height: 1000) 

    var ball = SKShapeNode(circleOfRadius: 25); 


    override func didMoveToView(view: SKView) { 

     // Declare the Sprites in your scene 
     var ball = SKShapeNode(circleOfRadius: 25); 
     let leftWall = SKShapeNode(rect: leftWallRect); 
     let rightWall = SKShapeNode(rect: leftWallRect); 
     let pin = SKSpriteNode(imageNamed: "pin"); 

     // Ball Properties 
     ball.strokeColor = UIColor.blackColor() 
     ball.fillColor = UIColor.redColor() 
     ball.position = CGPointMake(self.frame.width/2 , self.frame.height/4 - 100) 
     self.addChild(ball) 

     // Left Wall Properties 
     leftWall.fillColor = UIColor.purpleColor() 
     leftWall.strokeColor = UIColor.blackColor() 
     leftWall.position = CGPointMake(self.frame.size.width/3 - 45, self.frame.size.height/2 - 400) 
     self.addChild(leftWall) 

     //Right Wall Properties 
     rightWall.fillColor = UIColor.purpleColor() 
     rightWall.strokeColor = UIColor.blackColor() 
     rightWall.position = CGPointMake(self.frame.size.width/2 + 175, self.frame.size.height/2 - 400) 
     self.addChild(rightWall) 
    } 

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
     /* Called when a touch begins */ 

     let touch = touches.first as! UITouch 
     let touchLocation = touch.locationInNode(self) 

     if touchLocation == ball.position{ 

      ball.position = touchLocation 

      println("Touches"); 
     } 
    } 

    override func update(currentTime: CFTimeInterval) { 
     /* Called before each frame is rendered */ 
    } 
} 
+0

の内側にあなたのボールのタッチコードを実行している '-touchesBeganです'呼ばれている? (それがあるかどうかを確認するためにブレークポイントを設定します) –

+0

私はチェックしていて呼び出されていません。どうすれば修正できますか? @NicolasMiari –

+0

通常は、 'UIResponder'から継承された' userInteractionEnabled'プロパティが 'false'に設定されているためです。しかし、デフォルト値は 'true'です(少なくとも' UIResponder'では 'SKScene'についてはわかりません)。 –

答えて

2

すごいLOL、これはあなたがあなたのボールの正確なピクセル位置を打つことができる場合、私は感銘を受けるだろう面白い1、if touchLocation == ball.positionです。何が欲しいのはこれです:withEvent::

let touch = touches.first as! UITouch 
let touchLocation = touch.locationInNode(self) 
let touchedNode = self.nodeAtPoint(touchLocation) 
if touchedNode == ball{ 
... 

しかし、個人的に、私は何をするだろうことはSKSpriteNodeのサブクラスを作成し、この子のタッチルーチン