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

后端 未结 7 1298
逝去的感伤
逝去的感伤 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 18:44
    urSprite = [CCSprite spriteWithFile:@"one.png"];
    urSprite.position = ccp(240,160);
    [self urSprite z:5 tag:1];
    
    // Changing the image of the same sprite
    [urSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"two.png"]];
    
    0 讨论(0)
  • 2021-02-14 18:44

    In Cocos2d-x v3, you can use my_sprite->setTexture(sprite_path);

    0 讨论(0)
  • 2021-02-14 18:48

    This is the most straight forward way to change the image of a sprite(if you have it loaded trough a spritesheet) this definitely works (I use it all the time in my game). mySprite is the name of the sprite instance:

    [mySprite setDisplayFrame:
    [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"sprite1.png"] ];

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-02-14 19:05

    In cocos2d v3, I was able to accomplish this with...

    [mySprite setSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"two.png"]]
    

    ...but I have no idea if this has side effects that will hurt me in the long run. :)

    0 讨论(0)
  • 2021-02-14 19:05

    I use cocos2d 3.0 and this code works for me:

     [_mySprite setTexture:[CCTexture textureWithFile:@"myFile.png"]];
    
    0 讨论(0)
提交回复
热议问题