ecoguy
ecoguy

Reputation: 665

How can I move sprites around with my finger?

I want to be able to move a sprite with my finger. I first have a variable for whether the finger is on the sprite or not:

var isFingerOnGlow = false

Then I have the touchesBegan function where I change the value of the above variable if the user touches the sprite:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    var touch = touches.anyObject() as UITouch!
    var touchLocation = touch.locationInNode(self)

    if let body = physicsWorld.bodyAtPoint(touchLocation) {
        if body.node!.name == GlowCategoryName {
            println("Began touch on hero")
            isFingerOnGlow = true
        }
    }
}

Sometimes it works, and the console displays "began touch on hero", sometimes not. Is there a better way to code this first part?

Then I wonder if my following code for touchesMoved is effective:

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    if isFingerOnGlow{
        var touch = touches.anyObject() as UITouch!
        var touchLocation = touch.locationInNode(self)
        var previousLocation = touch.previousLocationInNode(self)

        var glow = SKSpriteNode(imageNamed: GlowCategoryName)

        // Calculate new position along x for glow
        var glowX = glow.position.x + (touchLocation.x - previousLocation.x)
        var glowY = glow.position.y + (touchLocation.y - previousLocation.y)

        // Limit x so that glow won't leave screen to left or right
        glowX = max(glowX, glow.size.width/2)
        glowX = min(glowX, size.width - glow.size.width/2)

        glow.position = CGPointMake(glowX, glowY)
    }
}

...

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    isFingerOnGlow = false
}

Upvotes: 1

Views: 726

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

You can modify the functions like this to track the touches.

var isFingerOnGlow = false
var touchedGlowNode : SKNode!


override func touchesBegan(touches: NSSet, withEvent event:UIEvent) {
    var touch = touches.anyObject() as UITouch!
    var touchLocation = touch.locationInNode(self)

    let body = self.nodeAtPoint(touchLocation)
    if body.name == GlowCategoryName {
        println("Began touch on hero")
        touchedGlowNode = body
        isFingerOnGlow = true
    }

}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {

    if isFingerOnGlow{

        let touch = touches.anyObject() as UITouch!
        let touchLocation = touch.locationInNode(self)
        let previousLocation = touch.previousLocationInNode(self)
        let distanceX = touchLocation.x - previousLocation.x
        let distanceY = touchLocation.y - previousLocation.y

        touchedGlowNode.position = CGPointMake(touchedGlowNode.position.x + distanceX,
            touchedGlowNode.position.y + distanceY)
    }
}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    isFingerOnGlow = false
}

Upvotes: 2

Related Questions