Arthur Macpherson Arthur Macpherson - 2 months ago 30
Swift Question

swift type class has no member function

I have created this function sayHello which is called every 4 seconds. The code for this is:

_ = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: #selector(GameScene.sayHello(_:)), userInfo: nil, repeats: true)

}
func sayHello(Enem: SKSpriteNode) {
}


However when i add a variable Enem to this function:

_ = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: #selector(GameScene.sayHello(_:)), userInfo: nil, repeats: true)
sayHello()

}
func sayHello() {

let array = [Enemy(imageNamed: "Slide 2"),Enemy(imageNamed: "Slide 1"),Enemy(imageNamed: "Slide 3"),Enemy(imageNamed: "Slide 4"),Enemy(imageNamed: "Slide 5"),Enemy(imageNamed: "Slide 6"),Enemy(imageNamed: "Slide 7"),Enemy(imageNamed: "Slide 8"),Enemy(imageNamed: "Slide 9"),Enemy(imageNamed: "Slide 10")]
var level0 = Enemy(imageNamed: "Slide 1")
let level2 = SKSpriteNode(imageNamed: "Slide 2")
let level3 = SKSpriteNode(imageNamed: "Slide 3")
let level4 = SKSpriteNode(imageNamed: "Slide 4")
let level5 = SKSpriteNode(imageNamed: "Slide 5")
let level6 = SKSpriteNode(imageNamed: "Slide 6")
let level7 = SKSpriteNode(imageNamed: "Slide 7")
let level8 = SKSpriteNode(imageNamed: "Slide 8")
let level9 = SKSpriteNode(imageNamed: "Slide 9")
let level10 = SKSpriteNode(imageNamed: "Slide 10")
var touchLocation = CGPoint?()
var slide = arc4random_uniform(10)
let level1 = Int(arc4random_uniform(UInt32(array.count)))
let Enem = array[level1]
//moveEnem(Enem)
(string: "\(level1)")

print("sayhdkl")
Enem.position = CGPoint(x: 540, y: 320 )
Enem.physicsBody = SKPhysicsBody(texture: Enem.texture!, size: Enem.size)
Enem.physicsBody?.dynamic = true
Enem.physicsBody?.categoryBitMask = BodyType.level1
Enem.physicsBody?.collisionBitMask = 0
Enem.zPosition = 12
addChild(Enem)


I get an error on the line

_ = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: #selector(GameScene.sayHello(_:)), userInfo: nil, repeats: true)


It says Type game scene has no member sayHello. Can someone explain why this now happens as i am very confused.

Answer

You need to change your selector syntax, your function has no parameter so you need to write selector like this.

NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: #selector(GameScene.sayHello()), userInfo: nil, repeats: true)