--in foo.lua
Foo = {}
--constructor
function Foo:new(x, y)
--the new instance
local foo = display. newImage("foo.png")
- set some instance vars
foo.x = x
foo.y = y
foo.name ='foo'
--instance method
function foo:speak()
print("I am an instance and my name is ".. self.name)
end
--another instance method
function foo:moveLeft()
self.x = self.x-1
end
function foo:drag( event)
self.x = event.x
self.y = event.y
end
foo:addEventListener("touch", drag)
return foo
end
--class method
function Foo:speak()
print("I am the class Foo")
end
return Foo
I want the event listener on the foo object to call foo: drag the same instance. I can’t figure out how: at this time it’s in my A local function called “drag” is called in main.lua, and then I pass it back to the instance. Can I call the instance method directly from the listener? I am reading the listener here http://developer.anscamobile.com/reference/index/objectaddeventlistener, but I am a bit confused:/
Thanks, max
To make it work, you need to use The table listener is as follows:
function foo:touch(event)
self.x = event.x
self.y = event. y
end
foo:addEventListener("touch", foo)
This is valid because the event listener will try to call the table with the same name as the event Function in foo – “touch” in this example.
If you need to keep the function name as drag, you can work around this limitation by adding this function after the function definition:
player.touch = player.drag
This basically redirects the touch call to the drag function.
I have a Foo class (well, a pseudo-class) set up as follows:
--in foo.lua
Foo = {}
--constructor
function Foo:new(x, y)
--the new instance
local foo = display.newImage("foo.png")
- set some instance vars
foo.x = x
foo.y = y
foo.name ='foo'
--instance method
function fo o:speak()
print("I am an instance and my name is ".. self.name)
end
--another instance method
function foo:moveLeft()
self.x = self.x-1
end
function foo:drag(event)
self.x = event.x< br /> self.y = event.y
end
foo:addEventListener("touch", drag)
return foo
end
--class method
function Foo:speak()
print("I am the class Foo")
end
return Foo< /pre>I want the event listener on the foo object to call foo: drag the same instance. I can’t figure out how: at this point it is calling a local function called "drag" in my main.lua , And then I pass it back to the instance. Can I call the instance method directly from the listener? I am reading the listener here http://developer.anscamobile.com/reference/index/objectaddeventlistener, but I am a bit confused:/
Thank you, the biggest
p>Corona has two different types of event listeners, function listeners and table listeners. The local function you mentioned is valid because the function is called directly when the event is triggered. Corona Passing table functions is not supported, so passing drag in this instance will not work.
To make it work, you need to use a table listener, as shown below:
function foo:touch(event)
self.x = event.x
self.y = event.y
end
foo:addEventListener("touch", foo)This is valid because the event listener will try to call the function in table foo with the same name as the event-in this example it is "touch ".
If you need to keep the function name as drag, you can solve this limitation by adding this function after the function definition:
player .touch = player.dragThis basically redirects the touch call to the drag function.