嵌套 SKActions 不使用等待时间
Nesting SKActions not using wait duration
我正在尝试在 SKSpriteNode 上执行以下操作:
把它变大,
将其移动到新位置,
再变小。
但是,代码似乎运行 一下子变大和变小相互抵消了。
这是在 Playground 中创建的代码:
import PlaygroundSupport
import UIKit
import SpriteKit
var theirCard:SKSpriteNode
let CARDSHRINK_TIME = 5.0
let AICARDMOVE_SPEED = 2.5
let droppoint:CGPoint = CGPoint(x: 100,y: 100)
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 480, height: 320))
let scene = SKScene(size: CGSize(width: 480, height: 320))
sceneView.showsFPS = true
sceneView.presentScene(scene)
PlaygroundSupport.PlaygroundPage.current.liveView = sceneView
theirCard = SKSpriteNode(imageNamed: "card")
theirCard.name = "theircard"
theirCard.position = CGPoint(x: scene.size.width * 0.50, y: scene.size.height * 0.5)
scene.addChild(theirCard)
func getwait(makeBig big:Bool) -> SKAction {
let ret = SKAction.wait(forDuration: CARDSHRINK_TIME)
var action:SKAction
if big {
action = SKAction.scale(to: 1.9, duration: CARDSHRINK_TIME)
}
else {
action = SKAction.scale(to: 1.0, duration: CARDSHRINK_TIME)
}
theirCard.run(action, completion: { print("getwait \(big) done!") } )
return ret
}
// We want to MAKE the node BIG, move it to the location, then make it SMALL again
let action1:SKAction = getwait(makeBig: true)
let action2:SKAction = SKAction.move(to: droppoint, duration: AICARDMOVE_SPEED)
let action3:SKAction = getwait(makeBig: false )
let seq:SKAction = SKAction.sequence([action1, action2, action3])
theirCard.run(seq, completion: {
print("animation complete!!!!")
})
我需要保持 getwait 的原样,因为在实际代码中它是 Card class 的一个方法(实际上是 theirCard)。我简化了它以便在 Playground
中进行测试
更新:
let action1 = SKAction.scale(to: 1.9, duration: CARDSHRINK_TIME)
let action2:SKAction = SKAction.move(to: droppoint, duration: AICARDMOVE_SPEED)
let action3 = SKAction.scale(to: 1.0, duration: CARDSHRINK_TIME)
如我所愿,但这意味着我无法使用卡片 class 中的方法。
使用 getwait(makeBig: true)
时一次性执行 运行 的原因是因为以下行:
theirCard.run(action, completion: { print("getwait \(big) done!") } )
无论何时调用该函数,您都会立即使卡片 运行 成为一个动作。所以你可以看到为什么连续打电话
getwait(makeBig: true)
getwait(makeBig: false)
只会在 相同 时间产生卡片放大和缩小的净效应。
建议改写如下:
func getScaleAction(makeBig big:Bool) -> SKAction {
return SKAction.scale(to: big? 1.9 : 1.0, duration: CARDSHRINK_TIME)
}
并按如下方式调用它:
let scaleUp = getScaleAction(makeBig: true)
let move = SKAction.move(to: droppoint, duration: AICARDMOVE_SPEED)
let scaleDown = getScaleAction(makeBig: false)
theirCard.run(SKAction.sequence([scaleUp, move, scaleDown])) {
print("done")
}
我正在尝试在 SKSpriteNode 上执行以下操作: 把它变大, 将其移动到新位置, 再变小。
但是,代码似乎运行 一下子变大和变小相互抵消了。
这是在 Playground 中创建的代码:
import PlaygroundSupport
import UIKit
import SpriteKit
var theirCard:SKSpriteNode
let CARDSHRINK_TIME = 5.0
let AICARDMOVE_SPEED = 2.5
let droppoint:CGPoint = CGPoint(x: 100,y: 100)
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 480, height: 320))
let scene = SKScene(size: CGSize(width: 480, height: 320))
sceneView.showsFPS = true
sceneView.presentScene(scene)
PlaygroundSupport.PlaygroundPage.current.liveView = sceneView
theirCard = SKSpriteNode(imageNamed: "card")
theirCard.name = "theircard"
theirCard.position = CGPoint(x: scene.size.width * 0.50, y: scene.size.height * 0.5)
scene.addChild(theirCard)
func getwait(makeBig big:Bool) -> SKAction {
let ret = SKAction.wait(forDuration: CARDSHRINK_TIME)
var action:SKAction
if big {
action = SKAction.scale(to: 1.9, duration: CARDSHRINK_TIME)
}
else {
action = SKAction.scale(to: 1.0, duration: CARDSHRINK_TIME)
}
theirCard.run(action, completion: { print("getwait \(big) done!") } )
return ret
}
// We want to MAKE the node BIG, move it to the location, then make it SMALL again
let action1:SKAction = getwait(makeBig: true)
let action2:SKAction = SKAction.move(to: droppoint, duration: AICARDMOVE_SPEED)
let action3:SKAction = getwait(makeBig: false )
let seq:SKAction = SKAction.sequence([action1, action2, action3])
theirCard.run(seq, completion: {
print("animation complete!!!!")
})
我需要保持 getwait 的原样,因为在实际代码中它是 Card class 的一个方法(实际上是 theirCard)。我简化了它以便在 Playground
中进行测试更新:
let action1 = SKAction.scale(to: 1.9, duration: CARDSHRINK_TIME)
let action2:SKAction = SKAction.move(to: droppoint, duration: AICARDMOVE_SPEED)
let action3 = SKAction.scale(to: 1.0, duration: CARDSHRINK_TIME)
如我所愿,但这意味着我无法使用卡片 class 中的方法。
使用 getwait(makeBig: true)
时一次性执行 运行 的原因是因为以下行:
theirCard.run(action, completion: { print("getwait \(big) done!") } )
无论何时调用该函数,您都会立即使卡片 运行 成为一个动作。所以你可以看到为什么连续打电话
getwait(makeBig: true)
getwait(makeBig: false)
只会在 相同 时间产生卡片放大和缩小的净效应。
建议改写如下:
func getScaleAction(makeBig big:Bool) -> SKAction {
return SKAction.scale(to: big? 1.9 : 1.0, duration: CARDSHRINK_TIME)
}
并按如下方式调用它:
let scaleUp = getScaleAction(makeBig: true)
let move = SKAction.move(to: droppoint, duration: AICARDMOVE_SPEED)
let scaleDown = getScaleAction(makeBig: false)
theirCard.run(SKAction.sequence([scaleUp, move, scaleDown])) {
print("done")
}