1

sometimes in SpriteKit, the animation of frames does not work: there is a white rectangle, then an image from the atlas, and so on. When I stop and run again, the animation works with every frame. Would you know why this happen?

The frames are regular pngs in a folder, I add ".atlas" manually and drag it to XCode. Here is the code :

var ball : SKSpriteNode!
var ballTurn : [SKTexture]!
ball = SKSpriteNode(texture: ballTurn[0])
ball.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
addChild(ball)

turningBall(ball, textures: ballTurn, str:"ballTurn")

func turningBall(theNode: SKSpriteNode, textures: [SKTexture], str:String) {
    theNode.removeAllActions()
    theNode.runAction(SKAction.repeatActionForever(
        SKAction.animateWithTextures(textures,
            timePerFrame: 0.05,
            resize: false,
            restore: true)),
        withKey:str)
}
//the method to generate the frames from Blender
func generatorFrames(atlas:SKTextureAtlas, str:String) -> [SKTexture]{
    var frames = [SKTexture]()
    let count = atlas.textureNames.count
    for var i=0; i<count; i++ {
        var theName = ""
        if i < 10 {
            theName = str+"000\(i)"
        } else {
            theName = str+"00\(i)"
        }
        frames.append(atlas.textureNamed(theName))
    }
    return frames
}
Paul
  • 6,108
  • 14
  • 72
  • 128
  • Are you sure that ballTurn has any texture inside it? From your code I can't see where you fill that array before accessing its elements. Another thing you should do is to reset Simulator ... You can do that with iOS Simulator -> Reset Contents and Settings. If this happens on an actual device, delete an app from it and run it again. Let me know if this helps and if you need a code about how to properly fill the array with textures... – Whirlwind Sep 24 '15 at 13:08
  • @Whirlwind thanks, yes I edited my post, I have the frames, the problem is one out of 10 times, the animation is replaced by a white rectangle. I don't understand why. I will try to delete the app and see what it does, but I am not sure this would solve the problem ;) Maybe a bug? – Paul Sep 24 '15 at 15:32
  • If you see white rectangle instead of an actual image, maybe this can help : http://stackoverflow.com/q/32746081/3402095 Otherwise if you see an white rectangle but with red X, which is missingResource image, then you should check image names and as I said, deleting app can help. Also, you can edit your question to explain in detail how you add/remove images from atlas. If that is done wrongly, you can get unexpected issues. – Whirlwind Sep 24 '15 at 15:37
  • Note that this line : ball = SKSpriteNode(texture: ballTurn[0]) will generate fatal error : "unexpectedly found nil while unwrapping an Optional value" You have to populate ballTurn array first in order to use it. – Whirlwind Sep 24 '15 at 15:42

0 Answers0