TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: DiveIntoSpriteKit - gameTimer - Cannot find 'createEnemy' in scope

Forums > Books

@tehv  

Hello,

I am working through DiveIntoSpriteKit and run into a probably minor thing but can not solve it. When I set gameTimer I get the error Cannot find 'createEnemy' in scope. I expect it to be the wrong place but could not figure out where the right one is. What do I do wrong?

import SpriteKit

@objcMembers

class GameScene: SKScene {

    let player = SKSpriteNode(imageNamed: "player-rocket.png")
    var touchingPlayer = false
    var gameTimer: Timer?

    override func didMove(to view: SKView) {

        let background = SKSpriteNode(imageNamed: "space.jpg")
        background.zPosition = -1
        addChild(background)

        if let particles = SKEmitterNode(fileNamed: "SpaceDust") {
            particles.advanceSimulationTime(10)
            particles.position.x = 512
            addChild(particles)
        }

        player.position.x = -400
        player.zPosition = 1
        addChild(player)

        gameTimer = Timer.scheduledTimer(timeInterval: 0.35, target: self, selector: #selector(createEnemy), userInfo: nil, repeats: true)

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){

    guard let touch = touches.first else { return }
    let location = touch.location(in: self)
    let tappedNodes = nodes(at: location)
        if tappedNodes.contains(player) {
            touchingPlayer = true
        }

    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard touchingPlayer else { return }
        guard let touch = touches.first else { return }
        let location = touch.location(in: self)
        player.position = location
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

        touchingPlayer = false

    }

    override func update(_ currentTime: TimeInterval) {

        func createEnemy() {

            let sprite = SKSpriteNode(imageNamed: "asteroid")
            sprite.position = CGPoint(x: 1200, y: Int.random(in: -350...350))
            sprite.name = "enemy"
            sprite.zPosition = 1
            addChild(sprite)

            sprite.physicsBody = SKPhysicsBody(texture: sprite.texture!, size: sprite.size)

            sprite.physicsBody?.velocity = CGVector(dx: -500, dy: 0)

            sprite.physicsBody?.linearDamping = 0

            }
        }
    }

3      

You placed the createEnemy function inside the update function. That makes createEnemy part of the update function and not the GameScene class. When you call createEnemy in the following line of code:

gameTimer = Timer.scheduledTimer(timeInterval: 0.35, target: self, selector: #selector(createEnemy), userInfo: nil, repeats: true)

The code can't find createEnemy because it's not a part of GameScene, causing a compiler error.

The fix is to move the createEnemy function outside the update function.

override func update(_ currentTime: TimeInterval) {

}

func createEnemy() {
    // Function code omitted
}

4      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.