gpt4 book ai didi

ios - Cocos2d View 在UISplitViewController中大小不正确

转载 作者:行者123 更新时间:2023-12-01 16:56:46 24 4
gpt4 key购买 nike

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

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

以下是所有相关文件:

Game1ViewController.h

#import
#import“cocos2d / cocos2d.h”

@class Game1Scene;

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

@property(非原子,保留)Game1Scene * scene;

@结束

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界限];
// 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);
}
返回矩形
}

-(void)startCocos2D {

CCDirector *导演= [CCDirector sharedDirector];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
NSLog(@“\ n ========== Size =%f,%f \ n \ n”,screenSize.height,screenSize.width);

//在iPhone 4上启用高分辨率模式(Retina显示屏),并在所有其他设备上保持低分辨率
if(![director enableRetinaDisplay:YES])
CCLOG(@“不支持视网膜显示”);

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

CGRect rotationBounds = [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节点];
[场景addChild:self.scene];
[导演runWithScene:scene];

}

//加载视图后,通常通过笔尖实现viewDidLoad进行其他设置。
-(void)viewDidLoad {
self.title = @“玩Game1”;
// self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// self.view.autoresizingMask = 1;
[super viewDidLoad];

[self startCocos2D];
}

-(BOOL)应该AutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//返回YES;
返回(interfaceOrientation == UIInterfaceOrientationPortrait);

}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation持续时间:(NSTimeInterval)duration
{
CGRect rotationBounds = [self getRotatedBounds:toInterfaceOrientation];

CCDirector *导演= [CCDirector sharedDirector];
CCGLView * glView = [导演视图];
float contentScaleFactor = [director contentScaleFactor];

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

[self.scene setBounds:rotatedBounds];
}

-(void)stopCocos2D {

CCDirector *导演= [CCDirector sharedDirector];

CCGLView * view = [导演视图];
[view removeFromSuperview];
// [导演分离];

[director stopAnimation];
[导演暂停];
// [director setOpenGLView:nil];

//杀死导演
[导演端];
}

-(void)viewDidDisappear:(BOOL)动画{
[self stopCocos2D];
[super viewDidDisappear:animated];
}
@结束

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( private )

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

@结束

@implementation Game1Scene

+(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_ =界限;
[self createUI];
}

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

bounds_ =界限;

[self startGame];
}
返回自我
}

-(void)createUI {

[self removeAllChildrenWithCleanup:TRUE];

//纯深紫色背景
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];
}

@结束

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;

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

浮点数newScaleX = newWidth / startWidth;
float newScaleY = newHeight / startHeight;

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

}
@结束

我采用了这个问题所解释的想法: UISplitViewController on iPad with Storyboards通过在DetailViews中创建几个Cocos2D场景。

[更新]

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

最佳答案

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

这是有关Splitviewcontrollers with Storyboards.的教程

关于ios - Cocos2d View 在UISplitViewController中大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11024624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com