从节点滑动时的 RemoveAction
RemoveAction when swiped from node
我有这个场景,里面只有几个节点。它是圈内圈内圈。按下里面最小的圆圈,我用很少的 SKActions 制作了这个动画。
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
let growOut = SKAction.scaleTo(1.2, duration: 0.3)
let growIn = SKAction.scaleTo(1.0, duration: 0.5)
let glowOut = SKAction.fadeAlphaTo(0.5, duration: 0.3)
let glowIn = SKAction.fadeAlphaTo(1, duration: 0.5)
let sOut = SKAction.group([glowOut, growOut])
let sIn = SKAction.group([glowIn, growIn])
let circleTouched = SKAction.sequence([sOut, sIn])
let circleRepeat = SKAction.repeatActionForever(circleTouched)
for touch in touches {
let location = (touch as! UITouch).locationInNode(self)
if let theCircle = nodeAtPoint(location) as SKNode?{
if theCircle.name == "SmallCircle" {
theCircle.runAction(circleRepeat, withKey: "circleTouched")
}
}
}
}
当触摸结束时,我像这样删除这个动作:
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
let growIn = SKAction.scaleTo(1.0, duration: 0.5)
let glowIn = SKAction.fadeAlphaTo(1, duration: 0.5)
let sIn = SKAction.group([glowIn, growIn])
for touch in touches {
let location = (touch as! UITouch).locationInNode(self)
if let theCircle = nodeAtPoint(location) as SKNode?{
theCircle.runAction(sIn)
theCircle.removeActionForKey("circleTouched")
}
}
}
但是当我将手指移出这个带有动作的圆圈时,它会继续播放。我尝试用 touchesMoved 函数修复它,但它对我来说有点奇怪。
override func touchesMoved(touches: Set, withEvent event: UIEvent) {
let growIn = SKAction.scaleTo(1.0, duration: 0.5)
let glowIn = SKAction.fadeAlphaTo(1, duration: 0.5)
let sIn = SKAction.group([glowIn, growIn])
let circleRepeat = SKAction.repeatActionForever(circleTouched)
for touch in touches {
let location = (touch as! UITouch).locationInNode(self)
if let theCircle = nodeAtPoint(location) as SKNode?{
if theCircle.name != "SmallCircle" {
println("I'M MOVING FROM NODE!!!")
theCircle.runAction(sIn)
theCircle.removeActionForKey("circleTouched")
}
}
}
}
所以我收到这个 "I'M OUT OF THE NODE" 信号,但动作不会停止。
我哪里错了?相同的代码适用于 touchesEnded 函数。
问题就是因为这个
if let theCircle = nodeAtPoint(location) as SKNode?
每次移动鼠标时,"theCircle" 都会重置。例如,第一次点击圆圈,"theCircle"就是你点击的圆圈,因此动画附在圆圈上。第二次,比如说,你点了背景,这次"theCircle"是背景,所以没有你设置的动画,所以没办法去掉"the animation".
解决办法是,你把圆圈声明为作用域级变量,通常在class内,在顶部:
var smallCircle: SKSpriteNode!
然后在didMoveToView(view: SKView)中配置圆圈(如果使用.sks):
smallCircle = childNodeWithName("the circle name") as! SKSpriteNode
smallCircle.name = "SmallCircle"
这次可以在touchMoved中指向圆圈:
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if let theCircle = nodeAtPoint(location) as SKNode?{
if theCircle.name != "SmallCircle" {
smallCircle.runAction(sIn)
smallCircle.removeActionForKey("circleTouched")
}
}
}
最后,你会发现动画停止了。
我有这个场景,里面只有几个节点。它是圈内圈内圈。按下里面最小的圆圈,我用很少的 SKActions 制作了这个动画。
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
let growOut = SKAction.scaleTo(1.2, duration: 0.3)
let growIn = SKAction.scaleTo(1.0, duration: 0.5)
let glowOut = SKAction.fadeAlphaTo(0.5, duration: 0.3)
let glowIn = SKAction.fadeAlphaTo(1, duration: 0.5)
let sOut = SKAction.group([glowOut, growOut])
let sIn = SKAction.group([glowIn, growIn])
let circleTouched = SKAction.sequence([sOut, sIn])
let circleRepeat = SKAction.repeatActionForever(circleTouched)
for touch in touches {
let location = (touch as! UITouch).locationInNode(self)
if let theCircle = nodeAtPoint(location) as SKNode?{
if theCircle.name == "SmallCircle" {
theCircle.runAction(circleRepeat, withKey: "circleTouched")
}
}
}
}
当触摸结束时,我像这样删除这个动作:
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
let growIn = SKAction.scaleTo(1.0, duration: 0.5)
let glowIn = SKAction.fadeAlphaTo(1, duration: 0.5)
let sIn = SKAction.group([glowIn, growIn])
for touch in touches {
let location = (touch as! UITouch).locationInNode(self)
if let theCircle = nodeAtPoint(location) as SKNode?{
theCircle.runAction(sIn)
theCircle.removeActionForKey("circleTouched")
}
}
}
但是当我将手指移出这个带有动作的圆圈时,它会继续播放。我尝试用 touchesMoved 函数修复它,但它对我来说有点奇怪。
override func touchesMoved(touches: Set, withEvent event: UIEvent) {
let growIn = SKAction.scaleTo(1.0, duration: 0.5)
let glowIn = SKAction.fadeAlphaTo(1, duration: 0.5)
let sIn = SKAction.group([glowIn, growIn])
let circleRepeat = SKAction.repeatActionForever(circleTouched)
for touch in touches {
let location = (touch as! UITouch).locationInNode(self)
if let theCircle = nodeAtPoint(location) as SKNode?{
if theCircle.name != "SmallCircle" {
println("I'M MOVING FROM NODE!!!")
theCircle.runAction(sIn)
theCircle.removeActionForKey("circleTouched")
}
}
}
}
所以我收到这个 "I'M OUT OF THE NODE" 信号,但动作不会停止。 我哪里错了?相同的代码适用于 touchesEnded 函数。
问题就是因为这个
if let theCircle = nodeAtPoint(location) as SKNode?
每次移动鼠标时,"theCircle" 都会重置。例如,第一次点击圆圈,"theCircle"就是你点击的圆圈,因此动画附在圆圈上。第二次,比如说,你点了背景,这次"theCircle"是背景,所以没有你设置的动画,所以没办法去掉"the animation".
解决办法是,你把圆圈声明为作用域级变量,通常在class内,在顶部:
var smallCircle: SKSpriteNode!
然后在didMoveToView(view: SKView)中配置圆圈(如果使用.sks):
smallCircle = childNodeWithName("the circle name") as! SKSpriteNode
smallCircle.name = "SmallCircle"
这次可以在touchMoved中指向圆圈:
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if let theCircle = nodeAtPoint(location) as SKNode?{
if theCircle.name != "SmallCircle" {
smallCircle.runAction(sIn)
smallCircle.removeActionForKey("circleTouched")
}
}
}
最后,你会发现动画停止了。