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

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

以下是所有相关文件:
Game1ViewController.h
#import <UIKit/UIKit.h>
#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 <Foundation/Foundation.h>
#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;
}
@结尾
我采用了这个问题中解释的想法:UISplitViewController on iPad with Storyboards by create a couple of Cocos2D Scenes in the DetailViews。
[更新]
我更新到2.0。我的问题听起来像这样:http://www.cocos2d-iphone.org/forum/topic/30797