Play Again按钮工作错误

时间:2016-02-26 16:49:17

标签: xcode swift uibutton sprite-kit

我在游戏中再次播放按钮,我使用 SKTransition 重新启动游戏。当我触摸再次播放按钮时,会出现新的场景。但是这个场景的对象比最后一个场景要大。场景被冻结了。当我再次触摸播放按钮时,我无法玩游戏。我该怎么办?这是关于按钮和重启场景的所有代码:

let playAgain: UIButton = UIButton(frame: CGRectMake(10, 0, 450, 1000))

class GameScene: SKScene {
func buttonPressed(sender: UIButton) {
    let pransition = SKTransition.fadeWithDuration(1.0) //create a transition
    let gamePlayScene = GameScene(fileNamed: "GameScene") // create a new scene from your file (let variableName = NameOfGameFile(fileNamed: "NameOfGameFile")
    self.view?.presentScene(gamePlayScene!, transition: pransition) // present the scene with the transition
}



override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch:AnyObject in touches{...



    if(..){...}
    else{
            playAgain.setTitle("Play Again", forState: UIControlState.Normal)
            playAgain.titleLabel!.font = UIFont(name: "Helvetica", size: 50)

            playAgain.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
            playAgain.tag = 1
            self.view!.addSubview(playAgain)

1 个答案:

答案 0 :(得分:0)

您的问题是当您通过播放按钮创建到游戏场景的过渡时,您没有设置场景大小。我只是插入你的代码,它做你说的。在swift中,最好在创建过渡时设置场景的大小和宽高比,让我们回到游戏中,或者从游戏到最终场景,如获胜场景或松散场景等。在声明游戏场景常量后添加场景宽高比,如下面的代码块:

let pransition = SKTransition.fadeWithDuration(1.0)
    let gamePlayScene = GameScene(fileNamed: "GameScene")
    gamePlayScene!.scaleMode = SKSceneScaleMode.AspectFill
    self.view?.presentScene(gamePlayScene!, transition: transition)
    self.playAgain.removeFromSuperview()

我通常喜欢为游戏的戏剧性方面创建一个等待一两秒,所以我使用以下与你所做的非常相似但在场景被解雇之前创造了一点延迟,以下是什么我使用,如果你愿意,随时改变它。

let wait = SKAction.waitForDuration(1.5)
    let theGameScene = GameScene(size: self.size)
    theGameScene.scaleMode = SKSceneScaleMode.AspectFill
    let theTransition = SKTransition.fadeWithDuration(1.0)
    let changeScene = SKAction.runBlock{
        self.scene?.view?.presentScene(theGameScene, transition: theTransition)
    }

    let sequence = SKAction.sequence([wait, changeScene])
    self.runAction(SKAction.repeatAction(sequence, count: 1))
祝你好运并继续编码并提问。