首页 文章

Cocos2d在UISplitViewController中查看未正确调整大小

提问于
浏览
2

我在UISplitViewController中有一个cocos2d 2视图作为Detail View . 出于某种原因,它将视图的大小调整为好像它有整个屏幕,无论是最初还是旋转屏幕时 . 这里有一些背景:

enter image description here

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

enter image description here

以下是所有相关文件:

Game1ViewController.h

#import <UIKit/UIKit.h>
        #import "cocos2d/cocos2d.h"

        @class Game1Scene;

        @interface Game1ViewController : UIViewController {
        //  Game1Scene *_scene;
        }

        @property (nonatomic, retain) Game1Scene *scene;

    @end

Game1ViewController.m

#import "cocos2d/cocos2d.h"
    #import "Game1Scene.h"
    #import "Game1ViewController.h"
    #import "CCSpriteExtensions.h"

    @implementation Game1ViewController
    @synthesize scene = _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 rect = CGRectZero;   

        if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {        
            rect = screenRect;
        } else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
        }
        return rect;
    }

    - (void)startCocos2D {

        CCDirector *director = [CCDirector sharedDirector];
        CGSize screenSize = [[CCDirector sharedDirector] winSize];
        NSLog(@"\n===========Size=%f, %f\n\n",screenSize.height, screenSize.width);

        // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
        if( ! [director enableRetinaDisplay:YES] )
            CCLOG(@"Retina Display Not supported");

        NSLog(@"self.view bounds width, height: %f %f", [self.view bounds].size.width, [self.view bounds].size.height);

        CGRect rotatedBounds = [self getRotatedBounds:[UIApplication sharedApplication].statusBarOrientation];

        CCGLView *glview  = [ CCGLView  viewWithFrame:rotatedBounds
                                          pixelFormat:kEAGLColorFormatRGB565    // kEAGLColorFormatRGBA8
                                          depthFormat:0                     // GL_DEPTH_COMPONENT16_OES
                             ];

        [self.view addSubview:glview];

        [director setView:glview];

        [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];

        self.scene = [Game1Scene nodeWithBounds:rotatedBounds];
        CCScene *scene = [CCScene node];
        [scene addChild: self.scene];
        [director runWithScene:scene];

    }

    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        self.title = @"Play Game1";
    //    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    //    self.view.autoresizingMask = 1;
        [super viewDidLoad];

        [self startCocos2D];
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    //    return YES;
        return (interfaceOrientation == UIInterfaceOrientationPortrait);

    }

    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        CGRect rotatedBounds = [self getRotatedBounds: toInterfaceOrientation];

        CCDirector *director = [CCDirector sharedDirector];
        CCGLView *glView = [director view];
        float contentScaleFactor = [director contentScaleFactor];

        if( contentScaleFactor != 1 ) {
            rotatedBounds.size.width *= contentScaleFactor;
            rotatedBounds.size.height *= contentScaleFactor;
        }
        glView.frame = rotatedBounds;

        [self.scene setBounds:rotatedBounds];
    }


    - (void)stopCocos2D {

        CCDirector *director = [CCDirector sharedDirector];

        CCGLView *view = [director view];
        [view removeFromSuperview];
        //[director detach];

        [director stopAnimation];
        [director pause];
        //[director setOpenGLView:nil];

        // kill the director
        [director end];
    }

    - (void)viewDidDisappear:(BOOL)animated {
        [self stopCocos2D];
        [super viewDidDisappear:animated];
    }
    @end

Game1Scene.h

#import <Foundation/Foundation.h>
    #import "cocos2d/cocos2d.h"

    @interface Game1Scene : CCLayer {
        CGRect bounds_;
    }

    + (id) nodeWithBounds:(CGRect)bounds;
    - (void)setBounds:(CGRect)bounds;
    @end

Game1Scene.m

#import "Game1Scene.h"
    #import "CCSpriteExtensions.h"
    #import "cocos2d/CCMenuItem.h"

    @interface Game1Scene (Private)

    - (void)startGame;
    - (void)createUI;
    - (id)initWithBounds:(CGRect)bounds;

    @end

    @implementation Game1Scene

    +(id) nodeWithBounds:(CGRect)bounds {
        return  [[self alloc] initWithBounds:bounds];
    }

    - (void) setBounds:(CGRect)bounds {
        NSLog(@"bounds.width, bounds.height: %f , %f", bounds.size.width, bounds.size.height);
        bounds_ = bounds;
        [self createUI];
    }

    -(id) initWithBounds:(CGRect)bounds {
        if( (self=[super init] )) {
            srand ( time(NULL) );

            bounds_ = bounds;

            [self startGame];
        }
        return self;
    }


    - (void) createUI {

        [self removeAllChildrenWithCleanup:TRUE];

        // Plain dark purple background
        CCSprite *background = [CCSprite spriteWithFile:@"game1-background.png"];
        background.position = ccp(0, 0);
        background.anchorPoint = ccp(0, 0);
        NSLog(@"\n=== bounds= %f, %f\n", bounds_.size.width, bounds_.size.height);
        [background resizeTo:CGSizeMake(bounds_.size.width, bounds_.size.height)];
        [self addChild:background];

    }

    - (void)startGame {
        [self createUI];
    }

    @end

CCSpriteExtensions.h

/*
     CCSpriteExtensions.h

     http://www.learn-cocos2d.com/tag/ccspriteextensions/
     */
    #import "cocos2d/cocos2d.h"

    @interface CCSprite (Xtensions)

    // Resize to the specified size by setting the scale factors correctly.
    -(void)resizeTo:(CGSize) theSize;

    @end

CCSpriteExtension.m

/*
     CCSpriteExtensions.m

     Source: 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;


        float startWidth = self.contentSize.width;
        float startHeight = self.contentSize.height;

        float newScaleX = newWidth/startWidth;
        float newScaleY = newHeight/startHeight;

        self.scaleX = newScaleX;
        self.scaleY = newScaleY;

    }
    @end

我在这个问题中解释了这个想法:UISplitViewController on iPad with Storyboards在DetailViews中创建了几个Cocos2D场景 .

[更新]

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

2 回答

  • 0

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

    这是关于Splitviewcontrollers with Storyboards.的教程

  • 0

    我正在使用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属性设置为它 .

相关问题