4

我试图了解在 Cocos2d 中使用 CCUIViewWrapper 与移植功能的优缺点。例如,在 CCUIViewWrapper 中使用 UITableView 会更好,还是使用 CCTableViewSuite。乍一看,我认为包装器是更好的方法,因为大概它允许我做 UITableView 提供的所有事情,但是我缺少关键细节吗?包装器是否存在严重的限制,无论是实际使用苹果 sdk 对象还是无法利用 Cocos2d 中带有像 CCTableView 这样的移植对象的某些功能?

4

2 回答 2

2

我从这里复制了这篇文章,可能有助于回答你的问题。我相信将 cocos2d 函数与 cocos2d 包装器一起使用会更好,但是您可以与其他人一起实现它,但它也没有集成。

如果有任何新手(比如我自己)需要额外帮助如何将 UIKit 项目与 cocos2d 集成,这可能会有所帮助。正如您在此线程的第一篇文章中所读到的那样,Blue Ether 编写了一个用于操作 UIViews 的包装器。什么是 UIView?查看http://developer.apple.com/iphone/library/documentation/uikit/reference/uikit_framework/Introduction/Introduction.html,向下滚动一点,您将看到不同 UIView 项的图表;UIButton、UISlider、UITextView、UILabel、UIAlertView、UITableView、UIWindow 等。其中有20多个。您可以使用 Blue Ethas 代码将它们中的任何一个包装在 cocos2d 中。在这里,我将包装一个 UIButton,但尝试使用您最喜欢的 UIView 项目选择。

  1. 创建一个新的 cocos 2d Application 项目。

  2. 创建一个新的 NSObject 文件并将其命名为 CCUIViewWrapper。这将为您提供文件 CCUIViewWrapper.h 和 CCUIViewWrapper.m。编辑(通过复制粘贴)它们,使它们看起来像 Blue Ether 定义的那样(参见此线程中的第一篇文章。

  3. 让你的 HelloWorld.h 看起来像这样

    //  HelloWorldLayer.h
    
    #import "cocos2d.h"
    
    #import <UIKit/UIKit.h>
    
    #import "CCUIViewWrapper.h"
    
    @interface HelloWorld : CCLayer
    
    {
        UIButton *button;
        CCUIViewWrapper *wrapper;
    }
    +(id) scene;
    @end
    

和你的 HelloWorld.m 像这样

//  HelloWorldLayer.m

     #import "HelloWorldScene.h"

// HelloWorld implementation
@implementation HelloWorld

+(id) scene
{
    CCScene *scene = [CCScene node];
    HelloWorld *layer = [HelloWorld node];
    [scene addChild: layer];
    return scene;
}

-(void)buttonTapped:(id)sender
{
    NSLog(@"buttonTapped");
}

// create and initialize a UIView item with the wrapper
-(void)addUIViewItem
{
    // create item programatically
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Touch Me" forState:UIControlStateNormal];
    button.frame = CGRectMake(0.0, 0.0, 120.0, 40.0);

    // put a wrappar around it
    wrapper = [CCUIViewWrapper wrapperForUIView:button];
    [self addChild:wrapper];
}

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

        // x=160=100+120/2, y=240=260-40/2
        wrapper.position = ccp(100,260);
        [wrapper runAction:[CCRotateTo actionWithDuration:4.5 angle:180]];
    }
    return self;
}

- (void) dealloc
{
    [self removeChild:wrapper cleanup:true];
    wrapper = nil;
    [button release];
    button=nil;
    [super dealloc];
}
@end

构建并运行。这应该在场景中间产生一个旋转按钮。您可以在按钮旋转时触摸它。它将在控制台中写入一条文本消息。

于 2010-11-20T20:59:09.980 回答
1

CCUIViewWrapper 不是一个好的解决方案。我已经尝试并删除了它。问题在于它将 UIKit 元素包装在一个容器中,并将其作为一个视图添加到 openGL 主视图上的 director。这样做的一个问题是您将无法在其上添加任何其他精灵。非常有限。

于 2012-06-02T13:57:30.427 回答