Hi everyone. Please I’ve been learning how to use Corona over the past two weeks, and I’m practising now with already written code to gain experience. I need help with a template for a Gem Matcher game found here. The thing is I’d like to substitute the circles that come by default with custom images, but after lots of trials and errors I can’t seem to get it working in the same way. I think circles and images classes behave differently or something, but cannot work it out. If someone points me in the right direction I’ll be quite grateful. Thanks in advance! 
I would say learning by modifying existing complex code is rather not good way.
There isn’t that much of a difference between circles and images. But be aware of the ones that are.
When you create a circle, you tell it the X, Y of where you want the circle to appear and how big the circle is.
For images, you provide an image name and the size of the image’s width and height (use display.newImageRect()) but you have to then explicitly set the X, Y of the image afterwards:
local myImage = display.newImageRect(“mycustomimage.png”, 128, 132)
myImage.x = 100
myImage.y = 120
vs.
local myCircle = display.newCircle(100, 120, 64) – keep in mind the size of a circle is it’s radius, or half the intended size so this will end up be a 128px wide circle)
After that you move them, fade them, rotate them, add them to groups the same way.
Hi, Rob. Thanks for your reply. I think my main problem is in this piece of code:
local function newGem (i,j) local newGem newGem = display.newCircle(i\*40-20, -60, 20) newGem.i = i newGem.j = j newGem.isMarkedToDestroy = false newGem.destination\_y = j\*40+60 local R = mRandom(1,4) if (R == 1 ) then newGem:setFillColor( 200, 0, 0 ) newGem.gemType = "red" elseif (R == 2 ) then newGem:setFillColor( 0, 200, 0 ) newGem.gemType = "green" elseif (R == 3 ) then newGem:setFillColor( 0, 0, 200 ) newGem.gemType = "blue" elseif (R == 4 ) then newGem:setFillColor( 200, 200, 0 ) newGem.gemType = "yellow" end --new gem falling animation transition.to( newGem, { time=100, y= newGem.destination\_y} ) groupGameLayer:insert( newGem ) newGem.touch = onGemTouch newGem:addEventListener( "touch", newGem ) return newGem end
This is what generates random color circles using setFillColor from a blank one. The issue is that I can’t find a way to apply something like this to newImageRect, so the code can give me random images in the correct position.
You would need four different image files, one for each color and when you call display.newImageRect() pass it the name of the color that you want like “green.png” or “yellow.png”. In the above code, they are choosing the color after the circle is created for this to work for you, you will have to randomly pick the image file before you load the image.
Ok, Rob. Thanks for your help. I finally managed to get it working today. In case someone has a similar problem I’ll post the solution.
local function newGem (i,j) local newGem local R = mRandom(1,4) if (R == 1 ) then newGem = display.newImageRect( "images/bred.png", 40, 40) newGem.gemType = "red" elseif (R == 2 ) then newGem = display.newImageRect( "images/bgreen.png", 40, 40) newGem.gemType = "green" elseif (R == 3 ) then newGem = display.newImageRect( "images/bblue.png", 40, 40) newGem.gemType = "blue" elseif (R == 4 ) then newGem = display.newImageRect( "images/byellow.png", 40, 40) newGem.gemType = "yellow" end newGem.x = i\*40-20 newGem.y = -60 newGem.i = i newGem.j = j newGem.isMarkedToDestroy = false newGem.destination\_y = j\*40+60 transition.to( newGem, { time=100, y= newGem.destination\_y} ) groupGameLayer:insert( newGem ) newGem.touch = onGemTouch newGem:addEventListener( "touch", newGem ) return newGem end
I would say learning by modifying existing complex code is rather not good way.
There isn’t that much of a difference between circles and images. But be aware of the ones that are.
When you create a circle, you tell it the X, Y of where you want the circle to appear and how big the circle is.
For images, you provide an image name and the size of the image’s width and height (use display.newImageRect()) but you have to then explicitly set the X, Y of the image afterwards:
local myImage = display.newImageRect(“mycustomimage.png”, 128, 132)
myImage.x = 100
myImage.y = 120
vs.
local myCircle = display.newCircle(100, 120, 64) – keep in mind the size of a circle is it’s radius, or half the intended size so this will end up be a 128px wide circle)
After that you move them, fade them, rotate them, add them to groups the same way.
Hi, Rob. Thanks for your reply. I think my main problem is in this piece of code:
local function newGem (i,j) local newGem newGem = display.newCircle(i\*40-20, -60, 20) newGem.i = i newGem.j = j newGem.isMarkedToDestroy = false newGem.destination\_y = j\*40+60 local R = mRandom(1,4) if (R == 1 ) then newGem:setFillColor( 200, 0, 0 ) newGem.gemType = "red" elseif (R == 2 ) then newGem:setFillColor( 0, 200, 0 ) newGem.gemType = "green" elseif (R == 3 ) then newGem:setFillColor( 0, 0, 200 ) newGem.gemType = "blue" elseif (R == 4 ) then newGem:setFillColor( 200, 200, 0 ) newGem.gemType = "yellow" end --new gem falling animation transition.to( newGem, { time=100, y= newGem.destination\_y} ) groupGameLayer:insert( newGem ) newGem.touch = onGemTouch newGem:addEventListener( "touch", newGem ) return newGem end
This is what generates random color circles using setFillColor from a blank one. The issue is that I can’t find a way to apply something like this to newImageRect, so the code can give me random images in the correct position.
You would need four different image files, one for each color and when you call display.newImageRect() pass it the name of the color that you want like “green.png” or “yellow.png”. In the above code, they are choosing the color after the circle is created for this to work for you, you will have to randomly pick the image file before you load the image.
Ok, Rob. Thanks for your help. I finally managed to get it working today. In case someone has a similar problem I’ll post the solution.
local function newGem (i,j) local newGem local R = mRandom(1,4) if (R == 1 ) then newGem = display.newImageRect( "images/bred.png", 40, 40) newGem.gemType = "red" elseif (R == 2 ) then newGem = display.newImageRect( "images/bgreen.png", 40, 40) newGem.gemType = "green" elseif (R == 3 ) then newGem = display.newImageRect( "images/bblue.png", 40, 40) newGem.gemType = "blue" elseif (R == 4 ) then newGem = display.newImageRect( "images/byellow.png", 40, 40) newGem.gemType = "yellow" end newGem.x = i\*40-20 newGem.y = -60 newGem.i = i newGem.j = j newGem.isMarkedToDestroy = false newGem.destination\_y = j\*40+60 transition.to( newGem, { time=100, y= newGem.destination\_y} ) groupGameLayer:insert( newGem ) newGem.touch = onGemTouch newGem:addEventListener( "touch", newGem ) return newGem end