event parameter nil in addeventlister?

Hello, this been bugging me for a while now, the event parameter in myData lua says its a nil. I structured the code that way so that I can reuse it whenever I want. But it seems that the event parameter doest go along with it when I call the function. I did some experimentation but still no luck, it always return nil in myData.lua.

please see my code below

main.lua

[lua]

local myData = require(“myData”)

objectToBeMove:addEventListener( “touch”,myData.dragMe )

[/lua]

myData.lua

[lua]

local M = {}

M.dragMe = function(self,event)

–checks if on simulator or not

local isSimulator = “simulator” == system.getInfo(“environment”)

 

if isSimulator then

 

if event.phase == “began” then

moveX = event.x - self.x

elseif event.phase == “moved” then

self.x = event.x - moveX

end

 

if((self.x - self.width * 0.5) < 0) then

self.x = self.width * 0.5

elseif((self.x + self.width * 0.5) > display.contentWidth) then

self.x = display.contentWidth - self.width * 0.5

end

end

end

return M

[/lua]

I don’t know what I did wrong. I have another code just like it but it doesn’t handle touch events and it works. But on this I don’t know why.

Thanks in Advance

Jam

Hi Jam Paraiso

Try this,

objectToBeMove.touch = myData.dragMe

objectToBeMove:addEventListener( “touch”,objectToBeMove )

 

Best Regards,

Team, SP Technolab

www.sptechnolab.com

Thanks for the reply SP Technolab

there is a error when I try what you said.

its says it attempts to index global objectToBeMove ( a nil value )

any idea on this?

Thanks again,

Jam

Hi Jam Paraiso

Here is my code and it is working fine

local myData = require(“myData”)

local circle = display.newCircle(0, 100, 20)

circle:setFillColor(0, 0.5, 0)

circle.touch = myData.dragMe

circle:addEventListener( “touch”,circle)

Best Regards,

Team, SP Technolab

www.sptechnolab.com

Thanks a lot!!

works like a charm :slight_smile:

Hi Jam Paraiso

Try this,

objectToBeMove.touch = myData.dragMe

objectToBeMove:addEventListener( “touch”,objectToBeMove )

 

Best Regards,

Team, SP Technolab

www.sptechnolab.com

Thanks for the reply SP Technolab

there is a error when I try what you said.

its says it attempts to index global objectToBeMove ( a nil value )

any idea on this?

Thanks again,

Jam

Hi Jam Paraiso

Here is my code and it is working fine

local myData = require(“myData”)

local circle = display.newCircle(0, 100, 20)

circle:setFillColor(0, 0.5, 0)

circle.touch = myData.dragMe

circle:addEventListener( “touch”,circle)

Best Regards,

Team, SP Technolab

www.sptechnolab.com

Thanks a lot!!

works like a charm :slight_smile: