cocos2d-xcocos2d-x-3.0cocos2d-x-3.x

How to create UI button from SpriteFrames?


A "Button" can be created by:

ui::Button* button = ui::Button::create("button.png");

How can we use a sprite sheet for this ?

EDIT: If I just put the name of a sprite frame for example:

ui::Button* button = ui::Button::create("button");

Then it doesn't detect it - thinks there is a missing image.


Solution

  • You could pass an extra argument to the ui:Button:create function.

    You could use

    ui::Button* button = ui::Button::create("button","","",TextureResType::PLIST);

    to create a button with a sprite frame named "button".

    For more information, please refer to the UIButton.h and UIButton.cpp files.