致命错误:为类使用未实现的初始化程序'init(size :)' [英] fatal error: use of unimplemented initializer 'init(size:)' for class

查看:190
本文介绍了致命错误:为类使用未实现的初始化程序'init(size :)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在不同的设备上测试我的应用程序,并意识到精灵运动非常不一致(在某些设备上运行速度比其他设备快得多)。我发现这篇文章并遵循说明并从我的所有 SKScene 中删除​​了尺寸参数然后我收到错误:

I was testing my app on different devices and realized the sprite movements were quite inconsistent (running considerably faster on some devices as compared to others). I found this post and followed the instructions and removed the size parameters from all my SKScenes then I got the error:

fatal error: use of unimplemented initializer 'init(size:)' for class 'SuperGame.PosterScene'

请参阅下面的 PosterScene 类以及调用它的 GameViewController 类。

Please see my PosterScene class below and the GameViewController class in which it is called.

PosterScene

PosterScene

class PosterScene: SKScene {

 override init(){
    super.init()

    let posterImage = SKSpriteNode(imageNamed: "poster")
    posterImage.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    self.addChild(posterImage)

    let sequence = SKAction.sequence([  SKAction.wait(forDuration: 3.0), SKAction.run({ self.changeToMainMenuScene() }) ])

    self.run(sequence)

}

func changeToMainMenuScene  ()  {

    let mainMenuScene = MainMenuScene()
    self.view!.presentScene(mainMenuScene)

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

GameViewController:

GameViewController:

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

}

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    let skView = self.view as? SKView

    if skView?.scene == nil  {
        skView?.showsFPS = true
        skView?.showsNodeCount = true
        skView?.showsPhysics = true
        skView?.ignoresSiblingOrder = false

        //starting the game with the Poster Scene
        let posterScene = PosterScene()
        posterScene.scaleMode = .resizeFill
        skView?.presentScene(posterScene)
    }

}

override var shouldAutorotate : Bool {
    return true
}

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
    if UIDevice.current.userInterfaceIdiom == .phone {
        return UIInterfaceOrientationMask.allButUpsideDown
    } else {
        return UIInterfaceOrientationMask.all
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override var prefersStatusBarHidden : Bool {
    return true
}

required init(coder aDecoder: NSCoder!) {
    super.init(coder: aDecoder)!
}
}


推荐答案

你请让我看看这个问题。

You have asked me to take a look at this question.

你的游戏在各种设备上都不一致,因为在你的GameViewController中你将场景比例模式设置为.resizeFill。
在大多数情况下,默认的.aspectFill设置是最佳选择。

You game does not feel consistent across devices because in your GameViewController you set scene scale mode to .resizeFill. In most cases the default .aspectFill setting is the best option.

关于错误消息,当你不使用xCode级别编辑器时,你需要在init方法中给场景一个大小,例如

In regards to the error message you need to give the scene a size in the init method when you are not using the xCode level editor e.g

let scene = GameScene(size: CGSize(width: ..., height: ...))

基本上有两个尺寸选项

1)将场景大小设置为iPad,就是我个人所做的。这也是Apple在DemoBots中使用的内容以及xCode 7中的默认设置。因此场景大小为1024x768(横向)或768x1024(纵向)。

1) Set the scene size to iPad, which is what I personally do. This is also what Apple uses in DemoBots and what was the default setting in xCode 7. So scene size is either 1024x768 (landscape) or 768x1024 (portrait).

我设计考虑到iPhone的游戏区域,只需在iPad上显示更多背景,通常是地面和天空。这就是我喜欢玩的很多热门游戏,例如Modern Combat 5,Limbo,Altos Adventure,Leos Fortune,The Line Zen,Tower Dash。

I design my game area with the iPhone in mind, and simply show some more background, usually ground and sky, on iPads. This is what a lot of popular games I like to play do e.g. Modern Combat 5, Limbo, Altos Adventure, Leos Fortune, The Line Zen, Tower Dash.

2)将场景大小设置为iPhone并在iPhone上显示更多背景在iPad上,例如卢米诺市。
因此场景大小将是xCode 8使用的,1334x750(横向)或750x1334(纵向)。

2) Set the scene size to iPhones and show more background on iPhones and less on iPads e.g. Lumino City. So scene size would be what xCode 8 uses, either 1334x750 (landscape) or 750x1334 (portrait).

这样你的游戏应该在所有设备上保持一致。您可能要做的唯一事情是在iPad和iPhone之间调整一些UI,如按钮定位。

This way your game should feel consistent across all devices. The only thing you might have to do is tweak some UI, like button positioning, between iPad and iPhone.

希望这会有所帮助

这篇关于致命错误:为类使用未实现的初始化程序'init(size :)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