RjC
RjC

Reputation: 827

Swift / Xcode: How to call a function from within another function in a new .swift file?

I have created a simple file called GameplayMechanics.swift (with Spritekit imported) and created a test function called checkSpeed().

All I want to do is call this function from within another function my Gameplay.swift file, but I don't understand why it doesn't work.

GameplayMechanics.swift:

import SpriteKit

func checkSpeedIncrease() {
    print("Checked")
}

Gameplay.swift:

import SpriteKit

class GameScene: SKScene {

...
...

func checkCounter(number: Int) {
        if number == 10 {
            GameplayMechanics.checkSpeed()
        } else if counter > 10 {
            counter = 0
        }
    }
}

I am talking about the checkCounter() function which should call checkSpeed(). The rest is just for illustration included.

Upvotes: 1

Views: 588

Answers (1)

donnywals
donnywals

Reputation: 7591

The way you set this up makes checkSpeed a free function. The way you're calling it makes the compiler expect an object of type GameplayMechanics that has a static method checkSpeed.

In your case, all you need to do is call the function like this:

if number == 10 {
    checkSpeed()
}

Upvotes: 2

Related Questions