dynamically change image of srpite

  • abhishek
    Likes 0

    Problem Description

    i think this is a simple question, but i have not been able to find a proper answer on google.

    if i have a SPRITE say Sprite *sticker which is created as follows:

    sticker = Sprite::create("sticker_1.png");

    this->addChild(sticker);

     

    now i have 10 different stickers and every time i launch the app, i want to show a random sticker out of the 10 i have

    so how should i replace the sprite image after generating a random number?

     

    i am using this method right now and it even works, but it tells me that the code i m using is deprecated

    so i want to know if there is any new way to do this?

    thanks

     

    CODE:

    int temp = 1 + arc4random() % 10;

    sticker->setTexture(CCTextureCache::sharedTextureCache()->addImage(CCString::createWithFormat("sticker_%d.png", temp)->getCString()));


  • Sonar Systems admin
    Likes 0

    Try

    sticker->setTexture(TextureCache::sharedTextureCache()->addImage(String::createWithFormat("sticker_%d.png", temp)->getCString()));

     
  • abhishek
    Likes 0

    i tried using the line you suggested, but i get the same warning..
    SharedTextureCache is the part that has been DEPRECATED, i also tried using getInstance but even that is being showed as deprecated !!

     

    sticker->setTexture(CCTextureCache::sharedTextureCache()->addImage(CCString::createWithFormat("sticker_%d.png", temp)->getCString()));

    sticker->setTexture(TextureCache::sharedTextureCache()->addImage(String::createWithFormat("sticker_%d.png", temp)->getCString()));

  • Sonar Systems admin
    Likes 0

    Try this

     

    sticker->setTexture(Director::getInstance()->getTextureCache()->addImage(String::createWithFormat("sticker_%d.png", temp)->getCString()));

     
  • abhishek
    Likes 0

    thank you so much,
    the above line works and gives no warnings..
    thank you again
    :)

  • Sonar Systems admin
    Likes 0

    Great to hear, could you please mark this Question as solved.

Login to reply