Newb Question:Hit test Object

Hi guys its me again, I have a little problem here regarding on collision detection without physics. See the attached image. Touching a letter and move vertically and horizontally to another letter works well but when touching a letter and move diagonally to another letter it keeps on dragging the sorrounding letters to it. Thanks in advance for answering I really do appreciate it! Cheers!

function hitTestObjects(obj1, obj2) local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin + 10 and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin + 10 local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin - 15 and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax - 15 local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin + 10 and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin + 10 local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin - 15 and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax - 15 return (left or right) and (up or down) and (up or right) end function detectLetters:touch(event) local selectedWord = '' if(event.phase == 'began') then elseif(event.phase == "moved") then for i = 1, tfs.numChildren do if(hitTestObjects(feedBackLines[feedBackLines.numChildren], tfs[i])) then selectedWord = selectedWord..tfs[i].value print(selectedWord) end end end end for i = 1, #L1Map[1] do for j = 1, #L1Map 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] = {} alphabet[i] = newImageRectNoDimensions(fname) alphabet[i].width = 25 alphabet[i].height = 26 alphabet[i]:setFillColor(1,1,1) alphabet[i].x = math.floor(-15 + (35 \* i)) alphabet[i].y = math.floor(50 + (35 \* j)) alphabet[i].value = L1Map[j][i] alphabet[i].alreadytouched = false alphabet[i].alreadytouchedj = false alphabet[i].id = i alphabet[i].idj = j tfs:insert(alphabet[i]) alphabet[i]:addEventListener( "touch", detectLetters ) alphabet[i]:addEventListener( "touch", startDraw ) end end

You are working with rectangles and I can’t tell how you’re creating them. What would be helpful is if you were drawing some colored background in your images so we can see what the boundaries are. Since we don’t know what scale your game is working on, what your newImageRectNoDimensions() is doing, all we can do is guess.

I don’t know if this is what’s happening or not, but this is what seems like is happening:

See how the touch event ends up clipping the B & C blocks? To get from A to D would require a very, very precise event that never hits the other blocks. Also, if the blocks overlap any more and you’re letting touch events propagate through the display hierarchy then its going to hit multiple blocks. 

I also don’t know the behavior of attaching two touch listeners to a given object. Events either propagate if the function returns false or stops if it returns true. But I don’t know how events propagate when they are on the same object.

Rob

You are working with rectangles and I can’t tell how you’re creating them. What would be helpful is if you were drawing some colored background in your images so we can see what the boundaries are. Since we don’t know what scale your game is working on, what your newImageRectNoDimensions() is doing, all we can do is guess.

I don’t know if this is what’s happening or not, but this is what seems like is happening:

See how the touch event ends up clipping the B & C blocks? To get from A to D would require a very, very precise event that never hits the other blocks. Also, if the blocks overlap any more and you’re letting touch events propagate through the display hierarchy then its going to hit multiple blocks. 

I also don’t know the behavior of attaching two touch listeners to a given object. Events either propagate if the function returns false or stops if it returns true. But I don’t know how events propagate when they are on the same object.

Rob