Astrum
Astrum

Reputation: 375

Can't call function from other class

So I am trying to call a function from my score class that will add one to the score:

Score.addOneToScore(1)

but I end up getting this error:

cannot convert value of type 'Int' to expected argument type 'Score'

I'm pretty sure i've overlooked something but i can't seem to see it.

Here is my score class:

import Foundation
import SpriteKit
import UIKit


class Score: SKLabelNode {

var number = 0

init (num: Int) {

    super.init()

    fontColor = UIColor.whiteColor()
    fontName = "Helvetica"
    fontSize = 75.0


    number = num
    text = "\(num)"

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func addOneToScore() {

    number++
    text = "\(number)"

  }

}

Upvotes: 0

Views: 84

Answers (1)

Greg
Greg

Reputation: 25459

There are few mistake you make: You are trying to call addOneToScore() as a class (static) function but it's an instance one, and you are passing the parameter but the function doesn't take one, try this:

let score = Score(num: 0) // Initialise score with the value of 1
score.addOneToScore() // add one to the score
print("\(score.number)") // print the value (1)

Upvotes: 3

Related Questions