swift类没有成员函数

时间:2016-09-13 08:57:41

标签: swift function class variables parameters

我创建了这个函数sayHello,每4秒调用一次。代码是:

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

}
func sayHello(Enem: SKSpriteNode) {
}

但是当我向这个函数添加变量Enem时:

 _ = 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)

我在行

上收到错误

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

它说类型游戏场景没有成员sayHello。有人可以解释为什么现在发生这种情况,因为我很困惑。

1 个答案:

答案 0 :(得分:3)

您需要更改选择器语法,您的函数没有参数,因此您需要像这样编写选择器。

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