SkSpritenode normal texture not restoring even with restore on?
Suggested Solutions
Try setting your sprite to the last texture in the atlas like this: theSprite.texture = atlas[atlas.count-1]
. Then run your code again. You should see that it restores to back to atlas[atlas.count-1]
.
If your animation stops at a texture other than your last atlas texture, then you could try this. Set theSprite.texture = atlas[atlas.count-1]
within your animation's completion block. You would also set restore
to false
in this case. This will animate your sprite then set the final texture to whatever you want, in this case it will be set to the last atlas texture.
Explanation
Restore will return your sprite back to the texture that it had before the animation began. So in your case with your current code, it should end the animation with the texture atlas[0]
. It is not meant to end on the last frame of the array as you stated.
According to the Apple dev reference, if restore
is true
:
When the action completes, the sprite’s texture is restored to the
texture it had before the action completed.
https://developer.apple.com/reference/spritekit/skaction/1417810-animate
Tell iOS to not resize SKNode animated texture?
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
.
changing texture of a skspritenode without changing size
Use SKAction.setTexture(bomba3)
instead of SKAction.setTexture(bomba3, resize: false))
.
SKSpriteNode animation where each frame in the animation is not centered
You should be able to try also:
Focus your attention to resize = true
If true, the sprite is resized to match each new texture. If false, the size of the sprite remains at a constant size.
If you need more official details take a look here.
About your request to the comments below you can re-create SKAction.animate
for example make an extension like this:
extension SKAction
{
static func animate(withMyTextures textures:[texture:SKTexture], timePerFrame:TimeInterval ,resize:Bool, restore:Bool) ->SKAction {
var originalTexture : SKTexture!
let duration = timePerFrame * Double(textures.count)
return SKAction.customAction(withDuration: duration)
{
node,elapsedTime in
guard let spriteNode = node as? SKSpriteNode
else
{
assert(false,"animateWithMyTextures only works on members of SKSpriteNode")
return
}
let index = Int((elapsedTime / CGFloat(duration)) * CGFloat(textures.count))
//If we havent assigned this yet, lets assign it now
if originalTexture == nil
{
originalTexture = spriteNode.texture
}
if(index < textures.count)
{
spriteNode.texture = textures[index].texture
}
else if(restore)
{
spriteNode.texture = originalTexture
}
if(resize)
{
spriteNode.size = spriteNode.texture!.size()
}
}
}
}
How to resize a sprite after changing it's texture
Looks like the texture bug is back, but only sometimes depending on how you do your assets folder. I was playing around with this in Xcode 8.3.3 and was able to get it both working and not working depending on how it got configured. Looks like it is broken in Xcode 9 as well. As a work around, do node.size = node.texture!.size()
after you assign your node a new texture. Note: node
is the name of the sprite you want to change in size.
Related Topics
Crash When Running on Device After Second Launch
Given a Function Parameter of Type [Int]; Can It Be Constrained to Not Be Empty
How to Display an Int Without Commas
How to Pass/Get Core Data Context in Swiftui Mvvm Viewmodel
Codable/Decodable Should Decode Array with Strings
Dyld: Library Not Loaded, App Requires Afnetworking 2.0.0 But Provides Version 1.0.0
How to Display Current Time (Realtime) in iOS 14 Home Widget
How to Set a Function as Function Argument in Swift 3
Images Inaccessible from Asset Catalog in a Swiftui Framework
Hovering a Modelentity in Front of Arcamera
Text from [String] to a Label.Text Isn't Working the First Time
Openinmapswithlaunchoptions Not Working
Change a Dictionary's Key in Swift
Cursor Shifts to End on Edit of Formatted Decimal Textfield - Swift
Swift Error Comparing Two Arrays of Optionals
How Does Appdelegate.Swift Replace Appdelegate.H and Appdelegate.M in Xcode 6.3