BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

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

            }
        }
    }

   

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
}

1      

Save 50% in my Black Friday sale.

SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.