请帮助我了解以下行为(iOS Sprite Kit)。
以下代码的输出是:
1.skView.bounds.size = 768.00,1024.00
2.skView.bounds.size = 1024.00,768.00
如上所示,宽度和高度在两种方法之间切换,这导致我的第二个场景不是以正确的比例显示。
我的游戏只能在横向模式下运行,这意味着事实上,第二个宽度x高度比是正确的(尽管它是第一个以正确的宽高比渲染场景,这本身就是一个谜对我来说。
有谁能告诉我如何解决这个问题?我做错了什么?
- (void)viewDidLoad
{
[super viewDidLoad];
self.currentGameState = GS_INTRO_SCENE;
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
printf("1.skView.bounds.size = %.2f, %.2f\n", skView.bounds.size.width, skView.bounds.size.height);
SKScene *scene = [SKTGameIntroScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.currentGameState == GS_INTRO_SCENE) {
self.currentGameState = GS_GAME_PLAY;
SKView * skView = (SKView *)self.view;
printf("2.skView.bounds.size = %.2f, %.2f\n", skView.bounds.size.width, skView.bounds.size.height);
SKScene *nextScene = [SKTMyScene sceneWithSize:skView.bounds.size];
nextScene.scaleMode = SKSceneScaleModeAspectFill;
SKTransition *fadeIn = [SKTransition fadeWithDuration:5.0f];
[skView presentScene:nextScene transition:fadeIn];
}
}
我提前非常感谢。
**编辑:**
@giorashc解决了我的问题,建议我将场景启动移动到 - (void)viewWillLayoutSubviews方法。
但我的第二个场景仍然拉伸....这是更改后SKTViewController.m的完整代码(有人可以说为什么第二个视图仍然被拉伸?):
//
// SKTViewController.m
// SpriteKitTest
//
// Created by Claudia Dazcal on 13/07/14.
// Copyright (c) 2014 DazcalFamily_inc. All rights reserved.
//
#import "SKTViewController.h"
#import "SKTMyScene.h"
#include "SKTGameIntroScene.h"
typedef enum
{
GS_INTRO_SCENE,
GS_GAME_PLAY
}GameStates;
@interface SKTViewController ()
@property GameStates currentGameState;
@property BOOL firstSceneLoaded;
@end
@implementation SKTViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.currentGameState = GS_INTRO_SCENE;
self.firstSceneLoaded = NO;
}
-(void)viewWillLayoutSubviews
{
if (!self.firstSceneLoaded) {
self.firstSceneLoaded = YES;
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
printf("1.skView.bounds.size = %.2f, %.2f\n", skView.bounds.size.width, skView.bounds.size.height);
SKScene *scene = [SKTGameIntroScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.currentGameState == GS_INTRO_SCENE) {
self.currentGameState = GS_GAME_PLAY;
SKView * skView = (SKView *)self.view;
printf("2.skView.bounds.size = %.2f, %.2f\n", skView.bounds.size.width, skView.bounds.size.height);
//CGSize DebugSize = CGSizeMake(skView.bounds.size.height, skView.bounds.size.width);
//SKScene *nextScene = [SKTMyScene sceneWithSize:DebugSize];
SKScene *nextScene = [SKTMyScene sceneWithSize:skView.bounds.size];
nextScene.scaleMode = SKSceneScaleModeAspectFill;
//SKTransition *fadeIn = [SKTransition fadeWithDuration:5.0f];
//[skView presentScene:nextScene transition:fadeIn];
[skView presentScene:nextScene];
}
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskAll;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
@end
*编辑2 **
好的,现在全部整理好了。
@giorashc在他建议将场景启动移动到viewWillLayoutSubviews时立即解决了我的问题。事实证明,由于我之前使用的是错误的边界,因此我使用错误的值设置原始场景,现在它已修复,看起来很拉伸。
但它现在已经解决了。非常感谢@giorashc !!
答案 0 :(得分:2)
使用viewWillLayoutSubviews
方法初始化场景。在此方法中,视图边界正确更新。
确保只在初始化场景一次,因为只要视图控制器呈现另一个视图控制器或应用程序正在更改其方向,就会调用此方法。
答案 1 :(得分:1)
在第一种方法中,视图尚未旋转到横向模式。在第二个中它已经旋转到横向模式,所以现在报告的边界比它更高。