local MainScene = class(“MainScene”, cc.load(“mvc”).ViewBase)
local BEEvents = {}
BEEvents.lockEvent= “baselayer_lock_event”
BEEvents.unlockEvent = “baselayer_unlock_event”
local MyClassBase = class(“MyClassBase”, function()
return display.newLayer()
end)
function MyClassBase:ctor()
self:enableNodeEvents()
end
function MyClassBase:onEnter()
–lock others
self:lock()
local dispatcher = self:getEventDispatcher()
--register lock msg
local lockListener = cc.EventListenerCustom:create(BEEvents.lockEvent, function(eventCustom)
print("lock message")
end)
dispatcher:addEventListenerWithSceneGraphPriority(lockListener, self)
--register unlock msg
local unlockListener = cc.EventListenerCustom:create(BEEvents.unlockEvent, function(eventCustom)
print("unlock message")
end)
dispatcher:addEventListenerWithSceneGraphPriority(unlockListener, self)
end
function MyClassBase:lock()
local lockEvent = cc.EventCustom:new(BEEvents.lockEvent)
local dispatcher = self:getEventDispatcher()
dispatcher:dispatchEvent(lockEvent)
end
function MyClassBase:unlock()
local unlockEvent = cc.EventCustom:new(BEEvents.unlockEvent)
local dispatcher = self:getEventDispatcher()
dispatcher:dispatchEvent(unlockEvent)
end
function MyClassBase:onExit()
self:unlock()
end
–classA
local MyClassA = class(“MyClassA”, MyClassBase)
function MyClassA:ctor()
MyClassA.super.ctor(self)
local bkg = cc.LayerColor:create(cc.c4b(255, 255, 0, 77))
self:addChild(bkg)
self.bkgLayer = bkg
end
function MyClassA.show()
if not MyClassA.currentActivity then
local parent = cc.Director:getInstance():getRunningScene()
local activity = MyClassA.new()
parent:addChild(activity, 100000)
MyClassA.currentActivity = activity
end
end
function MyClassA.hide()
if MyClassA.currentActivity then
MyClassA.currentActivity:removeFromParent()
MyClassA.currentActivity = nil
end
end
local MyClassB = class(“MyClassB”, function( … )
return display.newNode()
end)
function MainScene:onCreate()
local function onTouchBegan(touch, event)
print(“touch me”)
if self.state == nil then
MyClassA.show()
self.state = 1
print("step 1: crete a")
elseif self.state == 1 then
self.b = MyClassB.new()
local scene = cc.Director:getInstance():getRunningScene()
scene:addChild(self.b)
MyClassA.hide()
self.state = 2
print("step 2: create b then remove a")--and then it must be crash on c++ ~Node()
else
self.b:removeFromParent()
self.state = nil
print("step 3: remove b now")
end
return true
end
local listener = cc.EventListenerTouchOneByOne:create()
listener:setSwallowTouches(true)
listener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)
end
return MainScene