1
votes

I have an Objective-C project. In the project, I want to use some CCAction on the image(UIImage). So, I think I need to change all UIImage into CCSprite. I have a function which add image to view for all images in the project:

UIImage* firstImage = [imageArray objectAtIndex:0];    
UIImageView* imageView = [[UIImageView alloc] initWithImage:firstImage];
[targetView addSubview:imageView];
[imageView release];
return imageView;

At this point, I don't know how to do addSubview on CCSprite. As I know, in cocos2d, only one CCGLView is using. Is there any hints? thank you.

1

1 Answers

2
votes

Some useful Notes for you:

« You can add one CCSprite to other CCSprite by using addChild method.

« Creating CCSprite from UIImage:

CCTexture2D *tex = [[[CCTexture2D alloc] initWithImage:uiImage] autorelease];
CCSprite *sprite = [CCSprite spriteWithTexture:tex];

« Creating UIImage from CCSprite:

- (UIImage *) imageFromSprite :(CCSprite *)sprite
{
    int tx = sprite.contentSize.width;
    int ty = sprite.contentSize.height;

    CCRenderTexture *renderer   = [CCRenderTexture renderTextureWithWidth:tx height:ty];

    sprite.anchorPoint  = CGPointZero;

    [renderer begin];
    [sprite visit];
    [renderer end];

    return [renderer getUIImage];
}
//call syntax
CCSprite *sprite = (CCSprite *)node;
UIImage *p = [self imageFromSprite:sprite]