1: it should call the lua callback with the menuitem instance when the menuitem is activated
like this (in CCMenuItem.cpp )
if (m_functionName.size() && CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()) { CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeCallFuncN(m_functionName.c_str(), this); }
the lua code like :
function OnMusic(btn) if btn:getTag() == 0 then ..... end
2: please export the interface setNormalImage,setSelectedImage and setDisableImage of CCMenuItemImage, it can change the image at any time.
like:
@ if btn:getTag() == 0 then
—diable music
btn:setNormalImage(cocos2d.CCSprite:spriteWithFile(‘music_on.png’))
btn:setSelectedImage(cocos2d.CCSprite:spriteWithFile(‘music_on_click.png’))
btn:setTag(1)
else
—enable music
btn:setNormalImage(cocos2d.CCSprite:spriteWithFile(‘music_off.png’))
btn:setSelectedImage(cocos2d.CCSprite:spriteWithFile(‘music_off_click.png’))
btn:setTag(0)
end
@
3: If needed, it should support the interface for save the luafunction at CCMenuItem, and call it when the menuitem is actived
I have implemented it, the interface is
@ void registerScriptObject(CCScriptObject* scriptObject);@
please see the attachments.
For this solution, it can call a global function, a local function, a anonymous function, the upvalue…
I use it like this:
@ function CreateMenu()
local mn = CCMenu:menuWithItem(nil);
for i=1, 5, 1 do
local btn = cocos2d.CCMenuItemImage:itemFromNormalImage(“test.png”, “test_click.png”)
btn:setTag(i);
function OnClick()
dofunction(i)
end
btn:registerScriptObject(OnClick)
btn:setPosition(i*100, 200)
mn:addChild(mn)
end
return mn;
end
@
I hope it can add the latest version, because i have use it now:D
cocos2dx-lua.rar.zip (113.5 KB)