Swift:玩家跳跃动画

时间:2017-04-25 03:20:12

标签: swift animation

我试图让我的播放器在触摸开始时播放跳跃动画。玩家在他自己的swift文件中,我让GameScene.swift调用他的文件,所以处理他的动作的所有内容都在player.swift文件中。任何帮助都会很棒!

import SpriteKit




struct ColliderType {
static let PLAYER: UInt32 = 0
static let GROUND: UInt32 = 1
static let ROCKET_AND_COLLECTABLES: UInt32 = 2


}



class Player: SKSpriteNode {

private var textureAtlas = SKTextureAtlas()
private var playerAnimation = [SKTexture]()
private var animatePlayerAction = SKAction()
let jumpAnimation1 = [SKTexture]()
let jumpAnimation2 = [SKTexture]()

var jumpAnimation = SKAction ()

func initializePlayer() {
    name = "Player"

    for i in 1...7 {
        let name = "Player \(i)"
        playerAnimation.append(SKTexture(imageNamed: name))



    }


    animatePlayerAction = SKAction.animate(with: playerAnimation,    

timePerFrame: 0.08, resize: true, restore: false)

    //Animate the player forever.
    self.run(SKAction.repeatForever(animatePlayerAction))


    physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 
self.size.width-60, height: self.size.height-80))
    physicsBody?.affectedByGravity = true
    physicsBody?.allowsRotation = false
    //Restitution is Boucniess 
    physicsBody?.restitution  = 0
    physicsBody?.categoryBitMask = ColliderType.PLAYER
    physicsBody?.collisionBitMask = ColliderType.GROUND
    physicsBody?.contactTestBitMask =                                        

ColliderType.ROCKET_AND_COLLECTABLES

}




func move (){
    self.position.x += 10
}

func reversePlayer() {
    physicsBody?.velocity = CGVector(dx: 0, dy: 0)
    physicsBody?.applyImpulse(CGVector(dx: 0, dy: 300))

    playerNode?.removeAction(forKey:"animate")           playerNode?.run(attackAnimation,completion:{
        self.playerNode?.run(self.animation,withKey: "animate")
}




}

1 个答案:

答案 0 :(得分:0)

替换设置纹理的部分,使用以下代码段创建并运行操作。它对我来说非常适合,并且是最简单的方法。

let hero = SKSpriteNode(imageNamed: "hero")
hero.size = CGSize(width: 45, height: 125)
let f1 = SKTexture.init(imageNamed: "h1")
let f2 = SKTexture.init(imageNamed: "h2")
let f3 = SKTexture.init(imageNamed: "h3")
let frames: [SKTexture] = [f1, f2, f3]   
let hero = SKSpriteNode(imageNamed: "h1")
let animation = SKAction.animate(with: frames, timePerFrame: 0.1)
hero.run(SKAction.repeatForever(animation))

此外,您的动画操作可能无法像在另一个文件中那样被访问。全局声明播放器和动画,即在任何类或功能之外。希望它适合你!