2017-04-25 89 views
0

我試圖讓我的播放器在觸摸開始時播放跳轉動畫。玩家在他自己的swift文件中,我有GameScene.swift呼叫他的文件,所以處理他的運動的一切都在player.swift文件中。任何幫助都是極好的!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") 
} 




} 

回答

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)) 

此外,您的動畫操作可能無法訪問,因爲它在另一個文件中。在全球聲明播放器和動畫,即在任何類別或功能之外。希望對你有幫助!