Help, How to detect multiple id of objects

I am building a puzzle game, detecting objects when touch. Works horizontally and diagonally but not vertically what do I miss?

function touchMe(event)

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

        event.target.xScale = 1.0

        event.target.yScale = 1.0

        event.target.alpha = 0.4

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

        event.target.xScale = 1.0

        event.target.yScale = 1.0

        event.target.alpha = 0.4

        if (alphabet[event.target.id].alreadytouched == false ) then

          alphabet[event.target.id].alreadytouched = true

          assembledWord = assembledWord…event.target.value

          print(assembledWord)

        end

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

          event.target.alpha = 1

          if(alphabet[event.target.id].alreadytouched == false ) then

          alphabet[event.target.id].alreadytouched = true

        end

  end

end

for i = 1, 6 do

      for j = 1, 6 do

      local fname = nil

      if(L1Map[j][i] == 0) then

          str = sletter[math.random(26)]

          L1Map[j][i] = str

          fname = “letters/ltr_”…str…".png"

      else

           fname = “letters/ltr_”…L1Map[j][i]…".png"

      end

      alphabet[i] = newImageRectNoDimensions(fname)

      alphabet[i].x = math.floor(-30 + (55 * i))

      alphabet[i].y = math.floor(-10 + (35 * j))

      alphabet[i].alreadytouched = false

      alphabet[i].value =  L1Map[j][i]

      alphabet[i].id = i

      alphabet[i]:addEventListener( “touch”, touchMe)

      end

end

Hello. The code tags button is the blue <> looking thing on the tool bar. This formats the code to make it much easier to read. 

We could help much faster if you post code that can be run.   

Also helpful would be a better description of what the goal is.  

In this case I have deduced that you are dragging letters to make words. The dragging was working in some directions but not vertically.  

Because your code can’t be run as is I have modified to be able to test some aspects of it.  

I believe that the problem lies in the ‘move’ phase of the event.   

There is no reason to index alphabet inside there. event.target is already alphabet[event.target.id]. So you can just use 

if event.target.alreadytouched == false then event.target.alreadytouched = true &nbsp;

This change seems to have fixed the issue. 

Here is a modification of the code that can be run base on guessing what you are doing:

