I have been working on my own SceneManager class for fun and to learn more about Gideros and Lua. I come from using Obj-C and am used to many of its conventions.
I have a class "SceneManager" and a class "Layer". SceneManager and Layer are derived from a Sprite with Core.class(Sprite) and this works great. The problem is that I want to make a new class using Layer as a base and to override its functions. The problem is that Layer functions are not being overridden and are still being called. I assume this is because I am using events or something. Is there a way to call an eventlistener on the base class Layer and only use the functions in the subclass MenuScene?
layer.lua
Layer = Core.class(Sprite)
function Layer:init()
self:addEventListener(Event.ADDED_TO_STAGE, self.onAdd, self)
self:addEventListener(Event.ENTER_FRAME, self.onUpdate, self)
self:addEventListener(Event.REMOVED_FROM_STAGE, self.onRemove, self)
self.paused = false
end
function Layer:onUpdate(event)
print("LAYER_ONUPDATE")
end
function Layer:onAdd(event)
print("LAYER_ONADD")
end
function Layer:onRemove(event)
print("LAYER_ONREMOVE")
end |
menuscene.lua
MenuScene = Core.class(Layer)
function MenuScene:init()
local background = Bitmap.new(Texture.new("gfx/background.png"))
self:addChild(background)
local OptionsButton = Button.new( Bitmap.new(Texture.new("gfx/button_up.png")) , Bitmap.new(Texture.new("gfx/button_down.png")) )
OptionsButton:addEventListener("click", self.onOptionsButtonClicked, self)
self:addChild(OptionsButton)
end
function MenuScene:onUpdate(event)
print("MENUSCENE_ONUPDATE")
end
function MenuScene:onAdd(event)
self:removeEventListener(Event.ADDED_TO_STAGE, self.onAdd, self)
end
function MenuScene.onRemove(event)
--self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
--Handle Button Functions
function MenuScene:onOptionsButtonClicked(event)
scene = PauseScene:new()
sceneManager:push(scene)
end |
Comments
But about adding event listeners, it is better to add them inside extended classes:
http://www.giderosmobile.com/forum/discussion/1234/accessing-children-from-parent-class#Item_13
Summer times..
Website: http://www.castlegateinteractive.com
https://play.google.com/store/apps/developer?id=Castlegate+Interactive
I am not sure what you mean by your second solution. How do you stop the propagation of events?
In your touch event handler for your object do something like this
Website: http://www.castlegateinteractive.com
https://play.google.com/store/apps/developer?id=Castlegate+Interactive
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill