gpt4 book ai didi

iphone - 在单个 iPhone View 中使用 Cocos2d

转载 作者:行者123 更新时间:2023-12-03 19:42:39 27 4
gpt4 key购买 nike

我有一个主要使用标准控件的 iPhone 应用程序。有一种 View 是用 OpenGL 渲染的(花哨的图形)。为了节省自己的大量工作,我环顾四周,发现了 Cocos2d。看起来它正是我所需要的(OpenGL ES,使用起来相对简单),但我遇到了问题。

我不知道如何限制Cocos2d仅存在于单个 View 中。之前,我在 View 中实现了 OpenGL 内容,并使用简单的 ViewController 在导航 Controller 中呈现它。在这里,Cocos2d 似乎需要做更多的事情,而且我不确定如何将其填充到我使用原始 OpenGL 所做的相同 ViewController/View 方法中。

更新:这就是我正在做的事情。翻到下一个粗体,看看我现在在做什么。

#import <UIKit/UIKit.h>


@interface CocosController : UIViewController {
UIWindow *window;
}

@property (nonatomic,retain) UIWindow *window;

@end

还有我的 CocosController.m:

#import "CocosController.h"
#import "SomeScene.h"
#import "cocos2d.h"

@implementation CocosController

@synthesize window;

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
// Initialization code
CC_DIRECTOR_INIT();
CCDirector *director = [CCDirector sharedDirector];

//landscape
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
[director setDisplayFPS:true];

//turn on multi-touch
EAGLView *cocosView = [director openGLView];
[cocosView setMultipleTouchEnabled:true];

self.view = cocosView;

//default texture formats...
[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];

[[CCDirector sharedDirector] runWithScene:[SomeScene scene]];
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[[CCDirector sharedDirector] purgeCachedData];
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[[CCDirector sharedDirector] release];
[window release];
[super dealloc];
}


@end

最后,我如何尝试呈现它(通过单击按钮):

CocosController *cocosController = [[CocosController alloc] init];
[[self navigationController] pushViewController:cocosController animated:true];

这是新方法,但结果相同。

我的AppDelegate的ApplicationDidFinish部分:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeThreadMainLoop];

CCDirector *director = [CCDirector sharedDirector];
[director setDisplayFPS:YES];

state = kStateEnd;

// Add the navigation controller's view to the window and display.
[window addSubview:navigationController.view];
[window makeKeyAndVisible];

return YES;
}

我用来呈现 View 的 CocosController:

#import "CocosController.h"
#import "cocos2d.h"
#import "SomeScene.h"

@implementation CocosController

@synthesize mainView;

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
EAGLView *glview = [EAGLView viewWithFrame:CGRectMake(0, 0, 250,350)];
[mainView addSubview:glview];

CCDirector *director = [CCDirector sharedDirector];
[director setOpenGLView:glview];

CCScene *scene = [CCScene node];
id node = [SomeScene node];
[scene addChild: node];

[director runWithScene:scene];
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}

@end

这一点来 self 拥有的详细 View Controller 中的按钮单击,它应该使整个事情出现:

CocosController *cc = [[CocosController alloc]init];
cc.mainView = self.view;
[[self navigationController] pushViewController:cc animated:false];

最佳答案

你所做的事情似乎有些过分了。 CC_DIRECTOR_INIT(); 用于初始化 View 和 CCDirector,建议您将 cocos2d 附加到主视图。

如果您查看 Cocos2d 中提供的名为 AttachTest 的示例(请参阅文件 attachDemo.m),您可以了解如何附加 CCDirector 到任何 View 。以下是示例使用的代码:

EAGLView *glview = [EAGLView viewWithFrame:CGRectMake(0, 0, 250,350)];
[mainView addSubview:glview];

CCDirector *director = [CCDirector sharedDirector];
[director setOpenGLView:glview];

在此代码段中,创建了一个新的 EAGLView,然后将其设置为 CCDirectoropenGLView

我的建议是像往常一样创建 View Controller 和空 View (可能使用 Interface Builder,或上面的代码),然后在 loadView 方法中附加 CCDirector 到该 View 。

关于iphone - 在单个 iPhone View 中使用 Cocos2d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3144168/

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