some suggestiones for the lua interface of CCMenuIteme

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)

Thank you, alex cheng.

1: is resolved by #613.
2: we will implement it.
3: What’s CCScriptObject?

hi,Minggo Zhang
I have submit a attachment(cocos2dx-lua.rar) in this message and it include all the modified codes.
CCScriptObject is a script object class. It’s a pure virtual class. It’s in the CCScriptSupport.h file.
And LuaFunction is subclass of the CCScriptObject, It’s in the LuaValue.h/cpp files

Ok, I will have a look.
Thank you again.