EventDispatcher questions

Hello everyone!

I would like clarification on the EventDispatcher.

I have two classes:

  1. Button

    local M = {} function M.new() local button = display.newCircle( 160, 250, 20 ) button:setFillColor( 0, 1, 0 ) button.name = “button” function button:tap( event ) print(“You pressed the button”) --send an event to open the gate? end button:addEventListener( “tap” ) return button end return M

  2. Gate

    function M.new() local gate = display.newRect( 80, 100, 100, 20 ) gate:setFillColor( .1, 8, 1 ) gate.name = “Gate” gate.dir = “dx” local function openGate( event ) print(“Open the gate”) --my code to open gate… end return gate end return M

The fact is this, if I use normal EventDispatcher, I have to use Runtime events and I create many I’m afraid that all these controls overplay the game by slowing it down…

Looking for I found this: http://swfoo.com/2014/07/11/send-custom-events-with-eventdispatcher-for-corona-sdk/

In my opinion, It consume less than Runtime events, so I tried using it. But I can not tell the classes between them.

So I have two questions:

  1. How do I use the link module to communicate the two objects?

2.Create so many Runtime events consuming a lot? is it better to use the “normal” events then, or the module in the link?

To answer your first question, you can make a small change to the gate like this:

[lua]

– gate.lua

local M = {}

function M.new()

    local gate = display.newRect( 80, 100, 100, 20 )
    gate:setFillColor( .1, .8, 1 )
    gate.name = “Gate”

    gate.dir = “dx”

    gate.open = function( event )
        print(“Open the gate”)
        --my code to open gate…
    end

    return gate
end

return M

[/lua]

For the button:

[lua]

– btn.lua

– EventDispatcher.lua from: https://github.com/daveyang/EventDispatcher

local EvtD = require “EventDispatcher”

local M = EvtD()

function M.new(gateToOpen)

    local button = display.newCircle( 160, 250, 20 )
    button:setFillColor( 0, 1, 0 )
    button.name = “button”

    function button:tap( event )
        print(“You pressed the button”)

        --send an event to open the gate
        M:dispatchEvent(“open”)
    end

    button:addEventListener( “tap” )

    M:addEventListener(“open”, gateToOpen)

    return button
end

return M

[/lua]

And to tie it all together:

[lua]

local btn = require(“btn”)
local gate = require(“gate”)

local b = btn.new(gate.new())

[/lua]

I can’t answer the second question, perhaps one of the Corona engineers would be able to.

Hope this helps,

Dave

Thanks for the immediate response. Your idea is fine but I would like something that avoids an object to pass another, is it possible?

Sorry not sure what you mean.

Are you trying to avoid this?

btn.new(gate.new())

Somehow the button needs to know which gate it’s supposed to open. There are many ways to do this, my example is just a simple demo.

Dave

something like this:

in “main”:

local EvtD = require "EventDispatcher" local dispatcher = EvtD() EvtD.test = dispatcher 

In the Button and Gate

local EvtD = require "EventDispatcher" local dispatcher = EvtD.test

what I did work but of course is not a “clean”  solution.

Do you know a better way to do this?

To know which gate is sufficient to put an attribute “.id” at the gate, right?

Sorry you’ve lost me. Why do you want to create EvtD.test?

Also, the gate doesn’t need to know about EventDispatcher.

Now I’m showing you a working example though as I said I would need something “cleaner”.

--main-- local EvtD = require "EventDispatcher" local test = EvtD() EvtD.test = test local Button = require("Button") local Gate = require("Gate") local b1 = Button.new( 80, 400, "red") local b2 = Button.new(240, 400, "blue") local g1 = Gate.new(80, 100, "red") local g2 = Gate.new(240, 100, "blue")

--Button-- local EvtD = require "EventDispatcher" local dispatcher = EvtD.test local M = {} function M.new(x, y, id) local button = display.newCircle( x, y, 20 ) button.name = "Button" button.id = id if(id == "red")then button:setFillColor( 1, 0, 0 ) elseif(id == "blue")then button:setFillColor( 0, 0, 1 ) end function button:tap( event ) print("tap the "..self.id.." button") dispatcher:dispatchEvent({ name="moveGate", id=self.id }) end button:addEventListener( "tap" ) return button end return M

