event.target as nil problem

Hi there guys, im making a game where i need to drag and drop display objects. My problem is that when i drag the object, i need to know a parameter of this object.

i got this code…
 

-- make 'myObject' listen for touch events myObject[i]:addEventListener( "touch", touch ) myObject has the parameter id, cat and e. When its dragged, i need to know cat value. function touch( event ) print("---------") if event.phase == "began" then markX = event.target.x -- store x location of object markY = event.target.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + markX local y = (event.y - event.yStart) + markY event.target.x, event.target.y = x, y -- move object based on calculations above end cat2=event.target.cat return true end

i read that event.target is null if this was called by a runtime touch event, but then, how can i resolve my problem?

Thanks,
Kinari

Your sample doesn’t have a “Runtime” touch event, the touch event is attached to the object itself:

myObject[i]:addEventListener( "touch", touch )

so event.target will not be nil, and your code should work fine.

When you see someone saying about a runtime touch event, I would presume they mean a touch event which is added to the Runtime object:

Runtime:addEventListener( "touch", touch )

rather than just meaning any touch event that occurs during runtime,

Thanks for trying to help me…

i tried adding

local cat=event.target.cat print(cat)

in the function touch but when i print it, it displays null… how do you think i can fix it?

Can you paste more of your code please? 

It’s hard to tell with such a small amount.

Sure, thanks a lot…

i used dragItem that conteins code like this:
 

return { { id = "agua1", cat=8, e=0 }, { id = "agua2", cat=8, e=0 }, { id = "antib1", cat=6, e=0 }}

this is where i create my objects:

 

 for i=1, total do -- create object myObject[i]=display.newImageRect("images/"..dragItem[randomNum[i]].id..".jpg",200, 200, true) myObject[i].x=x myObject[i].y=y if(dragItem[randomNum[i]].cat==cat) then cont=cont+1 end -- make 'myObject' listen for touch events myObject[i]:addEventListener( "touch", touch ) tf=tf+1 x=x+ex end

And this is my complete touch function :

 

function touch( event ) local cat=event.target.cat print(cat) print("---------") if event.phase == "began" then markX = event.target.x -- store x location of object markY = event.target.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + markX local y = (event.y - event.yStart) + markY event.target.x, event.target.y = x, y -- move object based on calculations above end -- this is to determine where is located that object and change another attribute named e of the target if(markX\<455-100 and markX\>-45 and markY\>145 and markY\<670) then print("correcto") print(event.target.e) event.target.e=1 else print("incorrecto") print(event.target.e) event.target.e=0 end return true end

I don’t see anywhere that you add the .cat property to the myObject object.

You have this if statement:

if(dragItem[randomNum[i]].cat==cat) then cont=cont+1 end

but it doesn’t have a

myObject[i].cat = cat

anywhere in it at all.

Now i notice that and that was my problem, i want to access cat that is in the dragItem array but i never give this value to myObject as you said,

i added:

myObject[i].cat=dragItem[randomNum[i]].cat

where i create myObject and that was the solution…

thanks a lot!!!

Your sample doesn’t have a “Runtime” touch event, the touch event is attached to the object itself:

myObject[i]:addEventListener( "touch", touch )

so event.target will not be nil, and your code should work fine.

When you see someone saying about a runtime touch event, I would presume they mean a touch event which is added to the Runtime object:

Runtime:addEventListener( "touch", touch )

rather than just meaning any touch event that occurs during runtime,

Thanks for trying to help me…

i tried adding

local cat=event.target.cat print(cat)

in the function touch but when i print it, it displays null… how do you think i can fix it?

Can you paste more of your code please? 

It’s hard to tell with such a small amount.

Sure, thanks a lot…

i used dragItem that conteins code like this:
 

return { { id = "agua1", cat=8, e=0 }, { id = "agua2", cat=8, e=0 }, { id = "antib1", cat=6, e=0 }}

this is where i create my objects:

 

 for i=1, total do -- create object myObject[i]=display.newImageRect("images/"..dragItem[randomNum[i]].id..".jpg",200, 200, true) myObject[i].x=x myObject[i].y=y if(dragItem[randomNum[i]].cat==cat) then cont=cont+1 end -- make 'myObject' listen for touch events myObject[i]:addEventListener( "touch", touch ) tf=tf+1 x=x+ex end

And this is my complete touch function :

 

function touch( event ) local cat=event.target.cat print(cat) print("---------") if event.phase == "began" then markX = event.target.x -- store x location of object markY = event.target.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + markX local y = (event.y - event.yStart) + markY event.target.x, event.target.y = x, y -- move object based on calculations above end -- this is to determine where is located that object and change another attribute named e of the target if(markX\<455-100 and markX\>-45 and markY\>145 and markY\<670) then print("correcto") print(event.target.e) event.target.e=1 else print("incorrecto") print(event.target.e) event.target.e=0 end return true end

I don’t see anywhere that you add the .cat property to the myObject object.

You have this if statement:

if(dragItem[randomNum[i]].cat==cat) then cont=cont+1 end

but it doesn’t have a

myObject[i].cat = cat

anywhere in it at all.

Now i notice that and that was my problem, i want to access cat that is in the dragItem array but i never give this value to myObject as you said,

i added:

myObject[i].cat=dragItem[randomNum[i]].cat

where i create myObject and that was the solution…

thanks a lot!!!