SpriteKit中的场景

时间:2016-03-11 14:10:30

标签: ios swift sprite-kit

你好我在我的游戏中工作,但是当我试图改变场景时我遇到了问题。我按了一个按钮就可以了,但是当转移发生时,我看不到我的背景这是我的代码 这是我的游戏场景的代码。背景好了

import SpriteKit

class GameScene: SKScene {
    // Next scene button
    var nextButton: UIButton!
    let background = SKSpriteNode(imageNamed: "BG")


    override func didMoveToView(view: SKView) {
        backgroundColor = SKColor.whiteColor()

        // Background Image
        background.position = CGPoint(x: size.width / 2, y: size.height / 2)
        background.size = CGSize(width: 2100, height: 1200)
        background.zPosition = -3
        addChild(background)


        // Next button Action
        nextButton = UIButton(frame: CGRect(x: 0, y: 0, width: frame.size.width / 2, height:300))
        nextButton.center = CGPoint(x: view.frame.size.width / 7.3, y: view.frame.size.width / 3)
        nextButton.setTitle("Next Button", forState: UIControlState.Normal)
        nextButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        nextButton.addTarget(self, action: Selector("Next"), forControlEvents: UIControlEvents.TouchUpInside)
        self.view?.addSubview(nextButton)

    }

    func Next() {
        self.view?.presentScene(Play(), transition: SKTransition.crossFadeWithDuration(1.0))
        nextButton.removeFromSuperview()
        background.removeFromParent()

    }
}

这是我的Play场景的代码。背景没有出现我不知道为什么

  import Foundation
import SpriteKit

class Play: SKScene {

    override func didMoveToView(view: SKView) {
        backgroundColor = SKColor.whiteColor()
        // Background Image
        let background = SKSpriteNode(imageNamed: "BG")
        background.position = CGPoint(x: size.width / 2, y: size.height / 2)
        //background.size = CGSize(width: 2100, height: 1200)
        background.zPosition = -3
        addChild(background)

    }
}

1 个答案:

答案 0 :(得分:0)

您是否正在检查游戏场景是否实际转换过来了?。

如果不使用.sks文件,也应该使用大小初始化新场景。

func Next() {
    nextButton.removeFromSuperview()

    let scene = Play(size: self.size) // get size of current scene
    self.view?.presentScene(scene, transition: SKTransition.crossFadeWithDuration(1.0))
}

如果你使用.sks文件(xCode可视化编辑器),你需要创建一个名为Play的新.sks文件,然后加载场景

 func Next() {
    if let scene = Play(fileNamed: "Play") {
         nextButton.removeFromSuperview()
         self.view?.presentScene(scene, transition: SKTransition.crossFadeWithDuration(1.0))

     }
  }

作为提示,您不必在更改场景后删除SKSpriteNodes。您已经将它们添加到场景中(addChild),它们将自动被删除。 你仍然需要移除UIKit的东西,例如你的UIButton,因为它们被添加到你的视图而不是SKScene。

相关问题