5

Im messing around with an xcode project and I am trying to make a red ball SKShapeNode move around when I touch it. My own source code in the touch began class is not doing the trick. What is the flaw with my code and what do I need to do in order to fix it.

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 */
    }
}
Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189
Cj Miller
  • 51
  • 1
  • 7
  • Is `-touchesBegan: withEvent :` being called? (set up a breakpoint to see if it is) – Nicolas Miari Dec 25 '15 at 04:21
  • I checked and it is not being called. How do I fix it? @NicolasMiari – Cj Miller Dec 25 '15 at 04:25
  • Typically it is because the property `userInteractionEnabled` (inherited from `UIResponder`) is set to `false`. However, the default value is `true` (at least for `UIResponder`; not sure for `SKScene`). – Nicolas Miari Dec 25 '15 at 04:30

1 Answers1

2

LOL wow, this is a funny one, if touchLocation == ball.position I would be impressed if you can hit the exact pixel location of your ball. What you want is this:

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

But personally, what I would do is make a sub class of SKSpriteNode, and do your ball's touch code inside this child's touch routine

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44