RayCasting Alternatives?

I’m trying to recreate a 2D game that you have to match adjacent colored blocks in order to make them disappear. (Think Pet Rescue Saga from King Games, but only with the color matching concept). The problem that I have is that I currently am using raycasting to “mark” blocks that match which would be taken out when the player clicks on the block a second time; however with raycasting EVERY block is marked, so when the removeSelf function is ran, it takes out ever matched block instead of the single section I want. What I’m trying to get is the check to stop once it hits a block that isn’t matching, rather than skipping it over and continuing on. I was hoping that there would be a function that would be able to single out one group of matching blocks and mark those, ignoring everything else around it. Unfortunately, I can’t post the code I’m using now, but I definitely will when I get home. 

This is the code I’m working with, I know it’s a mess, I’m trying to get the main parts completed before I smooth everything out

testForMatches = function( lastMatches )

local left  = -15

local right =  15

local up    = -15

local down  =  15

touchesAllowed = false

local foundMatch = false

for k,v in pairs( gemGrid ) do

local horizontalHits = 0

local verticalHits = 0 

local leftHits = physics.rayCast( target.x, target.y, 10, 10, “any” ) or {}

local rightHits = physics.rayCast( target.x, target.y, 10, 10, “any” ) or {}

local upHits = physics.rayCast( target.x, target.y,10, 10, “any” ) or {}

local downHits = physics.rayCast( target.x, target.y, 10, 10, “any” ) or {}

local lH = #leftHits

local rH = #rightHits

local uH = #upHits

local dH = #downHits

– Count the horizontal hits

for i = 1, lH do

if(leftHits[i].object.myImageNumber ~= v.myImageNumber) then

break

end

timer.performWithDelay( 150, function() touchesAllowed = true end )

horizontalHits = horizontalHits + 1

end

for i = 1, rH do

if(rightHits[i].object.myImageNumber ~= v.myImageNumber) then

break

end

timer.performWithDelay( 150, function() touchesAllowed = true end )

horizontalHits = horizontalHits + 1

end

– Count the vertical hits

for i = 1, uH do

if(upHits[i].object.myImageNumber ~= v.myImageNumber) then

break

end

timer.performWithDelay( 150, function() touchesAllowed = true end )

verticalHits = verticalHits + 1

end

for i = 1, dH do

if(downHits[i].object.myImageNumber ~= v.myImageNumber) then

break

end

timer.performWithDelay( 150, function() touchesAllowed = true end )

verticalHits = verticalHits + 1

end