0

Whenever I try to use a NSTimer using the target of self an error pops up saying

argument type 'NSObject-> () -> GameScene' does not conform to expected type 'AnyObject'.

Just to be on the safe side I also tried adding in AnyObject instead of self:

class GameScene: SKScene {

    var point = SKSpriteNode(imageNamed: "Point")
    override func didMoveToView(view: SKView) {

    }

    var timer = NSTimer.scheduledTimerWithTimeInterval(0.0001, target: self, selector: ("stroke"), userInfo: nil, repeats: true)

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in touches {
            let location = touch.locationInNode(self)
            func stroke(){
                point.position = CGPoint(x: location.x, y: location.y)
                self.addChild(point)
            }
        }
    }
}
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • where are you calling the function from? Is it derived from a UIViewController class? – Christian Abella Feb 17 '16 at 01:20
  • Edit your question with more information. What do you want? What do you need? What happened? More code for example. – James Feb 17 '16 at 01:25
  • I added the code but I dont see anything out of the ordinary about it it's just a simple timer used to add sprites. – ryan clothier Feb 17 '16 at 01:40
  • 2
    You cannot use `self` in the *initialization* of a property, so this is essentially the same problem as in http://stackoverflow.com/questions/25855137/viewcontroller-type-does-not-have-a-member-named: Move the initialization to a method. – Martin R Feb 17 '16 at 01:52
  • 2
    Side note: the effective resolution of NSTimer is 50-100 ms. A timer will never fire every 0.0001 seconds. – Martin R Feb 17 '16 at 01:55

1 Answers1

3

I think the problem is you call the function during declaration of the variable and it is likely that the self or object is not yet created and that is the reason why you are getting the error.

Move the function call inside your constructor or a function that is called during the initialisation and it should be fine.

var timer : NSTimer!
...
...
func someFunc()
{
  timer = NSTimer.scheduledTimerWithTimeInterval(0.0001, target: self, selector: ("stroke"), userInfo: nil, repeats: true)
}
Christian Abella
  • 5,747
  • 2
  • 30
  • 42