gpt4 book ai didi

ios - SpriteKit 中的场景

转载 作者:行者123 更新时间:2023-11-30 13:37:15 25 4
gpt4 key购买 nike

你好,我正在我的游戏中工作,但当我尝试更改场景时遇到问题。我按下一个按钮,它可以工作,但是当传输发生时,我看不到我的背景,这是我的代码这是我的游戏场景的代码。背景还可以

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)

}
}

最佳答案

您是否检查过游戏场景实际上也在转换?

如果您不使用 .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,因为它们会添加到您的 View 中,而不是 SKScene 中。

关于ios - SpriteKit 中的场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35942374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com