-1

In SpriteKit, I want to catch event when touch moves over a sprite, but hasn't actually started on this sprite, but on another piece of SKScene.

I can catch touchesBegan inside the SKSpriteNode A if the touch starts on it and then dragged over it, but not when touch started on another node - B - and then dragged over my node - A. Anyone knows how to catch this one, because I think I am doing something wrong here.

2 Answers 2

1

try this: sorry this is swift.. but you can easily do the same thing in obj c

import SpriteKit

class GameScene: SKScene {

    let sprite = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(100, 100))
    var startedOutsideSprite = true

    override init(size: CGSize) {
        super.init(size: size)
        sprite.position = CGPointMake(size.width/2, size.height/2)
        addChild(sprite)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let touch = touches.first {
            let location = touch.locationInNode(self)
            if !sprite.containsPoint(location) {
                startedOutsideSprite = true
            }
        }
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let touch = touches.first {
            let location = touch.locationInNode(self)

            if sprite.containsPoint(location) && startedOutsideSprite {
                print("yayyy")
                // your code here
            }
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        startedOutsideSprite = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
0

Thanks @Christian W. but I have a simpleer solution for now, although it is not what I had on mind actually: Just put this in Scene and catch the touchesMoved inside it, having this code inside it:

SKNode * draggedOverNode = [self nodeAtPoint:location];
[draggedOverNode touchesMoved:touches withEvent:event];

And implement the touchesMoved function inside that object that actually extends the SKNode (which most of your classes will do).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.