I'm working with swift and SpriteKit and I'm having an issue with what I believe is my GameScene being wider than my device's screen size. What I need to do is move an image and then not allow it to move past the edges of the screen.
我正在与swift和SpriteKit合作,我认为我的GameScene比我的设备的屏幕尺寸要宽。我需要做的是移动一个图像,然后不允许它移动到屏幕的边缘。
Where I'm at right now is that my image will collide with the top and bottom of the screen, but it will move off to the left or right and collide with the edge somewhere off screen.
我现在所处的位置是我的图像会与屏幕的顶部和底部发生碰撞,但它会向左右移动,并与屏幕外的边缘碰撞。
I came across this question in my searches to try and fix the problem: Screen's real size is bigger than what I see
我在搜索中遇到了这个问题,试图解决这个问题:屏幕的实际大小比我看到的要大
Just doing what they tried and commenting out the line that says scene.scaleMode = .AspectFill
will cause collisions with the edge of the screen to work properly, however like they stated, the image is now not scaled properly and in my case only takes up the middle leaving grey edges to each side.
只是做他们想做的,并注释掉这一行。scaleMode = . aspectfill将会导致与屏幕边缘的冲突正常工作,但是就像他们说的那样,图像现在没有适当的缩放,在我的例子中只占据了中间留出的灰色边缘。
Their other remedy was adding scene = GameScene(size: self.view.frame.size)
after taking out the default created scene in, I'm assuming, GameViewController.swift
but I wasn't sure what to remove to try doing that.
他们的另一个补救方法是在GameViewController中去掉默认创建的场景后添加scene = GameScene(size: self.view.frame.size)。斯威夫特:但是我不知道该删除什么。
My viewDidLoad
currently looks like this:
我的viewDidLoad现在看起来是这样的:
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
//skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
If someone could help by pointing me in the right direction for what to remove or what else to try that might fix this, I would greatly appreciate it.
如果有人能给我指明正确的方向,让我删除什么,或者尝试什么可以解决这个问题,我会非常感激。
Thanks in advance!
提前谢谢!
3 个解决方案
#1
2
Try to use viewWillLayoutSubviews
instead of viewDidLoad and initialize your scene with skView.bounds.size
:
尝试使用viewWillLayoutSubviews而不是viewDidLoad,并使用skview . boundas .size:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsPhysics = true
skView.showsDrawCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
if(skView.scene == nil){
scene.scaleMode = .AspectFill
scene.size = skView.bounds.size
skView.presentScene(scene)
}
}
}
In viewDidLoad
the final size of view may not be know yet and viewWillLayoutSubviews
is sometimes better place to initialize the scene.
在viewDidLoad中,最终大小的视图可能还不知道,viewWillLayoutSubviews有时更适合初始化场景。
When scene is loaded from .sks file it has default size of 1024x768. That's why your scene is sometimes wider than expected, and this can be changed like from the code I've posted.
当从.sks文件加载场景时,它的默认大小是1024x768。这就是为什么您的场景有时比预期的要宽,这可以从我发布的代码中进行更改。
If you have trouble with wrong view's size (but I doubt that) check your launch images...If those are wrongly selected, a view can have a wrong size.
如果你在错误的视图的尺寸上有问题(但是我怀疑)检查你的发射图片…如果这些错误被选择,视图可能会有错误的大小。
#2
0
Try:
试一试:
var scene = GameScene(size: UIScreen.mainScreen().bounds)
or:
或者:
var dev_w = UIScreen.mainScreen().bounds.width*2
var dev_h = UIScreen.mainScreen().bounds.height*2
var new_frame = CGRectMake(0 , 0, dev_w, dev_h)
var scene = GameScene(size: new_frame)
#3
0
I have had a similar problem with Objective-C iOS 7 and 8 in SpriteKit. When replacing a SKScene
with another, setting the next SKScene
size with self.size
would cause this exact error. My solution was as simple as:
我在SpriteKit中遇到过Objective-C ios7和8的类似问题。当用另一个替换SKScene时,用self设置下一个SKScene的大小。大小将导致这个确切的错误。我的解决办法很简单:
//ObjectiveC
MyScene *newScene = [[MyScene alloc] initWithSize:CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height * 2)];
newScene.scaleMode = SKSceneScaleModeAspectFill;
//Swift
var newScene : MyScene = MyScene(size:CGSize(width: self.view.frame.size.width * 2, height: self.view.frame.size.height))
newScene.scaleMode = .AspectFill
#1
2
Try to use viewWillLayoutSubviews
instead of viewDidLoad and initialize your scene with skView.bounds.size
:
尝试使用viewWillLayoutSubviews而不是viewDidLoad,并使用skview . boundas .size:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsPhysics = true
skView.showsDrawCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
if(skView.scene == nil){
scene.scaleMode = .AspectFill
scene.size = skView.bounds.size
skView.presentScene(scene)
}
}
}
In viewDidLoad
the final size of view may not be know yet and viewWillLayoutSubviews
is sometimes better place to initialize the scene.
在viewDidLoad中,最终大小的视图可能还不知道,viewWillLayoutSubviews有时更适合初始化场景。
When scene is loaded from .sks file it has default size of 1024x768. That's why your scene is sometimes wider than expected, and this can be changed like from the code I've posted.
当从.sks文件加载场景时,它的默认大小是1024x768。这就是为什么您的场景有时比预期的要宽,这可以从我发布的代码中进行更改。
If you have trouble with wrong view's size (but I doubt that) check your launch images...If those are wrongly selected, a view can have a wrong size.
如果你在错误的视图的尺寸上有问题(但是我怀疑)检查你的发射图片…如果这些错误被选择,视图可能会有错误的大小。
#2
0
Try:
试一试:
var scene = GameScene(size: UIScreen.mainScreen().bounds)
or:
或者:
var dev_w = UIScreen.mainScreen().bounds.width*2
var dev_h = UIScreen.mainScreen().bounds.height*2
var new_frame = CGRectMake(0 , 0, dev_w, dev_h)
var scene = GameScene(size: new_frame)
#3
0
I have had a similar problem with Objective-C iOS 7 and 8 in SpriteKit. When replacing a SKScene
with another, setting the next SKScene
size with self.size
would cause this exact error. My solution was as simple as:
我在SpriteKit中遇到过Objective-C ios7和8的类似问题。当用另一个替换SKScene时,用self设置下一个SKScene的大小。大小将导致这个确切的错误。我的解决办法很简单:
//ObjectiveC
MyScene *newScene = [[MyScene alloc] initWithSize:CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height * 2)];
newScene.scaleMode = SKSceneScaleModeAspectFill;
//Swift
var newScene : MyScene = MyScene(size:CGSize(width: self.view.frame.size.width * 2, height: self.view.frame.size.height))
newScene.scaleMode = .AspectFill