重播按钮不起作用。 (Swift,SpriteKit)

时间:2015-08-03 21:30:56

标签: iphone swift sprite-kit

一旦我的游戏结束,它会显示一个重播按钮,但我无法理解如何在游戏结束后让Xcode知道是否有触摸。我的重播按钮的代码在didBeginContact方法中,如下所示:

func didBeginContact(contact: SKPhysicsContact) {
       if (.....) {
            ..........
      }  else {
         replayButton = SKSpriteNode(imageNamed: "ReplayButton")
            replayButton.position = CGPoint(x: size.width / 1.75, y: size.height / 2.5)
            replayButton.name = "replayButton"
            self.addChild(replayButton)
       }

New Swift文件:

class button: SKSprideNode {

let replayButton = button(imageNamed: "replayButton")


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

var touch = touches as!  Set<UITouch>
var location = touch.first!.locationInNode(self)
var node = self.nodeAtPoint(location)


if (node.name == "replayButton") {

    let myScene = EMode(size: self.size)
    let reveal = SKTransition.fadeWithDuration(2.0)
    self.scene!.view(myScene, transition: reveal)  //error on this line
    }
  }
}

1 个答案:

答案 0 :(得分:1)

您需要将重播按钮的用户互动设置为true,如下所示:

replayButton.userInteractionEnabled = true

然后你只需要听一下触摸按钮或场景的时间。一种方法是将SKSpriteNode的子类称为Button或其他内容,并覆盖touchesBegan方法。

实施例

<强> ButtonClass

class ExampleButton: SKSpriteNode {
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        //do stuff here
    }
}

按钮初始化

let button = ExampleButton(imageNamed: "ButtonImage")
button.userInteractionEnabled = true