我有一個玩家將physicsBody分成了幾部分(軀幹,腿,頭等),現在我在屏幕上有一個控制玩家動作的按鈕,如果按鈕向右按下,玩家向右移動,該部分正常工作。問題在於,每次運動改變時,walk()方法被調用,它所做的是讓玩家的腿看起來像它的行走,但是如果運動不停止,那麼在沒有它的情況下會一直調用walk()動畫,所以它看起來像卡住來回。我試圖實現的是讓玩家能夠不斷走路,但是walk()(動畫方法)被調用一次,完成,然後再次調用(只要仍然按下走路按鈕)。這裏是我到目前爲止有:在不禁用觸控的情況下在SpriteNode上運行的完成動作
func walk(){
let leftFootWalk = SKAction.run {
let action = SKAction.moveBy(x: 1, y: 0.1, duration: 0.1)
let actionReversed = action.reversed()
let seq = SKAction.sequence([action, actionReversed])
self.leftUpperLeg.run(seq)
self.leftLowerLeg.run(seq)
}
let rightFootWalk = SKAction.run {
let action = SKAction.moveBy(x: 0.4, y: 0.1, duration: 0.1)
let actionReversed = action.reversed()
let seq = SKAction.sequence([action, actionReversed])
self.rightUpperLeg.run(seq)
self.rightLowerLeg.run(seq)
}
let group = SKAction.sequence([leftFootWalk, SKAction.wait(forDuration: 0.2), rightFootWalk])
run(group)
}
extension GameScene: InputControlProtocol {
func directionChangedWithMagnitude(position: CGPoint) {
if isPaused {
return
}
if let fighter = self.childNode(withName: "fighter") as? SKSpriteNode, let fighterPhysicsBody = fighter.physicsBody {
fighterPhysicsBody.velocity.dx = position.x * CGFloat(300)
walk()
if position.y > 0{
fighterPhysicsBody.applyImpulse(CGVector(dx: position.x * CGFloat(1200),dy: position.y * CGFloat(1200)))
}else{
print("DUCK") //TODO: duck animation
}
}
}