--Gate-- local EvtD = require "EventDispatcher" local dispatcher = EvtD.test local M = {} function M.new(x, y, id) local gate = display.newRect( x, y, 100, 20 ) gate.name = "Cancello" gate.id = id if(id == "red")then gate:setFillColor( 1, 0, 0 ) elseif(id == "blue")then gate:setFillColor( 0, 0, 1 ) end gate.pos = "up" gate.moveGate = function( event ) if(event.id == gate.id) then if(gate.pos == "up")then transition.to(gate, {y = 300}) gate.pos = "down" elseif(gate.pos == "down") then transition.to(gate, {y = 100}) gate.pos = "up" end print("Move the gate", gate.id) end end dispatcher:addEventListener( "moveGate", gate ) function gate.finalize() dispatcher:removeEventListener( "moveGate", gate ) end gate:addEventListener( "finalize" ) return gate end return M

Now there is a way to better use your library as in my example?

No idea?

To answer your first question, you can make a small change to the gate like this:

[lua]

– gate.lua

local M = {}

function M.new()

    local gate = display.newRect( 80, 100, 100, 20 )
    gate:setFillColor( .1, .8, 1 )
    gate.name = “Gate”

    gate.dir = “dx”

    gate.open = function( event )
        print(“Open the gate”)
        --my code to open gate…
    end

    return gate
end

return M

[/lua]

For the button:

[lua]

– btn.lua

– EventDispatcher.lua from: https://github.com/daveyang/EventDispatcher

local EvtD = require “EventDispatcher”

local M = EvtD()

function M.new(gateToOpen)

    local button = display.newCircle( 160, 250, 20 )
    button:setFillColor( 0, 1, 0 )
    button.name = “button”

    function button:tap( event )
        print(“You pressed the button”)

        --send an event to open the gate
        M:dispatchEvent(“open”)
    end

    button:addEventListener( “tap” )

    M:addEventListener(“open”, gateToOpen)

    return button
end

return M

[/lua]

And to tie it all together:

[lua]

local btn = require(“btn”)
local gate = require(“gate”)

local b = btn.new(gate.new())

[/lua]

I can’t answer the second question, perhaps one of the Corona engineers would be able to.

Hope this helps,

Dave

Thanks for the immediate response. Your idea is fine but I would like something that avoids an object to pass another, is it possible?

Sorry not sure what you mean.

Are you trying to avoid this?

btn.new(gate.new())

Somehow the button needs to know which gate it’s supposed to open. There are many ways to do this, my example is just a simple demo.

Dave

something like this:

in “main”:

local EvtD = require "EventDispatcher" local dispatcher = EvtD() EvtD.test = dispatcher 

In the Button and Gate

local EvtD = require "EventDispatcher" local dispatcher = EvtD.test

what I did work but of course is not a “clean”  solution.

Do you know a better way to do this?

To know which gate is sufficient to put an attribute “.id” at the gate, right?

Sorry you’ve lost me. Why do you want to create EvtD.test?

Also, the gate doesn’t need to know about EventDispatcher.

Now I’m showing you a working example though as I said I would need something “cleaner”.

--main-- local EvtD = require "EventDispatcher" local test = EvtD() EvtD.test = test local Button = require("Button") local Gate = require("Gate") local b1 = Button.new( 80, 400, "red") local b2 = Button.new(240, 400, "blue") local g1 = Gate.new(80, 100, "red") local g2 = Gate.new(240, 100, "blue")

--Button-- local EvtD = require "EventDispatcher" local dispatcher = EvtD.test local M = {} function M.new(x, y, id) local button = display.newCircle( x, y, 20 ) button.name = "Button" button.id = id if(id == "red")then button:setFillColor( 1, 0, 0 ) elseif(id == "blue")then button:setFillColor( 0, 0, 1 ) end function button:tap( event ) print("tap the "..self.id.." button") dispatcher:dispatchEvent({ name="moveGate", id=self.id }) end button:addEventListener( "tap" ) return button end return M

--Gate-- local EvtD = require "EventDispatcher" local dispatcher = EvtD.test local M = {} function M.new(x, y, id) local gate = display.newRect( x, y, 100, 20 ) gate.name = "Cancello" gate.id = id if(id == "red")then gate:setFillColor( 1, 0, 0 ) elseif(id == "blue")then gate:setFillColor( 0, 0, 1 ) end gate.pos = "up" gate.moveGate = function( event ) if(event.id == gate.id) then if(gate.pos == "up")then transition.to(gate, {y = 300}) gate.pos = "down" elseif(gate.pos == "down") then transition.to(gate, {y = 100}) gate.pos = "up" end print("Move the gate", gate.id) end end dispatcher:addEventListener( "moveGate", gate ) function gate.finalize() dispatcher:removeEventListener( "moveGate", gate ) end gate:addEventListener( "finalize" ) return gate end return M

Now there is a way to better use your library as in my example?

No idea?