Olexander Korenyuk
Olexander Korenyuk

Reputation: 145

Tell iOS to not resize SKNode animated texture?

I'm coding a game for iOS with Swift.

The game is being initialized with an array of textures. I use those textures in animation.

I create an SKNode each time with different size:

let node = SKSpriteNode(texture: nodeTextures[0])
node.size = size; //from func argument
node.runAction(SKAction.repeatActionForever
(SKAction.animateWithTextures
(nodeTextures, timePerFrame: 0.05, resize: false, restore: false)))

Node rendered with resized texture.

Can I somehow tell iOS to not resize texture? If yes can I somehow define origin coordinate of texture in SKNode?

Upvotes: 2

Views: 603

Answers (2)

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

I think you've misunderstood the SpriteKit 'animate' resizing function. If you put "false" what will you see is only the real animation stretched to fill inside your node parent. So , you need to put the 'resize' parameter to true and you animation will be corrected to it's original size.

Using your code, you can simply scale your animation to fit inside your context:

if let firstTexture = nodeTextures.first {
   let node = SKSpriteNode(texture: firstTexture, size:size)
   let scaledAction = SKAction.scale(to: (node.size.height/firstTexture.size().height),duration:0.01)
   node.run(SKAction.repeatActionForever
(SKAction.sequence([scaledAction,SKAction.animate(nodeTextures, timePerFrame: 0.05, resize: true, restore: false)])))
}

Upvotes: 1

Gliderman
Gliderman

Reputation: 1205

Create a child node with the textures and the animation. Call node.addChild(textureNode) to make it a child. Then run the action on that child node, which will always be in the same position as node, but not change node's size. Something like this:

let node = SKSpriteNode()
node.size = size; //from func argument
// Create texture node to hold the texture
let textureNode = SKSpriteNode(texture: nodeTextures[0])
node.addChild(textureNode)
textureNode.runAction(SKAction.repeatActionForever
(SKAction.animateWithTextures
(nodeTextures, timePerFrame: 0.05, resize: false, restore: false)))

This allows you to adjust node's size later without rescaling the images in textureNode.

Upvotes: 1

Related Questions