2010-10-01 10 views
4

我想了解使用的東西CCUIViewWrapper在cocos2d與停靠的功能的利弊的利弊。例如,在CCUIViewWrapper中使用UITableView還是使用CCTableViewSuite會更好?乍一看,我認爲包裝是更好的方法,因爲它可以讓我做UITableView提供的所有東西,但是我錯過了關鍵細節嗎?包裝中是否存在嚴重的限制,無論是實際使用apple sdk對象還是無法利用Cocos2d中與CCTableView等移植對象一起提供的某些功能?優點和使用CCUIViewWrapper與停靠的功能

回答

2

我抄自here這個職位,可以幫助回答你的問題。我相信用cocos2d包裝器來使用cocos2d函數會更好,但是你可以用其他人來實現它,但它也不會被集成。

如果有需要額外的幫助如何使用cocos2d的一個UIKit的項目集成任何新手(像我一樣)在那裏,這可能幫助。正如你在這個線程的第一篇文章中讀到的,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個。您可以使用藍色Ethas代碼將它們中的任何一個包裝在cocos2d中。在這裏我將包裝一個UIButton,但試驗你最喜歡的UIView項目。

  1. 創建一個新的cocos 2d應用程序項目。

  2. 建立一個新的NSObject文件,並調用它CCUIViewWrapper。這會給你的文件CCUIViewWrapper.h和CCUIViewWrapper.m。編輯(通過複製粘貼)他們,使他們看起來像藍甲醚定義(請參閱第一篇文章在這個線程。

  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 

建立和運行。這應該在場景的中間產生旋轉按鈕,你可以觸摸按鈕同時轉動。它會寫在控制檯的文本消息。

1

CCUIViewWrapper是不是一個很好的解決方案。我試過並將其刪除。問題在於它將UIKit元素封裝在容器中,並將其作爲導向器上的視圖添加到openGL主視圖中。與此相關的一個問題是,您將無法在其上添加任何其他精靈。非常有限。

+0

嗨RubberDuck你喜歡什麼,而不是CCUIViewWrapper?謝謝 – yatanadam 2012-10-31 19:52:48

+0

最好的選擇是使用cocos創建自己的控件,而不是使用UIKIT。 – SpaceDog 2012-11-01 09:11:26