NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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

            }
        }
    }

   

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
}

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Reply to this topic…

You need to create an account or log in to reply.

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.