1
votes

I am new to the Swift language and i am following a tutorial that is using a outdated swift language. The tutorial is about creating a animation with png files. So there are 6 png files that in total creates a animation. This is the link of the tutorial "https://www.youtube.com/watch?v=5aCVyrJbrHQ". I have noticed a crucial difference in the old version of Swift and the new one. Here is where i get stuck.

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let shooterNode = self.childNodeWithName("shooterNode")

    if(shooterNode != nil) {
        let animation = SKAction.animateWithTextures(shooterAnimation, timePerFrame: 0.1)
        shooterNode?.runAction(animation)
    }

New version: override func touchesBegan(touches: Set, withEvent event: UIEvent?

Old version: override func touchesBegan(touches: NSSet, withEvent event: UIEvent

Error

As soon as i run the simulator and click on the object i get no response, no error just nothing. I checked if i made a typo with the object name but found nothing there.

This is the full code of the entire file in case you would ask for it

import UIKit
import SpriteKit

class ShooterScene: SKScene {

    var score = 0
    var enemyCount = 10
    var shooterAnimation = [SKTexture]()

    override func didMoveToView(view: SKView) {
        self.initShooterScene()
    }

    func initShooterScene(){
        let shooterAtlas = SKTextureAtlas(named: "shooter")

        for index in 1...shooterAtlas.textureNames.count {
            let imgName = String(format: "shooter%01d", index)
            shooterAnimation += [shooterAtlas.textureNamed(imgName)]

        }
    }
        //Anime the shooter
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let shooterNode = self.childNodeWithName("shooterNode")

        if(shooterNode != nil) {
            let animation = SKAction.animateWithTextures(shooterAnimation, timePerFrame: 0.1)
            shooterNode?.runAction(animation)
        }
    }
}

GameSwift.scene

import SpriteKit

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {


    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let introLabel = childNodeWithName("introLabel")

        if(introLabel != nil) {
            let fadeOut = SKAction.fadeOutWithDuration(1.5)

            introLabel?.runAction(fadeOut, completion: {
                let doors = SKTransition.doorwayWithDuration(1.5)
                let shooterScene = SKScene(fileNamed: "ShooterScene")
                self.view?.presentScene(shooterScene!, transition: doors)

            })

        }
    }

     /* Called before each frame is rendered */
    override func update(currentTime: CFTimeInterval) {
          }
}
1
Have you checked if every method have been executed as it should. Put some print statements in each method, and find out if everything is called correctly. That would be a start. Also put a print statement inside of if(shooterNode != nil) {...} block.Whirlwind
@Whirlwind Thanks for the respond, I added print on every function in the file and nothing appeared in the console. So i came to conclusion that there is something with the connection between my ShooterScene.sks and ShooterScene.swift file.C.B.
Where do you present ShooterScene ? The actual creation of a new scene (and its transition) should look like this if let nextScene = ShooterScene(fileNamed: "ShooterScene") {/* present scene here with optional transition*/} So check that part.Whirlwind
@Whirlwind the Shooterscene.swift represents 6 Gif files from 1 to 6 that in total creates an animation. I have put all these files in a .atlas map as you can see i put it in the "func initShooterScene" and then i recall the node character name in ("func touchesBegan") called "shooterNode". I dont want a transition that i already have in my GameScene.swift file. I want my ShooterScene.swift play the gif files from 1 to 6 that is related to my object "shooterNode" in ShooterScene.sksC.B.
You are missing the point here ... I asked where do you present your ShooterScene. You have to present it, before you use it. According to that tutorial, presenting of the ShootingScene scene (as well as transition) is done in GameScene's touchesBegan. So, again, where do you present your ShooterScene ?Whirlwind

1 Answers

2
votes

It should be :

let shooterScene = ShooterScene(fileNamed: "ShooterScene")

rather than:

let shooterScene = SKScene(fileNamed: "ShooterScene")