如何将CCSprite转换为CCTexture2d

时间:2012-02-29 21:35:22

标签: objective-c cocos2d-iphone box2d box2d-iphone

我正在使用cocos2d创建一个iPhone应用程序,我有一个CCSprite,我需要将它转换为CCTexture2d所以我可以将它放入box2d。这是我到目前为止的代码,它崩溃了运行时错误...

    //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];

2 个答案:

答案 0 :(得分:5)

您可以从图像中获取纹理和精灵:

    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];

答案 1 :(得分:0)

下一个代码是如何获取您想要的值的示例:

Texture2D texture2d = Texture.XNATexture;

(Texture 是一个 cocos2d Texture,意思是一个类“CCTexture2D”)