2015-12-25 27 views
5

मैं एक एक्सकोड प्रोजेक्ट के साथ गड़बड़ कर रहा हूं और मैं इसे छूने पर लाल गेंद बनाने के लिए एसकेशैप नोड को स्थानांतरित करने की कोशिश कर रहा हूं। स्पर्श में मेरा अपना स्रोत कोड शुरू हुआ वर्ग चाल नहीं कर रहा है। मेरे कोड के साथ दोष क्या है और इसे ठीक करने के लिए मुझे क्या करने की ज़रूरत है।स्विफ्ट स्प्राइट किट टॉग फंक्शन

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

आमतौर पर ऐसा इसलिए होता है क्योंकि संपत्ति 'userInteractionEnabled' ('UIResponder' से विरासत में मिली) को' false' पर सेट किया गया है। हालांकि, डिफ़ॉल्ट मान 'सत्य' है (कम से कम 'UIResponder' के लिए; 'SKScene' के लिए निश्चित नहीं है)। –

उत्तर

2

LOL वाह, यह एक अजीब एक है, 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 की एक उप वर्ग बनाने के लिए, और

संबंधित मुद्दे