Create and Change Button Attributes by code

Create and Change Button Attributes by code
0.0 0


I want to ask about 2 separate points related to Buttons:

  1. i want to create 29 buttons in a Slider … i want to add them on Run time … how can i do that?
  2. i want to change a button sprite after an event by code … how to do so?

Please advice


Hi @malbishtawi,

concerning your 2nd question, I used cc.spriteFrame property to change the sprite texture:

    spriteFrameTrueProp: {
        default: null,
        displayName: "True SpriteFrame",
        type: cc.SpriteFrame,

And to change it:

tempSprite.spriteFrame  = this.spriteFrameTrueProp;

The tempSprite variable is a cc.Sprite component of a button.

Best regards,


I dont understand why the properties specified in cocos API ( for the cc.Button component don’t work.
I think the api documentation is not clear on that point :
for (a very simple) exemple
mybutton.normalColor = new cc.Color(185, 155, 55);
doesn’t change anything, as you would expect it does after reading the doc…


@malbishtawi Hi,

  1. you can simply run a loop and instantiate a sample node with button component(stored as a prefab or simply instantiating new node and adding component button inside loop), and then assign its properties and positions etc.

  2. Are you sure the TRANSITION property of button is set to COLOR.