본문 바로가기

Dev/Cocos2D

cocos2D 2.x 텍스쳐 생성(+스프라이트)

    int width = 320;

    int height = 5;

    

    int textureWidth = ccNextPOT(width);

    int textureHeight = ccNextPOT(height);

    

    unsigned int size = textureWidth * textureHeight * sizeof(ccColor4B);

    ccColor4B * pixels = (ccColor4B *)malloc(size);

    memset(pixels, 0, width * height * sizeof(uint));

    

    for ( int y=0; y<height; ++y ) {

        for ( int x=0; x<width; ++x ) {

            int index = y * textureWidth + x;

            pixels[index] = ccc4(60, 60, 60, 190);

        }

    }

    

    CCTexture2D *textureOfBar = [[CCTexture2D alloc] initWithData:pixels pixelFormat:kCCTexture2DPixelFormat_RGBA8888

                                                       pixelsWide:ccNextPOT(width) pixelsHigh:ccNextPOT(height) contentSize:CGSizeMake(width, height)];

    CCSprite *bar = [CCSprite spriteWithTexture:textureOfBar];

    [bar setAnchorPoint:ccp(0,1)];

    [bar setPosition:ccp(0,gameplayLayer.winSize.height)];

    [self addChild:bar];



좀 쓸데없이 복잡한감이 없지 않아 있지만.. 어쨌든 성공 -,.-;;

참고url :Manually creating pixel data for a CCTexture2D results in a black sprite