How to differentiate witch menuItem I Clicked in Lua script ?

How to differentiate witch menuItem I Clicked in Lua script ?
0.0 0


Sorry for my poor English.
I want to make a game packet UI with lua.
I puted a lot of CCMenuItemImage as weapons.
But the registerScriptHandler’s param is a callback function.
So I must write a lot of callback function for every menu item.
Just as callback1, callback2, callback3…
Can you tell me how to solve it ? Thanks a lot.


You can use CCSprite or CCLabel for each menu item instead of using CCMenu. Store them in a table called _allMenuButtons. Then, register a touch handler with your layer. You can then check the bounding box of the touch point for each button in the onTouchEnded event handler:

local function onTouchEnded(x, y)
for i,button in ipairs(_allMenuButtons) do
if button:boundingBox():containsPoint(CCPointMake(x,y)) then
if i 1 then
–first button logic
elseif i 2 then
—second button logic

local function onTouch(eventType, x, y)
if eventType CCTOUCHBEGAN then
return true
elseif eventType CCTOUCHENDED then
return onTouchEnded(x, y)


For large amounts of menu items I recommend using CCMenu and a separate callback function for each for code readability.


Yes, I do~