我正在尝试对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类的一个方法(实际上是他们的卡片)。我将其简化为在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)
按照我的意愿工作,但意味着我不能使用Card类中的方法。
答案 0 :(得分:1)
当您使用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")
}