Can anyone help me with creating a Button Class

Can anyone help me with creating a Button Class
0.0 0


Hi, i been trying to figure out how to create a Button Class as a subclass of either CCLayer or CCMenuItemImage.

I been wrecking my head for days but i cant seem to get the correct result.

I am trying to make a Button class that works in a way that,

  1. it will only respond when i touch the area of Button Image (i created a function that check if the touch is within the area of the image) [Thinking if there is any other better way]
  2. Able to Hold down the button
  3. Change Image from normalImage to selectedImage when i hold down
  4. If Button is pressed once, it can swiftly change to selectedImage than back to normalImage.

Early Thanks for anyone who help, been on this for days

I tried to follow the CCMenuItemImage about closenormal on HelloWorld but i cant hold down the button


You could try “CCControlButton”.



I tired CCControlButton, but apparently it doesnt register as a class to be created
CCSprite9Sprite too

do i have to #include anything?


Yes, you have to include the header of the class you’re using, because none of the Extensions (afaik) are included when you include cocos2d.h. Like for CCScale9Sprite you’d include “CCScale9Sprite.h”

void HelloWorld::init()
  btnNewGame1= cocos2d::extension::CCControlButton::create(cocos2d::extension::CCScale9Sprite::create("btn_newgame.png"));
    btnNewGame1 -> setPosition(ccp(winSize.width/2, winSize.height/2));

    this -> addChild(btnNewGame1);

    this -> schedule(schedule_selector(HelloWorld::update));

    return true;


void HelloWorld::update(float dt)

    if(btnNewGame1 -> isPushed())
         cout << "hi";

HelloWorld is a scene object

so this is what i did

when i click it it print out “hi” in the console
the image that was displayed is like compressed;