Change button sprite-

 Hello,

i would get the Url of the normalSprite from a Button, i use:

        console.log(this.node.getComponent(cc.Button).normalSprite.nativeUrl); 

but both of them give me : undefined
Where do i wrong?

Thank you :slight_smile:

…mmm do you have defined normalSprite?
you could use this:

if(this.node.normalSprite !== undefined && this.node.normalSprite !== null) console.log(this.node.normalSprite.nativeUrl);

I define it, but on the IDE image
Isn’t it enough?

the error is because ‘normalSprite’ inside this.node don’t exists (https://docs.cocos.com/creator/api/en/classes/Node.html) but you could added with
this.node.normalSprite = …
So, to get normalsprite from a button you could make this:

if(this.node.getComponent(cc.Button) !== undefined || this.node.getComponent(cc.Button) !== null)
{

var normalSprite = this.node.getComponent(cc.Button).normalSprite;

this.node.normalSprite = normalSprite; // save in node the normalSprite variable to future use

}
cc.Button -> https://docs.cocos.com/creator/api/en/classes/Button.html

remember, normalSprite is a spriteFrame https://docs.cocos.com/creator/api/en/classes/SpriteFrame.html
and inside you will find the ‘nativeUrl’ variable: https://docs.cocos.com/creator/api/en/classes/SpriteFrame.html#nativeurl

Cheers

No. Maybe I didn’t explain myself correctly:

this.node.getComponent(cc.Button).normalSprite;

This doesn’t work. It said: undefined
even if i set (like i showed in the image) the proprierties in the IDE

I had read from here https://github.com/cocos-creator/engine/blob/f495398f4307775f0f733162e3d128d81e063063/cocos2d/core/components/CCButton.js#L323 that “Normal” (sprite-frame) is .normalSprite from the proprierties.
So why it doesn’t show the .nativeUrl?

Thank you

I could suggest you to debug with chrome dev tools and see what value have this.node. :confused: (very strange).

Cheers.

What version of Creator are you using? Can you give us a demo that reproduces the problem?