How to identify different CCMenuItem?

Hi,I’m new in Lua.
As we know there’s (void*)sender int Obj-c, but how can i identify different CCMenuItem?
How to write the callback function?
I’ll appreciate it so much!

I found out that one param in the callback equals the tag of item.

As I konw,you shuold create a register to listen when your menuitem was touching.try to search in google or read my code http://blog.csdn.net/chococlatetan/article/details/8708878

旭冬 谭 wrote:

As I konw,you shuold create a register to listen when your menuitem was touching.try to search in google or read my code http://blog.csdn.net/chococlatetan/article/details/8708878

Your code is not related to what i asked. But thank you all the same!

yzdzidan yuan wrote:

Hi,I’m new in Lua.
As we know there’s (void*)sender int Obj-c, but how can i identify different CCMenuItem?
How to write the callback function?
I’ll appreciate it so much!
>
I found out that one param in the callback equals the tag of item.

U could see detail params in CCLuaEngine.cpp
At Lua side,you can use:tag = args[1 ], pSender = args[2 ] // thats the menuitem !~

int CCLuaEngine::executeMenuItemEvent(CCMenuItem* pMenuItem)
{
int nHandler = pMenuItem~~>getScriptTapHandler;
if return 0;
m_stack~~>pushInt(pMenuItem~~>getTag);
m_stack~~>pushCCObject(pMenuItem, “CCMenuItem”);
return m_stack->executeFunctionByHandler(nHandler, 2);
}

sorry! i am not good at C++ coding. u could know how to create touch event .

code:

local function itemStart_Click()—touch event
gameStart()
end
local function itemExit_Click()—touch event
CCDirector:sharedDirector():endToLua()
end

local itemStart = CCMenuItemFont:create(“Start”)—menu item
local itemExit = CCMenuItemFont:create(“Exit”)—menu item

itemStart:registerScriptTapHandler(itemStart_Click)—registerScript item Start
itemExit:registerScriptTapHandler(itemExit_Click)—registerScript item Exit

even u could use CCRect.intersectsRect(proRect,tarRect) to check whether you tounch on the meun.

xu tao wrote:

yzdzidan yuan wrote:
> Hi,I’m new in Lua.
> As we know there’s (void*)sender int Obj-c, but how can i identify different CCMenuItem?
> How to write the callback function?
> I’ll appreciate it so much!
>
> I found out that one param in the callback equals the tag of item.
>
U could see detail params in CCLuaEngine.cpp
At Lua side,you can use:tag = args[1 ], pSender = args[2 ] // thats the menuitem !~
>
int CCLuaEngine::executeMenuItemEvent(CCMenuItem* pMenuItem)
{
int nHandler = pMenuItem~~>getScriptTapHandler;
if return 0;
m_stack~~>pushInt(pMenuItem~~>getTag);
m_stack~~>pushCCObject(pMenuItem, “CCMenuItem”);
return m_stack->executeFunctionByHandler(nHandler, 2);
}

That’s the detail code in CCLuaEngine, but I want to get the CCObject in my callback function. Thank you all the same!

maybe you can use tag, for example:

module("TmpLayer", package.seeall)

YES_BT_TAG = 0
NO_BT_TAG = 1

function createLayer()
    local layer = CCLayer:create()
    CCMenuItemFont:setFontName("Arial")
    CCMenuItemFont:setFontSize(25)
    -- create yes button
    local yes_item = CCMenuItemFont:create("yes")
    yes_item:setTag(YES_BT_TAG)
    yes_item:registerScriptTapHandler(ClickButton)
    -- create no button
    local no_item = CCMenuItemFont:create("no")
    no_item:setTag(NO_BT_TAG)
    no_item:registerScriptTapHandler(ClickButton)
    -- create menu
    local array = CCArray:create()
    array:addObject(yes_item)
    array:addObject(no_item)
    local menu = CCMenu:createWithArray(array)
    menu:alignItemsVertically()
    layer:addChild(menu)
    return layer
end

function ClickButton(tag)
    if tag == YES_BT_TAG then
        print("click yes bt")
    elseif tag == NO_BT_TAG then
        print("click no bt")
    else
        print("I don't know")
    end
end

Xinghui Chen wrote:

maybe you can use tag, for example:
>
[…]

it’s useful for me , thx

— this is menu item callback
local function btnCallback(tag, object)
local menuItem = tolua.cast(object, “CCMenuItemImage”)

end