【问题标题】:Cocos2d View not sizing properly in UISplitViewControllerCocos2d View 在 UISplitViewController 中没有正确调整大小
【发布时间】:2012-06-16 23:39:50
【问题描述】:

我有一个 cocos2d 2 视图作为 UISplitViewController 中的详细视图。出于某种原因,它调整视图的大小,就好像它具有整个屏幕一样,无论是最初还是旋转屏幕时。以下是一些虚构的背景:

这是我在模拟器中运行它时的样子:

以下是所有相关文件:

Game1ViewController.h

#import #import "cocos2d/cocos2d.h" @类游戏1场景; @interface Game1ViewController : UIViewController { // Game1Scene *_scene; } @property (nonatomic, 保留) Game1Scene *scene; @结尾

Game1ViewController.m

#import "cocos2d/cocos2d.h" #import "Game1Scene.h" #import "Game1ViewController.h" #import "CCSpriteExtensions.h" @implementation Game1ViewController @synthesize 场景 = _scene; - (CGRect)getRotatedBounds:(UIInterfaceOrientation)toInterfaceOrientation { CGRect screenRect = [self.view bounds]; // NSLog(@"%f, %f\n", [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height - 88); CGRect 矩形 = CGRectZero; if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 矩形 = 屏幕矩形; } else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { rect.size = CGSizeMake(screenRect.size.height, screenRect.size.width); } 返回矩形; } - (void)startCocos2D { CCDirector *director = [CCDirector sharedDirector]; CGSize screenSize = [[CCDirector sharedDirector] winSize]; NSLog(@"\n===========Size=%f, %f\n\n",screenSize.height, screenSize.width); // 在 iPhone 4 上启用高分辨率模式(视网膜显示屏)并在所有其他设备上保持低分辨率 if( ! [导演 enableRetinaDisplay:YES] ) CCLOG(@"不支持视网膜显示"); NSLog(@"self.view bounds width, height: %f %f", [self.view bounds].size.width, [self.view bounds].size.height); CGRect rotateBounds = [self getRotatedBounds:[UIApplication sharedApplication].statusBarOrientation]; CCGLView *glview = [ CCGLView viewWithFrame:rotatedBounds pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8 depthFormat:0 // GL_DEPTH_COMPONENT16_OES ]; [self.view addSubview:glview]; [导演 setView:glview]; [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888]; self.scene = [Game1Scene nodeWithBounds:rotatedBounds]; CCScene *scene = [CCScene 节点]; [场景添加子:self.scene]; [导演 runWithScene:scene]; } // 实现 viewDidLoad 以在加载视图后进行额外的设置,通常来自 nib。 - (void)viewDidLoad { self.title = @"玩游戏 1"; // self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // self.view.autoresizingMask = 1; [超级视图DidLoad]; [自启动Cocos2D]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // 返回是; return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 持续时间:(NSTimeInterval)duration { CGRect rotateBounds = [self getRotatedBounds: toInterfaceOrientation]; CCDirector *director = [CCDirector sharedDirector]; CCGLView *glView = [导演视图]; float contentScaleFactor = [导演 contentScaleFactor]; 如果(内容比例因子!= 1){ rotateBounds.size.width *= contentScaleFactor; rotateBounds.size.height *= contentScaleFactor; } glView.frame = 旋转边界; [self.scene setBounds:rotatedBounds]; } - (void)stopCocos2D { CCDirector *director = [CCDirector sharedDirector]; CCGLView *view = [导演视图]; [查看 removeFromSuperview]; //[导演分离]; [导演stopAnimation]; [导演暂停]; //[导演setOpenGLView:nil]; //杀死导演 [导演结束]; } - (void)viewDidDisappear:(BOOL)animated { [自停Cocos2D]; [超级 viewDidDisappear:动画]; } @结尾

Game1Scene.h

#import #import "cocos2d/cocos2d.h" @interface Game1Scene : CCLayer { CGRect bounds_; } + (id) nodeWithBounds:(CGRect)bounds; - (void)setBounds:(CGRect)bounds; @结尾

Game1Scene.m

