2012-08-08 27 views
0

我有一個正常的UIKit應用程序,但我想用cocos2d做一些小工作。我想使用函數glReadPixels,因爲我需要一個cocos2d圖像 - > CCSprite。cocos2d - 在UIKit應用程序中創建CCSprite

我工作2天,現在對這個問題,因爲我得到這個錯誤:

2012-08-08 16:58:50.673 iBlaulicht 2[11801:16a03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' +[NSString stringWithCString:encoding:]: NULL cString' ** First throw call stack: (0x1f88022 0x1b9bcd6 0x1f30a48 0x1f309b9 0x16050c7 0x62687 0x62589 0x62317 0x86979 0x47c5c 0x2d63 0xdc5a1e 0x222d 0xcfc386 0xcfd274 0xd0c183 0xd0cc38 0xd00634 0x338cef5 0x1f5c195 0x1ec0ff2 0x1ebf8da 0x1ebed84 0x1ebec9b 0xcfcc65 0xcfe626 0x20cb 0x2065) terminate called throwing an exception(lldb)

,這是我的代碼:

- (id)initWithImage:(UIImage *)drawnImage{ 

    if (self == [super init]) { 



     CCTexture2D *tex = [[[CCTexture2D alloc] initWithImage:drawnImage resolutionType:kCCResolutionRetinaDisplay] autorelease]; 
     CCSprite *imageSprite = [CCSprite spriteWithTexture:tex]; 


     imageSprite.position = CGPointMake(0, 0); 
     [self addChild:imageSprite]; 



    } 


    return self; 
} 


- (NSArray *)determinePixels{ 


    for (int y_direction = 0; currentImage.size.height > y_direction; y_direction += 20) { 


     for (int x_direction = 0; currentImage.size.width > x_direction; x_direction += 20) { 

      Byte pixelColor[4]; 
      glReadPixels(x_direction, y_direction, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pixelColor[0]); 

      // Just to test 
      NSLog(@"0"); 
     } 
    } 

    return 0; 
} 

我感謝所有幫助...謝謝。 Chris

回答

1

看看Ray Wenderlich在這個話題上的帖子;

http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit

+0

我會檢查出來:) – 2012-08-08 16:10:53

+0

@ChristianPappenberger有沒有運氣? – 2012-08-08 16:44:44

+0

不是真的......你能否提供一些示例代碼。我真的只想在cocos2d上下文中只有一個圖像。我會很感激你的幫助;) – 2012-08-08 18:02:26

1

我找到了解決辦法嘍!

我添加了一個EAGLView,並使用CCDirector將其設置爲OpenGl View。

- (id)initWithImage:(UIImage *)drawnImage{ 

    if (self == [super init]) { 

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


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

     CCTexture2D *tex = [[CCTexture2D alloc] initWithImage:drawnImage resolutionType:kCCResolutionRetinaDisplay]; 
     imageSprite = [KKPixelMaskSprite spriteWithTexture:tex]; 


     imageSprite.position = CGPointMake(0, 0); 
     [self addChild:imageSprite]; 



    } 


    return self; 
}