2

Currently working on an application that requires a little bit of animations through an array of images. I've done tons of research online on how to resolve my issue but couldn't run into anything for Swift 4. Below is my code, it shows the first picture in the loop when I run the app but no animation at all. Everything looks fine, I don't see a problem with my code but maybe you guys can help. Appreciate it in advanced!

let atlas = SKTextureAtlas(named: “mypic”)
var TextureArray = [SKTexture]()
var person = SKSpriteNode()

    override func didMove(to view: SKView) {
        person = SKSpriteNode(imageNamed: "red_1.png")
        person.size = CGSize(width: 150, height: 129)
        person.position = CGPoint(x: 0, y: 0)
        person = SKSpriteNode(imageNamed: atlas.textureNames[0])

        for i in 1...atlas.textureNames.count {
            let Name = "red_\(i).png"
            TextureArray.append(SKTexture(imageNamed: Name))
        }

        self.addChild(person)
    }

    override func update(_ currentTime: TimeInterval) {
        let myAnimation = SKAction.animate(with: TextureArray, timePerFrame: 0.1)
        person.run(SKAction.repeatForever(myAnimation))
    }
Dewan
  • 429
  • 3
  • 16

1 Answers1

3

The animation action is placed in update, which is executed once every frame. So if the game runs at 60 FPS, update gets called 60 times in one second. This means that every second person gets 60 new myAnimation actions that it needs to run.

To fix this, consider placing the animation action somewhere else in your code, e.g. right after adding the person to the scene. Since this is a repeatForever action, the animation will run as you intended until either the action is removed from the node, or the node is removed from the scene.

Hope this helps!

JohnV
  • 981
  • 2
  • 8
  • 18
  • Wow simple mistake huh! I truly appreciate your help brotha – Dewan Sep 15 '17 at 15:36
  • Quick question, you see how I have "person.position = CGPoint(x: 0, y: 0)" how come when I do "person.position = CGPoint(x: 100, y: 100)" it doesn't move position? – Dewan Sep 16 '17 at 02:44
  • @Dewan I'm not sure what the problem could be with moving of the position. Maybe post the code where you are changing the position and I will try to help. – JohnV Sep 16 '17 at 15:27
  • I tried to do "person.position = CGPoint(x: 0.5, y: 0.7)" but it still stays in the (0,0) position – Dewan Sep 16 '17 at 16:02
  • @Dewan I would suggest posting this as a new question with the exact code where you are changing the position. Also, a CGPoint(x: 0.5, y: 0.7) is so small that you won't notice any change in position. – JohnV Sep 16 '17 at 16:29
  • What numbers would make sense for the bottom center of the screen if you don't mind me asking? I'll try that first and if it doesn't work then I'll write up a new thread on this situation. – Dewan Sep 16 '17 at 16:32
  • @Dewan Try CGPoint(x: 0, y: -self.height / 2) But this is assuming that the scene's anchor point is CGPoint(x: 0.5, y: 0.5). That's why I suggest you post all the code because the node's actual location will depend on the anchor point of its parent node (in this case your scene). – JohnV Sep 16 '17 at 16:56
  • Oh I don't have any anchorpoint anywhere in my code. It's my first time working with SpriteKit honestly – Dewan Sep 16 '17 at 19:14