gpt4 book ai didi

ios - 将 UIView 转换为 SKView 时的程序集崩溃报告

转载 作者:可可西里 更新时间:2023-11-01 00:18:11 25 4
gpt4 key购买 nike

我正在用 SpriteKit 制作一个简单的游戏,我正在尝试使用 UIKit 制作一个主菜单。我为主菜单创建了一个 View Controller ,为游戏创建了一个单独的 View Controller 。每当按下播放按钮时,它都会切换到游戏 View Controller ,然后在其中呈现第一个场景。但是,每当它似乎试图将游戏 View Controller 的 UIView 转换到 SKView 以能够呈现场景时,都会出现故障,并且崩溃报告是程序集,我不知道如何调试。我相信它会两次实例化游戏 View Controller ,因为当我在 viewWillLayoutSubviews() 方法中放置一个 println() 时,它会打印两次。另一篇关于此的帖子说,在转换期间类型已经是 SKView,导致它失败,在这种情况下,这可能意味着当它触发两次时,它转换一次,然后尝试再次转换并失败。提前致谢!

这是快速代码

import UIKit
import SpriteKit

class MenuViewController: UIViewController {
let background = UIImageView(image: UIImage(named: "background.png"))
let titleImg = UIImageView(image: UIImage(named: "title.png"))

override func viewDidLoad() {
background.frame = self.view.frame
titleImg.frame = CGRectMake(0, 0, self.view.frame.width, 100)
self.view.addSubview(background)
self.view.addSubview(titleImg)
let bp = ButtonPositions(controller: self)
let play = button(bp.playY, named: "playButton.png")
let leader = button(bp.leaderY, named: "leaderButton.png")
let rate = button(bp.rateY, named: "rateButton.png")
play.addTarget(self, action: "playPressed", forControlEvents: UIControlEvents.TouchDown)
leader.addTarget(self, action: "leaderPressed", forControlEvents: UIControlEvents.TouchDown)
rate.addTarget(self, action: "ratePressed", forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(rate)
self.view.addSubview(leader)
self.view.addSubview(play)
}

func button(y:CGFloat, named name:String!) -> UIButton {
let bp = ButtonPositions(controller: self)
let img = UIImage(named: name)
let b = UIButton(frame: CGRectMake(bp.x, y, self.view.frame.width / 1.5, img.size.height))
b.setImage(img, forState: UIControlState.Normal)
return b
}

func playPressed() {
println("playPressed") //only prints once
changeVC()
}

func changeVC() {
self.presentViewController(GameViewController(), animated: true, completion: nil) //maybe initializing the GameViewController twice for some reason?
}

func leaderPressed() {

}

func ratePressed() {

}

func center() -> CGPoint {
return CGPoint(x: CGRectGetMidX(self.view.frame), y: CGRectGetMidY(self.view.frame))
}

override func prefersStatusBarHidden() -> Bool {
return true
}
}


class GameViewController: UIViewController {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
//prepareGame()
println("test") //this prints twice, even through the button press event is only triggered once
}

func prepareGame() {
let skView = self.view as SKView //crashes here when called
let scene = GameScene(size: self.view.bounds.size)
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)

}

override func shouldAutorotate() -> Bool {
return true
}

override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.toRaw())
} else {
return Int(UIInterfaceOrientationMask.All.toRaw())
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func prefersStatusBarHidden() -> Bool {
return false
}
}

最佳答案

我遇到了同样的问题。我使用 iOS 的默认 Swift Game 项目创建了我的项目,该项目默认为 SceneKit 创建了项目,因为我想使用 SpriteKit 并尝试将我的新场景转换到 SKView,它在我试图转换的那一行产生了 EXC_BAD_ACCESS。为了解决我的问题,我不得不将主 Storyboard中的类更改为 SKView,因为它默认为 SCNView。

我遵循的修复步骤:

  1. 选择主 Storyboard(在我的例子中是 Main.storyboard)
  2. 从 View Controller 下的层次结构中选择 View
  3. 选择身份检查器选项卡(右侧的实用程序面板)
  4. 在自定义类部分下,将类从 SCNView 更改为 SKView

执行此操作后,问题就消失了。

关于ios - 将 UIView 转换为 SKView 时的程序集崩溃报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25553415/

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