Drag object returning nil value

Hi,

I have a function setup that is similar to the basic object drag function - https://coronalabs.com/blog/2011/09/24/tutorial-how-to-drag-objects/

It works fine if it is set with the addeventlistener assigned to an object. However I want to move the object by dragging anywhere on the screen. So essentially the object will mirror your touch and drag anywhere on the screen. But I keep getting an error saying that event.target is a nil value. I have set it up as runtime:addeventlistener () given that it’s not an object that I am clicking on… Can that still work?

Also, I notice in the example given that they refer to self.markX. I gather markX is just a variable? But they don’t define it anywhere prior. I had to create markX as a local variable to get it to work. I gather in this example using self or event.target doesn’t make any difference?

Thank you to those that take the time to respond.

show your exact code so folks can help.

However, if you want to see a conversion of the code you referenced that would work for the whole screen, here is one possibility (of many; Corona + LUA == Very flexible and many alternate solutions):

-- touch listener function function touch( self, event ) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y -- move object based on calculations above end return true end local myObject = display.newRect( 0, 0, 100, 100 ) myObject:setFillColor( 255 ) myObject.touch = touch Runtime:addEventListener( "touch", myObject )

Fantastic! It works! However I am still unsure of how exactly… I don’t quite understand the myObject.touch = touch part… how does that produce the end result?

I would be very interested in some further explanation of what you have provided compared to the track that I was on… i’m sure you could shed some light on where I may have been going wrong in general…

Here is what I had previously… but I was yet to get it to not return an error of nil value… i was then going to try storing the value of (event.x - event.xStart) and use that to move the object:

[lua]

function touchM(event)

local objectM = event.target

local markX, markY

if(event.phase == “began”) then

display.getCurrentStage( ):setFocus(objectM)

objectM.markX = objectM.x

objectM.markY = objectM.y

elseif(event.phase == “moved”) then

objectM.x = (event.x - event.xStart) + objectM.markX

objectM.y = (event.y - event.yStart) + objectM.markY

elseif(event.phase == “ended” or event.phase == “cancelled”) then

display.getCurrentStage( ):setFocus(nil)

end

return true

end

Runtime:addEventListener(“touch”, touchM)

[/lua]

I now have the following which works:

[lua]

function touchM(self, event)

if(event.phase == “began”) then

display.getCurrentStage( ):setFocus(self)

self.markX = self.x

self.markY = self.y

elseif(event.phase == “moved”) then

self.x = (event.x - event.xStart) + self.markX

self.y = (event.y - event.yStart) + self.markY

elseif(event.phase == “ended” or event.phase == “cancelled”) then

display.getCurrentStage( ):setFocus(nil)

end

return true

end

objectM.touch = touchM

Runtime:addEventListener(“touch”, objectM)

[/lua]

You were using a function listener, whereas I prefer table listeners wherever possible.  I’d walk through the various guides on the site and read up.  That’s how I learned of these features:

Sorry, but research, trial and error, and lots and lots of practice are the only way to really get this. 

I’ve already read through the corona university pages. I looked through the lua syntax again and finally understand how that code works.

show your exact code so folks can help.

However, if you want to see a conversion of the code you referenced that would work for the whole screen, here is one possibility (of many; Corona + LUA == Very flexible and many alternate solutions):

-- touch listener function function touch( self, event ) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y -- move object based on calculations above end return true end local myObject = display.newRect( 0, 0, 100, 100 ) myObject:setFillColor( 255 ) myObject.touch = touch Runtime:addEventListener( "touch", myObject )

Fantastic! It works! However I am still unsure of how exactly… I don’t quite understand the myObject.touch = touch part… how does that produce the end result?

I would be very interested in some further explanation of what you have provided compared to the track that I was on… i’m sure you could shed some light on where I may have been going wrong in general…

Here is what I had previously… but I was yet to get it to not return an error of nil value… i was then going to try storing the value of (event.x - event.xStart) and use that to move the object:

[lua]

function touchM(event)

local objectM = event.target

local markX, markY

if(event.phase == “began”) then

display.getCurrentStage( ):setFocus(objectM)

objectM.markX = objectM.x

objectM.markY = objectM.y

elseif(event.phase == “moved”) then

objectM.x = (event.x - event.xStart) + objectM.markX

objectM.y = (event.y - event.yStart) + objectM.markY

elseif(event.phase == “ended” or event.phase == “cancelled”) then

display.getCurrentStage( ):setFocus(nil)

end

return true

end

Runtime:addEventListener(“touch”, touchM)

[/lua]

I now have the following which works:

[lua]

function touchM(self, event)

if(event.phase == “began”) then

display.getCurrentStage( ):setFocus(self)

self.markX = self.x

self.markY = self.y

elseif(event.phase == “moved”) then

self.x = (event.x - event.xStart) + self.markX

self.y = (event.y - event.yStart) + self.markY

elseif(event.phase == “ended” or event.phase == “cancelled”) then

display.getCurrentStage( ):setFocus(nil)

end

return true

end

objectM.touch = touchM

Runtime:addEventListener(“touch”, objectM)

[/lua]

You were using a function listener, whereas I prefer table listeners wherever possible.  I’d walk through the various guides on the site and read up.  That’s how I learned of these features:

Sorry, but research, trial and error, and lots and lots of practice are the only way to really get this. 

I’ve already read through the corona university pages. I looked through the lua syntax again and finally understand how that code works.