Draggable Object Issue

I want to create a draggable object.  I found the code below in the forum.  But when I try to use it, I get the following message in the simulator.  " Attempt to index global ‘myObject’ (a nile value).

What is causing the error?  


– create object

local myObject = display.newRect( 0, 0, 100, 100 )

myObject:setFillColor( 255 )

– touch listener function

function myObject:touch( 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

– make ‘myObject’ listen for touch events

myObject:addEventListener( “touch”, myObject )


Thanks

what line is it saying the error is on?

Also, your setFillColor(255) is a graphics 1.0 style call, if you want it white and you’re using a recent daily build, perhaps:

:setFillColor(1)

would do be more correct.

Below is my error message:

Corona Simulator Runtime error

file: level 35.lua

Line 335

Attempt to index local "myObject’ (a nil value)

The first line below is line 335:

function myObject:touch( 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

Thanks

Lori

Is the:  local myObject = display.newRect(…) call inside some other function where the scope of myObject wouldn’t let it be scene where you’re defining the function?

I’m using Composer and have it in Scene Create - where I have all my other objects.

Hi Lori,

OK, so in that case, where is your “myObject:touch()” function scoped? Outside of all scene blocks? If so, above or below the functions where you manage the various scene phases?

Brent

It was the last fuction event in my forward declarations so I assume that would make it after all the scene phases.   Sorry, I’m not sure what you mean by scene blocks and scene phases (newbie).

All of my events are in Forward Declarations and my functions for non (events) are in Scene Show after Scene Create.

I hope I answered your question correctly.

Thanks

Lori  

Hi Lori,

I’d suggest just making this function local and “de-associated” with the object, like below, and then place that function outside and above any of the scene function blocks (those being like “function scene:create( event )” and so forth).

[lua]

local function dragObject( event )

   local obj = event.target

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

      obj.markX = obj.x  – store x location of object

      obj.markY = obj.y  – store y location of object

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

      local x = (event.x - event.xStart) + obj.markX

      local y = (event.y - event.yStart) + obj.markY

      obj.x, obj.y = x, y  – move object based on calculations above

   end

   return true

end

[/lua]

Then, add a touch listener like this:

[lua]

myObject:addEventListener( “touch”, dragObject )

[/lua]

what line is it saying the error is on?

Also, your setFillColor(255) is a graphics 1.0 style call, if you want it white and you’re using a recent daily build, perhaps:

:setFillColor(1)

would do be more correct.

Below is my error message:

Corona Simulator Runtime error

file: level 35.lua

Line 335

Attempt to index local "myObject’ (a nil value)

The first line below is line 335:

function myObject:touch( 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

Thanks

Lori

Is the:  local myObject = display.newRect(…) call inside some other function where the scope of myObject wouldn’t let it be scene where you’re defining the function?

I’m using Composer and have it in Scene Create - where I have all my other objects.

Hi Lori,

OK, so in that case, where is your “myObject:touch()” function scoped? Outside of all scene blocks? If so, above or below the functions where you manage the various scene phases?

Brent

It was the last fuction event in my forward declarations so I assume that would make it after all the scene phases.   Sorry, I’m not sure what you mean by scene blocks and scene phases (newbie).

All of my events are in Forward Declarations and my functions for non (events) are in Scene Show after Scene Create.

I hope I answered your question correctly.

Thanks

Lori  

Hi Lori,

I’d suggest just making this function local and “de-associated” with the object, like below, and then place that function outside and above any of the scene function blocks (those being like “function scene:create( event )” and so forth).

[lua]

local function dragObject( event )

   local obj = event.target

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

      obj.markX = obj.x  – store x location of object

      obj.markY = obj.y  – store y location of object

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

      local x = (event.x - event.xStart) + obj.markX

      local y = (event.y - event.yStart) + obj.markY

      obj.x, obj.y = x, y  – move object based on calculations above

   end

   return true

end

[/lua]

Then, add a touch listener like this:

[lua]

myObject:addEventListener( “touch”, dragObject )

[/lua]