Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
How does Gideros Lua 'Override' base class? - Gideros Forum

How does Gideros Lua 'Override' base class?

newbie2018newbie2018 Member
edited February 2018 in General questions
I read this related post. But I haven't figured out how it works.

First, I have this base class "Item" (simple one)
Item = Core.class(Sprite)
function Item:init()
     self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
end

It works perfectly.
Now I would like to create another class "SubItem"
SubItem = Core.class(Item)
function SubItem:init()
     self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
end
I would like to have it behave such a way that, when it is touched, it is triggering the onTouchesBegin of the SubItem, but not the base class Item.

Now, from observation, it enters "Both" - first from base class, then its own onTouchesBegin

Not sure I have missed out anything. Is that a way to override the base class function? (without calling its base class function)

P.S.:
Sorry I missed out the point that it is a 'Constructor'.

Comments

  • olegoleg Member
    edited February 2018
    use
    e: stopPropagation ()

    in the onTouchesBegin function

    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • newbie2018newbie2018 Member
    edited February 2018
    @oleg
    Thanks for your response. Do you mean this? (Adding to both base and subclass).
    Item = Core.class(Sprite)
    function Item:init()
         self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
    end
     
    function Item:onTouchesBegin(e)
         e: stopPropagation ()
         -- my logic here...
    end
    SubItem = Core.class(Item)
    function SubItem:init()
         self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
    end
     
    function SubItem:onTouchesBegin(e)
         e: stopPropagation ()
         -- my logic here...
    end
    I actually had this. But doesn't seem to work...
    Probably I miss out something... Or did I misunderstand .. When it "stopPropagation", does it mean it stop propagating to its base class?
  • olegoleg Member
    edited February 2018 Accepted Answer
    variant2
     Item = Core.class(Sprite)
    Item = Core.class(Sprite)
    function Item:init()
         self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
    end
     
    function Item:onTouchesBegin(e)
         e: stopPropagation ()
         -- my logic here...
    	  print("Item")
    end
    SubItem = Core.class(Item)
    function SubItem:init()
    	self:removeEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
         self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin2, self)
    end
     
    function SubItem:onTouchesBegin2(e)
         e: stopPropagation ()
    	 print("SubItem")
         -- my logic here...
    end
     
    a=SubItem.new()
    stage:addChild(a)

    Likes: newbie2018

    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
    +1 -1 (+1 / -0 ) Share on Facebook
  • Haha @oleg
    it works~ thanks
  • By the way, i think I understand overall what I messed up now...
    I used to do initialization in the Constructor.
    But those initialization always call the base class functions before those from subclass.

    As an alternative, I need to have another function (eg: named as Setup()). This function would override the base one without calling the base contents...
  • newbie2018newbie2018 Member
    edited February 2018
    It may sound strange, this actually works...
    add a minor delay... LOL
    function Item:init()
    	Timer.delayedCall(1, function()
    		self:Setup()
    	end)
    end
    it wouldn't call the base class function this way
Sign In or Register to comment.