0

我有一个普通的 UIKit 应用程序,但我想用 cocos2d 做一些小工作。我想使用函数 glReadPixels,为此我需要一个 cocos2d 图像 -> CCSprite。

我现在在这个问题上工作了 2 天,因为我收到了这个错误:

2012-08-08 16:58:50.673 iBlaulicht 2[11801:16a03] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“+[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称为抛出异常(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;
}

我感谢任何帮助...谢谢。克里斯

4

2 回答 2

1

I found the solution myself !

I added a EAGLView and used a CCDirector to set it as 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;
}
于 2012-08-08T18:58:11.360 回答
1

看看 Ray Wenderlich 关于这个主题的帖子;

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

于 2012-08-08T15:37:28.547 回答