Basic question about sprite animation

After several years as a registered developer, I'm finally creating my first app. It's a "Space Invaders" style top-down shooter. I'm using Swift 4 and Xcode 9.2. Everything is working out fine so far, but I'm having a problem animating with a simple texture atlas. Most of the tutorials I've seen put the animation code in the "didMove(to view: )" function in the "GameScene.swift." However, my player is set up as a separate Swift file (see image below) that passes necessary functions into the "didMove(to view: )". Everything works fine EXCEPT the animation. All my player sprite needs to do is animate four sprites in a texture atlas. The ship is the first thing I've tried to animate. All the names and spelling are correct. I get NO error messages, but the code doesn't work. I've staired at this thing so long that even the code I'm sure of looks weird. I know it's pretty basic, but I need some help. Most of my code knowledge was acquired via YouTube, and tutorial sites. Depending on the date of the tutorials, there seem to be number of ways to do things. I'm looking for "simple." What you see below is the result of trial and error. Like I said, it works very well EXCEPT for the animation.

ONE OTHER THING TO NOTE: the image (mainShip1) used to define the constant, "ship" in line 12 is NOT part of the texture atlas. I had to place a duplicate outside the atlas in order to be recognized (See image below). If not, I just get a big "missing sprite X." I THINK that's part of the problem. It's almost like the atlas isn't being referenced.

HERE ARE THE PERTINENT SECTIONS OF CODE:


For the player position, physics, and sprite animation:

class Ship: SKSpriteNode {
   
    var goLeft: Bool = false
    var goRight: Bool = false
    let mySpeed: CGFloat = 700
    var canShoot = true
    let fireRate = 0.5
    let bulletTexture = SKTexture(imageNamed: "testBullet")
   

     public static func newInstance() -> Ship {
        let ship = Ship(imageNamed: "mainShip1")
      
        ship.position = CGPoint(x: 0, y: -300)
        ship.size = CGSize(width: 128, height: 128)
        ship.anchorPoint = CGPoint(x: 0.5, y: 0.5)
      
      
        ship.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(32))
        ship.physicsBody?.isDynamic = true
        ship.physicsBody?.affectedByGravity = false
        ship.physicsBody?.categoryBitMask = shipCategory
        ship.physicsBody?.contactTestBitMask = spiderCategory
        ship.physicsBody?.collisionBitMask = shipCategory
      
        return ship
    }
  
    func shipAnim(){
    
        let atlas = SKTextureAtlas(named: "ship")
        let s1 = atlas.textureNamed("mainShip1.png")
        let s2 = atlas.textureNamed("mainShip2.png")
        let s3 = atlas.textureNamed("mainShip3.png")
        let s4 = atlas.textureNamed("mainShip2.png")
          
        let textures = [s1, s2, s3, s4]
        let shipAnim = SKAction.animate(with: textures, timePerFrame: 0.1)
          
        run(SKAction.repeatForever(shipAnim))
      
    }



For GameScene:

class GameScene: SKScene, SKPhysicsContactDelegate {

    var lastFrameTime: Double = 0
    let player = Ship.newInstance()
    let base = SKSpriteNode(imageNamed: "base")
    let fireButton = SKSpriteNode(imageNamed: "fireButton")
    let leftArrow = SKSpriteNode(imageNamed: "leftArrow")
    let rightArrow = SKSpriteNode(imageNamed: "rightArrow")
    let panel = SKSpriteNode(imageNamed: "panel2")
    let statPanel = SKSpriteNode(imageNamed: "Ceiling")
    let floor = SKSpriteNode(imageNamed: "Floor1")
    let mine = SKSpriteNode(imageNamed: "oreToMine")


    let nest = spiderNest()
    let stats = statController()
    let ship = Ship()

    override func didMove(to view: SKView) {
        setupControls()
        addChild(player)
        createBulletRemoveLine()
        stats.setupScore(scene: self)
        stats.setupLives(scene: self)
        stats.setupOre(scene: self)
        ship.shipAnim()
     
        self.physicsWorld.contactDelegate = self
     
    }


Hopefully someone can tell me what I'm doing wrong.


Thanks!

Replies

My screenshots didn't come through. Hopefully the code is all that's needed. 🙂