【问题标题】:Node and Button Scaling - Touches Began, Touches Moved, Touches Ended节点和按钮缩放 - 触摸开始、触摸移动、触摸结束
【发布时间】:2014-12-31 19:40:09
【问题描述】:

就像我在应用商店中找到的许多其他应用一样,当您触摸一个按钮时,它会以某种方式缩放,无论它变大还是变小。我想使用SpriteKit 节点和SKAction 来实现这一点; scaleTo。这是我到目前为止所得到的。

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)
        }
    }
}

缩放在touchesBegantouchesEnded 中工作得非常好,但我希望用户能够移动触摸,并且如果它开始触摸一个新节点,该节点也以“Scale”为名,我想要它具有与 Touches Began 相同的效果,如果触摸远离节点,我希望它具有与 touchesEnded 相同的效果。感谢您的帮助,抱歉之前是凌晨 3:00,所以没有详细说明。

【问题讨论】:

  • 请拨打tour 并阅读How to Ask,了解我们对问题的期望。以目前的形式,您的问题无法回答,因为它太宽泛了。请收集更多信息,然后edit您的帖子。
  • 抱歉,已更新。

标签: swift sprite-kit skaction touchesmoved


【解决方案1】:

我想出了如何使用单独的Boolean 值来表示节点当前是否被缩放。用户现在可以触摸和按住;如果他们将手指从节点上移开,它将scaleTo 正常(1.0),如果他们将手指移到节点上,它将scaleTo1.5。我的代码运行良好,但我不确定是否有更完善的方法可以做到这一点。这是我的代码(这个有效),但如果有人有更精致和更有效的方法来实现这一点,请发布您的答案。谢谢你

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)
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多