Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Classes and Overriding Functions - Gideros Forum

Classes and Overriding Functions

ruxpinruxpin Member
edited July 2012 in Game & application design
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

  • ar2rsawseenar2rsawseen Maintainer
    I guess thats an expected behavior, when you call extended classes method, it automatically call's parent's classes method. At least for constructors.
    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
  • gorkemgorkem Maintainer
    This is a bug. Fixed already and will be ready for next release.
  • ruxpinruxpin Member
    Sweet! Any updates on a release timeframe? I check the blog every day, but have been sad for almost a month now! ha ha
  • gorkemgorkem Maintainer
    The week after next week most probably. Oh yes the blog :) have to take care of it.

    Summer times..
  • ruxpinruxpin Member
    Okay another question regarding events. I am working on the ability to push and pop scenes off a stack and want to be able to pause the scenes underneath from updating. The problem is that if the Pause Scene doesn't take up the whole screen than the scenes underneath can still receive touches. Is there a way to pause a Sprite from receiving events?
  • @ruxpin: I assume set some flag when you pause a scene / layer. Can you not just check the status of the flag in your event handler and ignore events if the layer / scene is paused? Or have the scene / layer stop propagation of events after the layer has done it's processes so layers below don't receive them.
  • ruxpinruxpin Member
    @Sciyser I assume your first solution is to simply set up something like
    function Layer:onUpdate(event)
        if self.paused then
            return
        end
    end
    but for every function? That seems like a nice easy idea, but it seems like it is asking for me to constantly forget something.


    I am not sure what you mean by your second solution. How do you stop the propagation of events?
  • I wrote the last comment on my tablet, so now I'm on my desktop machine, I'll try and be a little clearer.

    In your touch event handler for your object do something like this
    function Object:onMouseDown(event)
    	-- check to see if the touch event is for this object
    	if self:hitTestPoint(event.x, event.y) then
    		-- yes it is so let's process for the button
    		self.focus = true
    		self:updateVisualState(true)
    		-- Stop this event from affecting any objects further up the heirarchy
    		event:stopPropagation()
    	end
    end
  • I'd also consider adding a full-screen semi trans shape behind the pause screen, you could then add a mouse listener to that and use that to capture the events, its also visually more of an acknowledgement to the user as to why the buttons behind (or to the side of) the pause menu aren't working
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • ruxpinruxpin Member
    Awesome ideas! I will probably wind up using both of them!
Sign In or Register to comment.