How do i simply change a sprite's image in cocos2d?

后端 未结 7 1300
逝去的感伤
逝去的感伤 2021-02-14 18:28

I\'ve tried

[[CCTextureCache sharedTextureCache] addImage: @\"still.png\"];

But I always end up with a distorted image for some reason. It\'s m

7条回答
  •  太阳男子
    2021-02-14 19:02

    You would just call the sprite.texture function.

    Example

    In your init method:

    CCTexture2D *tex1 = [[CCTextureCache sharedTextureCache] addImage:@"still.png"];
    CCTexture2D *tex2 = [[CCTextureCache sharedTextureCache] addImage:@"anotherImage.png"];
    CCSprite *sprite = [CCSprite spriteWithTexture:tex1];
    //position the sprite
    [self addChild:sprite];
    

    Then to change the image of the sprite to tex2:

    sprite.texture = tex2;
    

    Very simple!

    Hope this helped!

提交回复
热议问题