Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Starting with Gideros, question on dispatching events to parent... — Gideros Forum

Starting with Gideros, question on dispatching events to parent...

harlock74harlock74 Member
edited July 2012 in General questions
Hi,
I'm hoping someone can help me out on this one. I have the following code:

In game.lua file at one point I declare:

local tileBtn = Tile.new("normalTile", 1, myTileSound);

Tile.lua looks like this:
Tile = Core.class(Sprite)
 
local function tileClicked(self, event)
 
	-- play tile sound
	local tileSound = Sound.new("assets/audio/tileSounds/" .. self.tileSound .. ".wav");
	audioChannel = tileSound:play();
end
 
function Tile:init(type, pValue, tSound) 	
	local tileUp = Bitmap.new(Texture.new("assets/tile_low.png"));
	local tileDown = Bitmap.new(Texture.new("assets/tile_high.png"));
 
	local tileBtn = Button.new(tileUp, tileDown);
	tileBtn:addEventListener("click",  tileClicked, tileBtn);
	tileBtn.pointValue = pValue;
	tileBtn.tileSound = tSound;
	self:addChild(tileBtn);
end
My question is, what do I need to do so that when tileClicked gets called and plays the sound, it also executes a function that is in game.lua... I figured this would be using events, so I would dispatch an event at the end of tileClicked and then I just setup a listener in game.lua like so:

tileBtn:addEventListener("click", doSomething);

I think I have the idea right, but for my life, I have not been able to wire it correctly in the code :( :(

Thank you for any help... loving Gideros btw!

Dislikes: hosamred

+1 -1 (+0 / -1 )Share on Facebook

Comments

  • Well it should look something like that:
    Tile = Core.class(Sprite)
     
    local function tileClicked(self, event)
     
    	-- play tile sound
    	local tileSound = Sound.new("assets/audio/tileSounds/" .. self.tileSound .. ".wav");
    	audioChannel = tileSound:play();
    	--store some data in event to pass to handler
    	self.myCustomEvent.x = event.x
    	self.myCustomEvent.y = event.y
    	--dispatching event
    	self:dispatchEvent(self.myCustomEvent)
    end
     
    function Tile:init(type, pValue, tSound) 
    	--adding custom event
    	self.myCustomEvent = Event.new("customClick")
    	local tileUp = Bitmap.new(Texture.new("assets/tile_low.png"));
    	local tileDown = Bitmap.new(Texture.new("assets/tile_high.png"));
     
    	local tileBtn = Button.new(tileUp, tileDown);
    	tileBtn:addEventListener("click",  tileClicked, tileBtn);
    	tileBtn.pointValue = pValue;
    	tileBtn.tileSound = tSound;
    	self:addChild(tileBtn);
    end
    And then when using class inside your game.lua
    local tile = Tile.new(someparams you need to pass)
    --add event listener to custom click
    tile:addEventListener("customClick", function(e)
    	--we can get info we passed with event
    	print(e.x)
    	print(e.y)
    end, tile)
  • Thank you very much for your help. That works, perfectly! B-)
Sign In or Register to comment.