how to resize sprite if image has been changed OR create a sprite with image from a set of images

  • abhishek
    Likes 0

    Problem Description

    Hi,
    I have a specific requirement with creating and/or using sprites. This is what i need:
    ----------------------------------------------------------------------------------------------------
    OPTION 1
    I create a sprite with the code:
    sticker1 = Sprite::create("common/sticker_0.png"); // sticker_0.png is a 100 x 100 pixel image

    now i have more than 50 different stickers and each of them have different dimensions (100 x 100, 20 x 40, 40 x 45 whatever)
    so i want to change the image of sticker1 depending upon the value of myInt (myInt = 1 + arc4random() % 50)
    so to do this, i used the following code:

    sticker1 = Sprite::create("common/sticker_0.png");
    sticker1->setTexture(Director::getInstance()->getTextureCache()->addImage(String::createWithFormat("common/sticker_%d.png", myInt)->getCString()));


    this does work and i do get the new sticker image in sticker1
    but the problem is that the dimensions of the image remain as 100 x 100 no matter what sticker image was chosen due to the value of myInt
    so i want the sticker1 sprite to be resized depending upon the orginal dimensions of the new image that was set
    ----------------------------------------------------------------------------------------------------

     

    ----------------------------------------------------------------------------------------------------
    OPTION 2
    can i create an empty sprite object so that it does not have any image in it
    and then can i set an image in the sprite depending upon the value of myInt
    and even if this is possible, how can i ensure that the sprite gets resized depending upon the orginal dimensions of the new image that was set
    ----------------------------------------------------------------------------------------------------

    i hope i was able to explain my requirement.
    the main point is that i want to write a common piece of code and pass variables to it (like %d. png) and want the sprite images to be changed accordingly.
    thanks

     

  • Sonar Systems admin
    Likes 0

    For option 1 try using setContentSize method.

     

    For option 2 you should be all good as the sprite will take the size from the first image it is assigned.

Login to reply