Hitting a button and executing a particular function I give it?

Hitting a button and executing a particular function I give it?
0.0 0


I made my own button class that inherits from: cocos2d::CCMenuItemImage.
When I hit and release the button, it executed its selected() and unselected() methods respectively.

I want to make it so when I hit a particular button, it executes a function I pass into it in it’s parameters.

For example, say I have a jump button:
Button jumpButton = new Button(…);

How would I:

  1. Pass in parameters to the selected() method, since selected() is called when I physically hit the button.
  2. With these parameters, allow myself to execute any function or class function I pass into the parameters?


One solution I have is not do any of this at all and just make a subclass for each type of button I make.
But as I continue into the game I’m going to have a ton of buttons and…would making subclasses for every single
button be efficient?

If the answer is no, how would I pass in parameters to a class function/any function and execute it in the button’s selected() or unselected() functions?

In the code I provided, there’s an example of passing in a player’s jump function into the button, and an enemy’s hit function into the button when it’s clicked.
I’m just really stumped…


I don’t know if you want to call a callback function when a menu item is hit?
If so, you can refer HelloWorld/Classes/HelloWorldScene.cpp.