Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Some trouble with a Custom Event. Any ideas? — Gideros Forum

Some trouble with a Custom Event. Any ideas?

Ozkar619Ozkar619 Member
edited May 2013 in General questions
Hi, i'm having some trouble trying to implement a custom event.

Here i coded a little a situation that is exactly similar to mine but much less complicated to understand, so we can get straight to the problem:

[code]
ClassA = Core.class(EventDispatcher)

function ClassA:init()
self.name = "Oscar"
end

ClassB = Core.class(EventDispatcher)

function ClassB:init()
self.a = ClassA.new()
end

function ClassB:SayHi(event)
print("--With Event--")
print("Hello",event.name)
print("--With Self--")
print("Hello",self.a.name)
end

function ClassB:run()
self:addEventListener("myevent",self.SayHi,self)
self:dispatchEvent(Event.new("myevent"))
end

local b = ClassB.new()

b:run()
[/code]

And here is the result it throws:

--With Event--
Hello nil
--With Self--
Hello Oscar

Why am i not getting the right event arguments?

Thanks in advance!

Comments

  • @Ozkar619,
    The event created by using Event.new does not create an event with the name as you specified, instead it is the type of event that you have created.

    To get that, you need to query the
    __type
    which would provide you with what you are after.

    Likes: Ozkar619

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
    +1 -1 (+1 / -0 )Share on Facebook
  • ... slightly different than how it is with C*SDK ...

    Likes: Ozkar619

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
    +1 -1 (+1 / -0 )Share on Facebook
  • sslivkasslivka Member
    @Ozkar619,
    [lua]
    function ClassB:run()
    self:addEventListener("myevent",self.SayHi,self)
    local myE = Event.new("myevent")
    myE.name = "MyEvent"
    self:dispatchEvent(myE)
    end
    [/lua]

    Likes: Ozkar619

    +1 -1 (+1 / -0 )Share on Facebook
  • sslivkasslivka Member
    @Ozkar619,
    [lua]
    function ClassB:SayHi(event)
    print("--With Event--")
    print("Hello",event.name)
    print("--With Self--")
    print("Hello",self.a.name)

    for i,v in pairs(event) do
    print(i, v)
    end
    end
    [/lua]

    results:
    Uploading finished.
    --With Event--
    Hello MyEvent
    --With Self--
    Hello Oscar
    __target table: 0763A858
    __type myevent
    __userdata userdata: 0763B0E8
    name MyEvent

    Likes: Ozkar619

    +1 -1 (+1 / -0 )Share on Facebook
  • Thanks a lot guys :) that will do.
  • MellsMells Guru
    edited May 2013
    @Ozkar619
    This should work :
    local target = event:getTarget()
    print (target.name) -- "Oscar"
     
    local eventName = event:getType()
    print (eventName) -- "myEvent"
    Avoid this :
    self:addEventListener("myevent",self.SayHi,self)
    local myE = Event.new("myevent")
    myE.name = "MyEvent"
    instead :
    local eventName = "myEvent"
    self:addEventListener(eventName,self.SayHi,self)
    local myE = Event.new(eventName)
    You want to avoid typing errors as much as possible..

    Likes: Ozkar619

    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.