Home > Software engineering >  Part of my animation is not working in SpriteKit
Part of my animation is not working in SpriteKit

Time:12-19

init () {
    
    super.init(texture: nil, color: .clear, size: initialSize)
    // Create physicsBody based on a frame of the sprite
    // basically giving it gravity and physics components
    let bodyTexture = textureAtlas.textureNamed("MainCharacterFlying1")
    self.physicsBody = SKPhysicsBody(texture: bodyTexture, size: self.size)
    // Lose momentum quickly with high linear dampening
    self.physicsBody?.linearDamping = 0.9
    // weighs around 30 kg
    self.physicsBody?.mass = 30
    // no rotation
    self.physicsBody?.allowsRotation = false
    createAnimations()
    self.run(soarAnimation, withKey: "soarAnimation")
}


// Animations to make the main character seem like it is flying
func createAnimations() {
    let rotateUpAction = SKAction.rotate(byAngle: 0, duration: 0.475)
    rotateUpAction.timingMode = .easeOut
    
    let rotateDownAction = SKAction.rotate(byAngle: -1, duration: 0.8)
    rotateDownAction.timingMode = .easeIn
    
    let flyFrames: [SKTexture] = [textureAtlas.textureNamed("MainCharacterFlying1"), textureAtlas.textureNamed("MainCharacterFlying2"),textureAtlas.textureNamed("MainCharacterFlying3"), textureAtlas.textureNamed("MainCharacterFlying4"),textureAtlas.textureNamed("MainCharacterFlying5"),textureAtlas.textureNamed("MainCharacterFlying4"),textureAtlas.textureNamed("MainCharacterFlying3"),textureAtlas.textureNamed("MainCharacterFlying2")]
    var flyAction = SKAction.animate(with: flyFrames, timePerFrame: 0.1)
    flyAction = SKAction.repeatForever(flyAction)
    flyAnimation = SKAction.group([flyAction,rotateUpAction])
    
    let soarFrames: [SKTexture] = [textureAtlas.textureNamed("MainCharacterFlying5")]
    var soarAction = SKAction.animate(with: soarFrames, timePerFrame: 1)
    soarAction = SKAction.repeatForever(soarAction)
    let soarAnimation = SKAction.group([soarAction,rotateDownAction])
}

When I run this code on the IOS Simulator, I have to click on the screen once in order for my main sprite to show up on the screen, or else it will not. And when I click on the sprite, the sprite will start flapping its wings and go up (I have other code for that) however, the rotateUpAction and rotateDownAction are not showing up at all on the Simulator. So I was wondering if there were any solutions and anyone willing to answer. Thank you for your time. Also, this code from the class of the main character, the name of the class is "Player"

CodePudding user response:

you declare let soarAnimation inside your createAnimations function, meaning it's not in scope when you call self.run(soarAnimation). Solution: declare soarAnimation as a class property. Alternate solution: have createAnimations() return the SKAction and grab it in init that way

  • Related