简体   繁体   English

SKView bounds.size在触摸事件上被反转(Sprite Kit)

[英]SKView bounds.size get inverted on touch event (Sprite Kit)

Please help me understand the following behavior (iOS Sprite Kit). 请帮助我了解以下行为(iOS Sprite Kit)。

The output of the code I present below is: 我在下面显示的代码输出为:

1.skView.bounds.size = 768.00, 1024.00 1.skView.bounds.size = 768.00,1024.00

2.skView.bounds.size = 1024.00, 768.00 2.skView.bounds.size = 1024.00,768.00

As seen above, width and height are switched between the two methods, and that causes my second scene to be presented not in the correct scale. 如上所示,宽度和高度在两种方法之间切换,这导致第二个场景的显示比例不正确。

My game will run in landscape mode only, which means that in fact, the second width x height ratio is the correct one (though it is the first that renders the scene in the correct aspect ratio, which in itself is a mystery to me). 我的游戏仅在横向模式下运行,这意味着实际上第二个宽度x高度比是正确的(尽管这是第一个以正确的纵横比呈现场景的方式,这本身对我来说是个谜) 。

Can anyone tell me how I can fix this behavior? 谁能告诉我如何解决此问题? What am I doing wrong? 我究竟做错了什么?

- (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];
    }
}

I greatly appreciate it in advance. 我非常感谢。

** edit: ** **编辑:**

@giorashc solved my problem suggesting I move the scene initiation to -(void)viewWillLayoutSubviews method. @giorashc解决了我的问题,提示我将场景初始化移至-(void)viewWillLayoutSubviews方法。

But my second scene remains stretched.... here is the full code for SKTViewController.m after the change (can anyone say why second view is still stretched?): 但是我的第二个场景仍然被拉伸。...这是更改后的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

* edit 2 ** *编辑2 **

Ok, it`s all sorted out now. 好吧,现在都整理好了。

@giorashc solved my problem right away when he suggested moving the scene initiation to viewWillLayoutSubviews. @giorashc建议将场景初始化移动到viewWillLayoutSubviews时,立即解决了我的问题。 It turns out that since I was previously working with the wrong bounds, I set up my original scene with the wrong values and now that it was fixed, it looked stretched. 事实证明,由于我以前使用的是错误的边界,所以我用错误的值设置了原始场景,而现在它已修复,因此看起来很拉伸。

But it`s fixed now. 但是现在已经修复。 Thank you so much @giorashc!! 非常感谢@giorashc!

Initialize your scene in the viewWillLayoutSubviews method. viewWillLayoutSubviews方法中初始化场景。 In this method the view bounds are updated correctly. 在这种方法中,视图边界会正确更新。

Make sure though that you initialize the scene only once as this method is called whenever the view controller presents another view controller or the application is changing its orientation. 确保确保仅将场景初始化一次,因为只要视图控制器提供另一个视图控制器或应用程序更改其方向,该方法就会被调用。

In the first method the view hasn't rotated to landscape mode yet. 在第一种方法中,视图尚未旋转到横向模式。 In the second it has rotated to landscape mode so is now reporting bounds that are wider than it is tall. 在第二秒中,它已旋转到横向模式,因此现在报告的边界比其高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM