I am trying to match images on Corona. I created images and matches array variables and insert the display objects inside those arrays. However, i can’t figure it out how i will be able to move one of the same objects onto other and make it disappear. Can you help me out? Thanks.
Here is my code so far:
local images={}
local matches={}
local function onTouch( event )
local t = event.target
local phase = event.phase
if "began" == phase then
-- Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
t.isFocus = true
elseif t.isFocus then
if "moved" == phase then
t.x = event.x
t.y = event.y
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
return true
end
local arguments =
{
{img="img1.png" , x=30, y=30 },
{img="img2.png", x=100, y=30},
{img= "img3.png",x=170, y=30},
}
local arguments2={
{img= "img1.png",x=30, y=150},
{img= "img2.png",x=100, y=150},
{img= "img3.png",x=170, y=150}
}
for _,item in ipairs( arguments ) do
local imgg = display.newImage( item.img,item.x,item.y )
table.insert(images,imgg)
-- Make the button instance respond to touch events
imgg:addEventListener( "touch", onTouch )
end
for _,item in ipairs( arguments2 ) do
local imgg = display.newImage( item.img,item.x,item.y )
table.insert(matches,imgg)
-- Make the button instance respond to touch events
imgg:addEventListener( "touch", onTouch )
end
It would be easiest to assign a type or the like to each image, eg;
{img=”img1.png” , x=30, y=30, type=”red” }
Then when it is released check whether or not it is within the content bounds of the matching image in the other arguments table using object.contentBounds (you can find detailed info about this on the Corona Labs API page.)