Ejmedina
Ejmedina

Reputation: 509

How to animate with textures of different sizes in SpriteKit

I'm trying to animate a walking character's dead but the dying sprites are different on proportion than the walking ones.

First I create a SKSpriteNode on func didMove(to view: SKView) { and add it to the scene:

//create monster, add physics to detect collision with projectile and add to scene with walking animation
let monster = SKSpriteNode(imageNamed: "zombie")
monster.yScale = CGFloat(-0.58)
monster.xScale = CGFloat(0.58)
monster.physicsBody = SKPhysicsBody(rectangleOf: monster.size)
monster.physicsBody?.isDynamic = true
monster.physicsBody?.categoryBitMask = PhysicsCategory.Monster
monster.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile
monster.physicsBody?.collisionBitMask = PhysicsCategory.None
// Determine where to spawn the monster along the Y axis
 let actualY = random(min: monster.size.height/2, max: size.height - (monster.size.height/2)-20)
 // Position the monster slightly off-screen along the right edge,
 // and along a random position along the Y axis as calculated above
 monster.position = CGPoint(x: size.width + monster.size.width/2, y: actualY)

// Add the monster to the scene
        worldNode.addChild(monster)

        // Determine speed of the monster

        let actualDuration = random(min: CGFloat(2.0)-CGFloat(self.level/10), max: CGFloat(4.0)-CGFloat(self.level/10))

        // Create the actions
        let actionMove = SKAction.move(to: CGPoint(x: -monster.size.width/2, y: actualY), duration: TimeInterval(actualDuration))

        let actionMoveDone = SKAction.removeFromParent()

        let atlas = SKTextureAtlas(named: monsterVariant)
        var m0 = SKTexture()
        var m1 = SKTexture()
        var m2 = SKTexture()
        var m3 = SKTexture()
        var m4 = SKTexture()
        var m5 = SKTexture()
        m0 = atlas.textureNamed("\(monsterVariant)_Walk1") //222x396
        m1 = atlas.textureNamed("\(monsterVariant)_Walk2")//222x366
        m2 = atlas.textureNamed("\(monsterVariant)_Walk3")//250x375
        m3 = atlas.textureNamed("\(monsterVariant)_Walk4")//237×379
        m4 = atlas.textureNamed("\(monsterVariant)_Walk5")//221×371
        m5 = atlas.textureNamed("\(monsterVariant)_Walk6")//228×402

        let textures = [m0,m1,m2,m3,m4,m5]
        let walkAnimation = SKAction.animate(with:textures, timePerFrame: 0.07)
        monster.run(SKAction.repeatForever(walkAnimation))

Then, when a projectile and a monster collide I want the die animation to play and the monster to dissapear. Please note textures dimensions on comments:

func projectileDidCollideWithMonster(projectile: SKSpriteNode, monster: SKSpriteNode) {
        let dyingMonsterPosition = monster.position
        addToScoreBasedOnPosition(monster:monster)
        projectile.removeFromParent()

        let atlas = SKTextureAtlas(named: String(monster.name!))
        print(String(monster.name!))
        var m1 = SKTexture()
        var m2 = SKTexture()
        var m3 = SKTexture()
        var m4 = SKTexture()
        var m5 = SKTexture()
        var m6 = SKTexture()
        var m7 = SKTexture()
        var m8 = SKTexture()
        m1 = atlas.textureNamed("\(String(monster.name!))_Dead1")//220 × 374
        m2 = atlas.textureNamed("\(String(monster.name!))_Dead2")//272 × 377
        m3 = atlas.textureNamed("\(String(monster.name!))_Dead3")//349 × 355
        m4 = atlas.textureNamed("\(String(monster.name!))_Dead4")//384 × 296
        m5 = atlas.textureNamed("\(String(monster.name!))_Dead5")//380 × 248
        m6 = atlas.textureNamed("\(String(monster.name!))_Dead6")//406 × 218
        m7 = atlas.textureNamed("\(String(monster.name!))_Dead7")//465 × 211
        m8 = atlas.textureNamed("\(String(monster.name!))_Dead8")//475 × 193
        let textures = [m1,m2,m3,m4,m5,m6,m7,m8]

        let playSound = SKAction.run {
            SKAction.playSoundFileNamed("163442__under7dude__man-dying.wav", waitForCompletion: false)
        }
        let dieAnimation = SKAction.animate(with: textures, timePerFrame: 0.1, resize: false, restore: true)
        let removeMonster =  SKAction.run {
            monster.removeFromParent()
        }
        let monsterDieSequence = SKAction.sequence([playSound,dieAnimation,removeMonster])
        monster.removeAllActions()
        monster.run(monsterDieSequence)
}

If I use resize:true from SKAction.animate I have a perfect animation but I lose the original monster.xScale and monster.yScale. The monster "grows to die".

enter image description here

If I use resize:false the animation is deformed:

enter image description here

There is a possible answer in this Question: animate-an-skspritenode-with-textures-that-have-a-size-different-from-the-origin

But I think making all sprites the same size and adding transparence can't be the only option for solving this.

Upvotes: 4

Views: 657

Answers (1)

folimero abdala
folimero abdala

Reputation: 1

have you tried not to use a rotated sprite?, i mean,draw the whole dying sequence whih the character portrait, and do the rotation effect with code in the dying sequence with zRotation method grouped with the animation using SKAction.group([rotate,animate]).

Upvotes: 0

Related Questions