0

I try to create a class variable for later assign it a function, to later call this function just by using the variable. The idea is to be able to change the function pointed by the variable whe user touch differents buttons, and the call is made in an update loop function all functions i want assign in the variable is on the code because them contains a animation codes but different, and i don't want to use a switch, is more efficient to call a function as is made in Obj-C or C++ with pointer function

class declaration

private var animationFunction: (TimeInterval)

error on this line : Class 'MenuScene' has no initializers

try to assign function :

/**** interface ****/
func addInterface()
{
    // couleur de fond et taille de la vue de la pub
    self.backgroundColor = Constants.UI.colorsArray[appDelegate.theme][Constants.UI.C_0101100_COLOR_BACKGROUND]

    // animation
    animationFunction = standardAnimation
}

on line assignment i get this error : Cannot assign value of type '(TimeInterval) -> ()' (aka '(Double) -> ()') to type 'TimeInterval' (aka 'Double')

the function i want to assign into class variable

func standardAnimation(dtTime: TimeInterval)
{ do something }

Regards, Cedric

1 Answer 1

2

To solve the error

Class 'MenuScene' has no initializers

you have two options: change the declaration of animationFunction to make it an Optional or create a designated initializer for MenuScene where you assign a value to the non-Optional `animationFunction.

To solve the second error of incompatible types, you need to change the declaration of animationFunction since currently it's type is TimeInterval and not a function type. From the error message it seems you want a function of type (TimeInterval)->(), namely one that accepts a single input parameter of type TimeInterval and returns Void.

To fix both errors, simply modify the declaration of animationFunction to make it Optional and have the required function type:

private var animationFunction: ((TimeInterval)->())?
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thx a lot just change the declaration resolve all issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.