与我在应用程序商店中找到的许多其他应用程序一样,当您触摸按钮时,它会以某种方式缩放,无论它是变大还是变小。我想使用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)
}
}
}
缩放在touchesBegan
和touchesEnded
中完全正常,但我希望用户能够移动触摸,如果它开始触及名称为“缩放”的新节点“我希望它与Touches开始具有相同的效果,如果触摸远离节点,我希望它具有与touchesEnded
相同的效果。感谢您的帮助,对于因为缺乏细节而感到抱歉,因为它是凌晨3点。
答案 0 :(得分:1)
我想出了如何使用单独的Boolean
值来表示节点当前是否已缩放。用户现在可以触摸并按住;如果他们将手指移离节点,它将scaleTo
正常(1.0
),如果他们将手指移动到节点上,它将scaleTo
1.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)
}
}
}