#import "Game1Scene.h" #import "CCSpriteExtensions.h" #import "cocos2d/CCMenuItem.h" @interface Game1Scene(私人) - (无效)开始游戏; - (void)createUI; - (id)initWithBounds:(CGRect)bounds; @结尾 @实现游戏1场景 +(id) nodeWithBounds:(CGRect)bounds { 返回 [[self alloc] initWithBounds:bounds]; } - (void) setBounds:(CGRect)bounds { NSLog(@"bounds.width, bounds.height: %f, %f", bounds.size.width, bounds.size.height); bounds_ = 界限; [自我创建UI]; } -(id) initWithBounds:(CGRect)bounds { 如果((自我=[超级初始化])){ srand (时间(NULL)); bounds_ = 界限; [自启动游戏]; } 回归自我; } -(无效)createUI { [自我 removeAllChildrenWithCleanup:TRUE]; // 纯深紫色背景 CCSprite *background = [CCSprite spriteWithFile:@"game1-background.png"]; 背景.位置 = ccp(0, 0); 背景.anchorPoint = ccp(0, 0); NSLog(@"\n=== bounds= %f, %f\n", bounds_.size.width, bounds_.size.height); [背景resizeTo:CGSizeMake(bounds_.size.width, bounds_.size.height)]; [自我添加孩子:背景]; } -(无效)开始游戏{ [自我创建UI]; } @结尾

CCSpriteExtensions.h

/* CCSpriteExtensions.h http://www.learn-cocos2d.com/tag/ccspriteextensions/ */ #import "cocos2d/cocos2d.h" @interface CCSprite (Xtensions) // 通过正确设置比例因子来调整到指定的大小。 -(void)resizeTo:(CGSize) theSize; @结尾

CCSpriteExtension.m

/* CCSpriteExtensions.m 来源:http://www.learn-cocos2d.com/tag/ccspriteextensions/ */ #import "CCSpriteExtensions.h" @implementation CCSprite (Xtensions) -(void)resizeTo:(CGSize) theSize { CGFloat newWidth = theSize.width; CGFloat newHeight = theSize.height; 浮动 startWidth = self.contentSize.width; 浮动 startHeight = self.contentSize.height; 浮动 newScaleX = newWidth/startWidth; 浮动 newScaleY = newHeight/startHeight; self.scaleX = newScaleX; self.scaleY = newScaleY; } @结尾

我通过在 DetailViews 中创建几个 Cocos2D 场景来接受这个问题中解释的想法:UISplitViewController on iPad with Storyboards

[更新]

我更新到 2.0。我的问题听起来像这样:http://www.cocos2d-iphone.org/forum/topic/30797

【问题讨论】:

  • 只是仔细检查:上面的代码在细节视图控制器中,而不是在 UISplitViewController 的某些子类中,是吗?您的视图故事板是否有“想要全屏检查”的机会?
  • 没有检查。它是一个常规的 UISplitViewController,带有用于不同 DetailViews 的 Cocos2d 场景。
  • 首先,在Cocos2D 2.0中,使用CCGLView。然后如果你想使用Interface Builder,你可以这样连接它,去掉CCGLView初始化代码。试试看,也许也可以发布你的 CCDirector 初始化代码。
  • 另外,如果我上次评论中没有说清楚,你应该更新到 Cocos2D 2.0,这样会更容易。
  • 我提供了更多来源并提供了说明问题的图片。

标签: ios ios5 cocos2d-iphone uisplitviewcontroller


【解决方案1】:

我正在使用 cocos2d v2,我不确定 1.1,但在 v2 中,CCScene 和 CCLayer 正在初始化屏幕大小:

@implementation CCScene
-(id) init
{
    if( (self=[super init]) ) {
        CGSize s = [[CCDirector sharedDirector] winSize];
        self.ignoreAnchorPointForPosition = YES;
        anchorPoint_ = ccp(0.5f, 0.5f);
        [self setContentSize:s];
    }

    return self;
}
@end

...

 @implementation CCLayer

    #pragma mark Layer - Init

        -(id) init
        {
            if( (self=[super init]) ) {

                CGSize s = [[CCDirector sharedDirector] winSize];
                anchorPoint_ = ccp(0.5f, 0.5f);
                [self setContentSize:s];
                self.ignoreAnchorPointForPosition = YES;

                isTouchEnabled_ = NO;

        }

            return self;
        }

如果你想改变它,只需将 contentSize 属性设置为它。

【讨论】:

    【解决方案2】:

    您是否尝试过使用一些标准 UIView 来进行比较? 实现相同的 getRotatedBounds 但没有所有 Cocos2D 的东西。这样您就可以确定错误是出在 SplitViewController/Storyboards 上还是在您的 Cocos2D 处理中?

    这是Splitviewcontrollers with Storyboards.的教程

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      相关资源
      最近更新 更多