从另一个类调用GameScene

时间:2018-03-31 18:12:07

标签: swift3 sprite-kit

我的沟通有问题。 我想从一个类中找到一个节点。 把孩子绑在上面。

class GameScene: SKScene {

let buttonChange = SKSpriteNode(imageNamed: "ButtonRate")


override func didMove(to view: SKView) {

    backgroundColor = SKColor.magenta

    let myLabel = SKLabelNode(fontNamed:"Chalkduster")
    myLabel.text = "Game Scene"
    myLabel.fontSize = 45
    myLabel.position = CGPoint(x:860, y:540)


    self.addChild(myLabel)

    //add button
    buttonChange.position = CGPoint(x: frame.width/2, y: frame.height/2)
    buttonChange.zPosition = 1
    self.addChild(buttonChange)


    Add().test()

...........

class Add  {

let gameScene : GameScene! = nil

func test() {

    let PosSize = CGRect(x: 200, y: 100, width: 100, height: 100)
    let shape = SKShapeNode(rect: PosSize)
    shape.fillColor = .white
    //shape.fillColor = UIColor.clear  // one Color
    shape.name = "white"
    //shape.strokeColor = UIColor.clear // one Color
    shape.strokeColor = .white
    shape.lineWidth = 0.0 // Keine Umrandung
    shape.alpha = 0.5  // 1.0 Kein Alpha
    shape.zPosition = 10

    gameScene.buttonChange.addChild(shape) // >> Error!!
}

xcode因错误而中止: 线程1:致命错误:在解包可选值时意外发现nil

我做错了什么?

0 个答案:

没有答案