Changing sprite-frame of a node

so i have this node and i want to change its image how do i do that?

i used this inside onLoad so that if the picture was changed it would load the new one

var image2 = cc.url.raw("Texture/balloon0.png");
this.balloon.getComponent(cc.Sprite).spriteFrame.setTexture(image2);

on first picture thats the balloon with its new picture
1b 2b

so on the right one it kinda shrinks but the outline of the rigid body stays the same because the balloon reacts as if its the same size as in the first picture

i think this is a very bad explanation but honestly i dont know how to explain so if you ask questions to understand better ill try to answer as best as i can

THANKS

@slackmoehrle any ideas?

I change sprites at runtime by creating a cc.SpriteFrame property, and assigning it to the cc.Sprite component in my TypeScript code:

    // the property. assign a value in the Cocos Creator!
    @property(cc.SpriteFrame)
    mySprite: cc.SpriteFrame = null;

    changeSprite() {
        // change sprite frame to the one specified by the property
        this.getComponent(cc.Sprite).spriteFrame = mySprite;
    }

I hope this helps.

1 Like

ok so like what do i enter instead of null ? location of another .png file or something like that?

OK so thanks for trying to help me i just found this https://docs.cocos2d-x.org/creator/manual/en/scripting/load-assets.html#how-to-dynamically-load which answers lots of questions marks. i’ll try to solve the problem and i’ll let you know

No, you keep that as null, but in the Cocos Creator, you assign the sprite frame, like this: 06

oh dude thanks so much this solves everything :smiley:

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.