Information About Using

Asked 2 years ago, Updated 2 years ago, 86 views

vara=3
letb = SKAction.moveByX(-100,y:0.0,duration:2)

What should I do if I want to include variable a in the value of duration?

swift spritekit

2022-09-30 10:28

1 Answers

Does this mean that an error occurred and it didn't work?

vara=3
letb = SKAction.moveByX(-100,y:0.0, duration:a)

This is the signature of Swift.
+moveByX:y:duration:

class func moveByX(_deltaX:CGFloat,
                 y deltaY —CGFloat,
          duration sec:NSTimeInterval) - >SKAction

The argument given to duration is of type NSTimeInterval, so declare the variable itself as NSTimeInterval or

vara:NSTimeInterval=3
letb = SKAction.moveByX(-100,y:0.0, duration:a)

Or you have to cast it and adjust the pattern.

vara=3//<- In this way a is Int type.
letb = SKAction.moveByX(-100, y: 0.0, duration: NSTimeInterval(a))

(NSTimeInterval is Double's typealias in Swift, so the above model name can be Double.)

If it is different from what you would like to hear, please let us know by correcting the comments or questions.
As for how to write questions in general, if there are examples that don't work, including code and error messages in those cases is more likely to give you a better answer, so try to include them as much as possible.


2022-09-30 10:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.