Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
understanding event.new — Gideros Forum

understanding event.new

arcadiaarcadia Member
edited October 2013 in General questions
i don't understant how to dispatch custom event:
my code is:
Tutorial = Core.class(Sprite)
 
function Tutorial:init()
	self.tuts = tutorialGFX
	self:close()
end
 
function Tutorial:close()
	self:dispatchEvent(Event.new("onClose"))
end
 
Game = Core.class(Sprite)
 
function Game:init()
        local tutorial = Tutorial.new()
	self:addChild(tutorial)
	tutorial:addEventListener("onClose",function()
		print("CLOSED")
	end)
end
don't print "CLOSED"

Comments

  • edited October 2013
    Very interesting question, I try and find that will work:

    So i think in your code, maybe when you init Tutorial via Tutorial.new(), it does not have eventListener. In my version, put it before call close, you can dispatchEvent as normal.
    Tutorial = Core.class(Sprite)
     
    function Tutorial:init()
    	self.tuts = tutorialGFX
    	self:addEventListener("onClose",function()
    		print("CLOSED")
    	end)
     
    	self:close()
    end
     
    function Tutorial:close()
    	self:dispatchEvent(Event.new("onClose"))
    end
     
    Game = Core.class(Sprite)
     
    function Game:init()
            local tutorial = Tutorial.new()
    	self:addChild(tutorial)
    end
     
    Game.new()
    Or you can test your version by call close after addEventListener and it will work:
    Tutorial = Core.class(Sprite)
     
    function Tutorial:init()
    	self.tuts = tutorialGFX
    	self:close()
    end
     
    function Tutorial:close()
    	self:dispatchEvent(Event.new("onClose"))
    end
     
    Game = Core.class(Sprite)
     
    function Game:init()
            local tutorial = Tutorial.new()
    	self:addChild(tutorial)
    	tutorial:addEventListener("onClose",function()
    		print("CLOSED")
    	end)
    	tutorial:close();
    end
     
    Game:new()

    Likes: ar2rsawseen

    Coming soon
    +1 -1 (+1 / -0 )Share on Facebook
  • @vitalitymobile
    Work but is not exactly what i need...
    I dont want to addEventListner inside The tutorial class...and i don t want to call Close outside The class....

    Tutorial is a fullscreen tutorial that explain The game and is called in different scenes of The game so, outside The tutorial class, i have to intercept when The tutorial finish and the event "onClose" si dispatched because i have to do different things depending on where i am.
  • ar2rsawseenar2rsawseen Maintainer
    edited November 2013
    @arcadia but if you dispatch the event inside Tutorial:close() function, then this function needs to get called somewhere. You can't expect to execute the contents of function, if you don't call the function, or am I missing something here?

    So basically what you posted in your first post, will work, when you call tutorial:close somewhere in your code
    +1 -1 (+1 / -0 )Share on Facebook
  • Mmm i call self:colse() in The tutorial class when The tutorial will finish And here i dispatch The new event onClose that i expect to intercept in class that create The tutorial classe...or i don t understand The use of dispatchevent? Something Like The button class that create The click event usable in The class that add a button...
  • edited November 2013 Accepted Answer
    Ah OK, I think i understand what you want, but if you look deep into Button example (Gideros Example) you will see that in init of Button class, it will not call self:dispatchEvent(Event.new("click")),

    So i think your class should be like this:
    Tutorial = Core.class(Sprite)
     
    function Tutorial:init()
    	self.tuts = tutorialGFX
    	--self:doRealTutorialHere(); 
    end
     
    function Tutorial:doRealTutorialHere(level)
    	print("I will teach you something about level " ..level.." and after that i will close, so please catch this event and do the next job...")
    	self:close();
    end
    function Tutorial:close()
    	self:dispatchEvent(Event.new("onClose"))
    end
     
    Game = Core.class(Sprite)
     
    function Game:init()
            local tutorial = Tutorial.new()
    	self:addChild(tutorial)
    	tutorial:addEventListener("onClose",function()
    		print("CLOSED")
    	end)
    	local currentLevel = 1;
    	tutorial:doRealTutorialHere(currentLevel); 
    end
     
    Game:new()

    Likes: arcadia

    Coming soon
    +1 -1 (+1 / -0 )Share on Facebook
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Yes @vitalitymobile is right, in your case you call self:close inside init method, which will automatically dispatch event, before even you could attach event handler to it.

    There should be some other action or input from user, that would dispatch the even only after you attached the event listener
    +1 -1 (+2 / -0 )Share on Facebook
  • Ah OK. I have wrote self: close() in the init only to try an example. Now i will implementa a real class with its logic. So The problemi was The dispatch in The init.
    Thanks both
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.