简体   繁体   中英

Node and Button Scaling - Touches Began, Touches Moved, Touches Ended

Like many other apps i have found on the app store when you touch a button it scales in some way, whether it gets bigger or smaller. I want to achieve this using SpriteKit nodes and the SKAction ; scaleTo . This is what i have got so far.

import SpriteKit

class GameEndedScene: SKScene {

let scaleIn = SKAction.scaleTo(1.5, duration: 0.2)
let scaleOut = SKAction.scaleTo(1.0, duration: 0.2)

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    var playButton = SKSpriteNode()
    var shareButton = SKSpriteNode()

    playButton.size = CGSizeMake(100, 100)
    shareButton.size = CGSizeMake(100, 100)

    playButton.position = CGPointMake(self.frame.midX, self.frame.midY + 100)
    shareButton.position = CGPointMake(self.frame.midX, self.frame.midY - 100)

    playButton.color = UIColor.whiteColor()
    shareButton.color = UIColor.whiteColor()

    playButton.name = "Scale"
    shareButton.name = "Scale"

    addChild(playButton)
    addChild(shareButton)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let node: SKNode = nodeAtPoint(location)

        if node.name == "Scale" {
            node.runAction(scaleIn)
        }
    }
}

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

}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let node: SKNode = nodeAtPoint(location)

        if node.name == "Scale" {
            node.runAction(scaleOut)
        }
    }
}

The scaling works perfectly fine in the touchesBegan And touchesEnded but i want the user to be able to move the touch and if it begins to touch a new node that is also under the name "Scale" i want it to have the same affect as Touches Began, And if touch moves away from the node i want it to have the same affect as touchesEnded . Thank you for any help, sorry for the lack of detail before as it was 3:00 am.

I figured out how to achieve this using separate Boolean values to represent whether or not the node is currently scaled. The user now has the ability to touch and hold; if they move their finger away from a node it will scaleTo normal ( 1.0 ) and if they move their finger onto a node it will scaleTo 1.5 . The code i have works perfectly but I'm not sure if there is a more refined way available to do this. Here is my code (This one works) but if anyone has a more refined and efficient way to achieve this please post your answer. Thank You

import SpriteKit

class GameEndedScene: SKScene {

var playButton = SKSpriteNode()
var shareButton = SKSpriteNode()
let scaleIn = SKAction.scaleTo(1.5, duration: 0.2)
let scaleOut = SKAction.scaleTo(1.0, duration: 0.2)
var playScale: Bool = Bool()
var shareScale: Bool = Bool()

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    playButton.size = CGSizeMake(100, 100)
    shareButton.size = CGSizeMake(100, 100)

    playButton.position = CGPointMake(self.frame.midX, self.frame.midY + 100)
    shareButton.position = CGPointMake(self.frame.midX, self.frame.midY - 100)

    playButton.color = UIColor.whiteColor()
    shareButton.color = UIColor.whiteColor()

    addChild(playButton)
    addChild(shareButton)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let node: SKNode = nodeAtPoint(location)

        if node == playButton {
            playScale = true
            playButton.runAction(scaleIn)
        }
        else if node == shareButton {
            shareScale = true
            shareButton.runAction(scaleIn)
        }
    }
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let node: SKNode = nodeAtPoint(location)

        if node != playButton && (playScale) {
            playScale = false
            playButton.runAction(scaleOut)
        }
        else if node != shareButton && (shareScale) {
            shareScale = false
            shareButton.runAction(scaleOut)
        }
        else if node == playButton && (!playScale) {
            playScale = true
            playButton.runAction(scaleIn)
        }
        else if node == shareButton && (!shareScale) {
            shareScale = true
            shareButton.runAction(scaleIn)
        }
    }
}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let node: SKNode = nodeAtPoint(location)

        if node == playButton {
            playScale = false
            playButton.runAction(scaleOut)
        }
        else if node == shareButton {
            shareScale = false
            shareButton.runAction(scaleOut)
        }
        else if node != playButton && (playScale) {
            playScale = false
            playButton.runAction(scaleOut)
        }
        else if node != shareButton && (shareScale) {
            shareScale = false
            shareButton.runAction(scaleOut)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM