cocos2d-x lua 调用onEnter和onExit
cocos2d-x lua 调用onEnter和onExit
version: cocos2d-x 3.6
onEnter和onExit在lua中不会因节点别add和remove而直接被调用,当子节点被父节点add和remove时,会发送enter和exit的消息,所以需要再初始化节点的时候,监听消息,并在收到消息后调用onEnter或onExit。
local function onNodeEvent(event)
if event == "enter" then
self:onEnter()
elseif event == "exit" then
self:onExit()
end
end
self:registerScriptHandler(onNodeEvent)
在创建该节点时,加入上面的代码,那么onEnter和onExit就会在节点被add和remove是调用了。