local alphabet={} local assembledWord = "" function touchMe(event) if ( event.phase == "began" ) then --print(event.target.fname) event.target.xScale = 1.0 event.target.yScale = 1.0 event.target.alpha = 0.4 elseif (event.phase == "moved") then --print"move" event.target.xScale = 1.0 event.target.yScale = 1.0 event.target.alpha = 0.4 --if (alphabet[event.target.id].alreadytouched == false ) then if event.target.alreadytouched == false then --alphabet[event.target.id].alreadytouched = true event.target.alreadytouched = true assembledWord = assembledWord..event.target.value print(assembledWord) end elseif( event.phase == "ended") then event.target.alpha = 1 if(alphabet[event.target.id].alreadytouched == false ) then alphabet[event.target.id].alreadytouched = true end end end local L1Map={} local sletter={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"} for i = 1, 6 do local str = "" --L1Map[i]= {} alphabet[i]={} for j = 1, 6 do L1Map[j] = {} L1Map[j][i] = 0 local fname = nil if(L1Map[j][i] == 0) then str = sletter[math.random(26)] L1Map[j][i] = str fname = "letters/ltr\_"..str..".png" else fname = "letters/ltr\_"..L1Map[j][i]..".png" end --alphabet[i] = newImageRectNoDimensions(fname) alphabet[i] = display.newText(str,0,0, native.systemFont, 18) alphabet[i].x = math.floor(-30 + (55 \* i)) alphabet[i].y = math.floor(-10 + (35 \* j)) alphabet[i].alreadytouched = false alphabet[i].value = L1Map[j][i] alphabet[i].id = i alphabet[i]:addEventListener( "touch", touchMe) end end&nbsp;

Hello,

I think your problem is come from your indentation. We don’t see where the loop of j is apply.

That’s mean you have an ID problem you have many object with the same id.

function touchMe(event) if ( event.phase == "began" ) then event.target.xScale = 1.0 event.target.yScale = 1.0 event.target.alpha = 0.4 elseif (event.phase == "moved") then event.target.xScale = 1.0 event.target.yScale = 1.0 event.target.alpha = 0.4 if (alphabet[event.target.id].alreadytouched == false ) then alphabet[event.target.id].alreadytouched = true assembledWord = assembledWord..event.target.value print(assembledWord) end elseif( event.phase == "ended") then event.target.alpha = 1 if(alphabet[event.target.id].alreadytouched == false ) then alphabet[event.target.id].alreadytouched = true end end end for i = 1, 6 do for j = 1, 6 do local fname = nil if(L1Map[j][i] == 0) then str = sletter[math.random(26)] L1Map[j][i] = str fname = "letters/ltr\_"..str..".png" else fname = "letters/ltr\_"..L1Map[j][i]..".png" end alphabet[i] = newImageRectNoDimensions(fname) alphabet[i].x = math.floor(-30 + (55 \* i)) alphabet[i].y = math.floor(-10 + (35 \* j)) alphabet[i].alreadytouched = false alphabet[i].value = L1Map[j][i] alphabet[i].id = i\*6+j alphabet[i]:addEventListener( "touch", touchMe) end end

remiduchalard, I think you are right. 

In the code alphabet[i] is being changed 6 times for each value of i.  

That whole nested loop needs reworking.

For example this line being inside the j loop changes the image for alphabet[i] 6 times, settling on the last image j=6 

alphabet[i] = newImageRectNoDimensions(fname)

Hi guys, thank you for your help. I already solved the issue. I just added an id and checker for j and it works!

Hello. The code tags button is the blue <> looking thing on the tool bar. This formats the code to make it much easier to read. 

We could help much faster if you post code that can be run.   

Also helpful would be a better description of what the goal is.  

In this case I have deduced that you are dragging letters to make words. The dragging was working in some directions but not vertically.  

Because your code can’t be run as is I have modified to be able to test some aspects of it.  

I believe that the problem lies in the ‘move’ phase of the event.   

There is no reason to index alphabet inside there. event.target is already alphabet[event.target.id]. So you can just use 

if event.target.alreadytouched == false then event.target.alreadytouched = true &nbsp;

This change seems to have fixed the issue. 

Here is a modification of the code that can be run base on guessing what you are doing:

local alphabet={} local assembledWord = "" function touchMe(event) if ( event.phase == "began" ) then --print(event.target.fname) event.target.xScale = 1.0 event.target.yScale = 1.0 event.target.alpha = 0.4 elseif (event.phase == "moved") then --print"move" event.target.xScale = 1.0 event.target.yScale = 1.0 event.target.alpha = 0.4 --if (alphabet[event.target.id].alreadytouched == false ) then if event.target.alreadytouched == false then --alphabet[event.target.id].alreadytouched = true event.target.alreadytouched = true assembledWord = assembledWord..event.target.value print(assembledWord) end elseif( event.phase == "ended") then event.target.alpha = 1 if(alphabet[event.target.id].alreadytouched == false ) then alphabet[event.target.id].alreadytouched = true end end end local L1Map={} local sletter={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"} for i = 1, 6 do local str = "" --L1Map[i]= {} alphabet[i]={} for j = 1, 6 do L1Map[j] = {} L1Map[j][i] = 0 local fname = nil if(L1Map[j][i] == 0) then str = sletter[math.random(26)] L1Map[j][i] = str fname = "letters/ltr\_"..str..".png" else fname = "letters/ltr\_"..L1Map[j][i]..".png" end --alphabet[i] = newImageRectNoDimensions(fname) alphabet[i] = display.newText(str,0,0, native.systemFont, 18) alphabet[i].x = math.floor(-30 + (55 \* i)) alphabet[i].y = math.floor(-10 + (35 \* j)) alphabet[i].alreadytouched = false alphabet[i].value = L1Map[j][i] alphabet[i].id = i alphabet[i]:addEventListener( "touch", touchMe) end end&nbsp;

Hello,

I think your problem is come from your indentation. We don’t see where the loop of j is apply.

That’s mean you have an ID problem you have many object with the same id.

function touchMe(event) if ( event.phase == "began" ) then event.target.xScale = 1.0 event.target.yScale = 1.0 event.target.alpha = 0.4 elseif (event.phase == "moved") then event.target.xScale = 1.0 event.target.yScale = 1.0 event.target.alpha = 0.4 if (alphabet[event.target.id].alreadytouched == false ) then alphabet[event.target.id].alreadytouched = true assembledWord = assembledWord..event.target.value print(assembledWord) end elseif( event.phase == "ended") then event.target.alpha = 1 if(alphabet[event.target.id].alreadytouched == false ) then alphabet[event.target.id].alreadytouched = true end end end for i = 1, 6 do for j = 1, 6 do local fname = nil if(L1Map[j][i] == 0) then str = sletter[math.random(26)] L1Map[j][i] = str fname = "letters/ltr\_"..str..".png" else fname = "letters/ltr\_"..L1Map[j][i]..".png" end alphabet[i] = newImageRectNoDimensions(fname) alphabet[i].x = math.floor(-30 + (55 \* i)) alphabet[i].y = math.floor(-10 + (35 \* j)) alphabet[i].alreadytouched = false alphabet[i].value = L1Map[j][i] alphabet[i].id = i\*6+j alphabet[i]:addEventListener( "touch", touchMe) end end

remiduchalard, I think you are right. 

In the code alphabet[i] is being changed 6 times for each value of i.  

That whole nested loop needs reworking.

For example this line being inside the j loop changes the image for alphabet[i] 6 times, settling on the last image j=6 

alphabet[i] = newImageRectNoDimensions(fname)

Hi guys, thank you for your help. I already solved the issue. I just added an id and checker for j and it works!