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
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
Likes: Ozkar619
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
Likes: Ozkar619
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
[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
[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
This should work :
Likes: Ozkar619