1

I am making an iPhone app using cocos2d and I have a CCSprite and I need to convert it to a CCTexture2d so I can put it into box2d. Here is the code I have so far, It crashes with a runtime error...

    //Set up sprite
    // Use batch node. Faster
    CCSprite *parent = [CCSpriteBatchNode batchNodeWithFile:@"egg.png" capacity:100];

    eggSprite_ = [CCSprite spriteWithTexture:[parent texture]];

    [eggSprite_ setScaleX: .5/eggSprite_.contentSize.width];
    [eggSprite_ setScaleY: .5/eggSprite_.contentSize.height];


    [eggSprite_ setTexture:eggTexture_];

    [self addChild:parent z:0 tag:kTagParentNode];
King Popsicle
  • 465
  • 5
  • 17
  • You can get CCTexture from your CCSprite object.Like this way CCTexture2D *x1=[sprite1 texture]; – banu Mar 01 '12 at 04:14
  • i prefer to avoid using [CCSprite spriteWithTexture:[mySprite rexture]]. because your mySprite can use only some part of texture(for example, if your sprites are packed to one atlas), and -texture method will return the whole texture. – Morion Mar 01 '12 at 09:14
  • Remember to accept answers to your questions if they provided the needed solution. – johnbakers Aug 12 '12 at 11:00

2 Answers2

5

You can get a texture and sprite from an image like this:

    NSString* file = @"myImage.png";
CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
CGSize texSize = texture.contentSize;
CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect];
johnbakers
  • 24,158
  • 24
  • 130
  • 258
0

the next code is an example of how to get the value what you want:

Texture2D texture2d = Texture.XNATexture;

(Texture is a cocos2d Texture that is means a class "CCTexture2D")