Need help with speed Matching Game logic

We use physics because it keeps the objects from touching each other. “Static” means the spawned object stays in place; “dynamic” enables it to move if it collides with another object. It’s handy because you can set a radius value that gives each item its own personal space.

For the matching images, I ended up pulling those from a separate table altogether. It’s kludgy, but I did this because I knew that I was going to include a two player version. 

I only worry about the 2x images for the application bundle (icons, App Store graphics, etc). For in-game assets, I usually just start out with a high-res image to begin with, then just scale down using :scale( x%, y%). Do a Google search for ultimate config.lua file, and it should take care of the rest (at least for iOS).

local matchingimages = {    ...your objects } randomMatchImage = matchingimages[math.random(#matchingimages)] --pick 1 item from matchingimages table above filename1 = randomMatchImage filename2 = filename1 --make filename2 a copy of filename1 math.randomseed(os.time()) local function showMatchItem1()     item1 = display.newImage(filename1)     item1:scale(.2, .2)     item1.x, item.y = etc... --spawn on left half of screen end              local function showMatchItem2()      item2 = display.newImage(filename2)     item2:scale(.2, .2)     item1.x, item.y = etc... --spawn on right half of screen end              showMatchItem1() showMatchItem2()  

  

Thanks marlon,

i will bother you with more questions :slight_smile:

1- so you have two sets of images … one for matching and one for non matching. ?? is this because you dont want to repeat the images between sets. 

2-for physics : still not clear… how do you start them as dynamic ?? normally they fall down if you do so ??

3- how they make the effect around the selected object when you click the right match ? i mean the bubble ones ?

regards

Abdul

I used two separate arrays for organizational purposes. When I first started building my game, I wasn’t sure how to create the loop to exclude the images that were already used. But you can also use something like this to generate a matching pair instead:

local function makeMatchingPair()

    local thing = display.newImageRect(“ball.png”, 50, 50)

    thing.x, thing.y = math.random(30, 400), math.random(50, display.contentHeight-50)

end

 

timer.performWithDelay(0, makeMatchingPair, 2) --2 here means spawn two of them

Your physics objects only fall down if you invoke the physics.setGravity() method and enter some values for it. Otherwise, even when a physics object is designated as “dynamic”, it will stay put unless affected by other nearby objects.

There is a nice sparkle effect on the page below. You just attach it to the buttonlistener at event.x and event.y

https://developer.coronalabs.com/code/simple-particle-emitter-and-fireworks-show

Thabks sir for sharing the info.
So I should not start the physics … correct?
I tried to include physics wihout
physics.setGravity() but still they fall down.
Thx
Abdul

My bad. You do need to setGravity but it needs to look like this:

physics.setGravity(0, 0)

no issue sir :slight_smile: … thanks for you help…

i will play with the code and see how it will work  :)

regards

Abdul

Here’s one for you, Jay.

In making my two player version, I am using the same logic to distribute the objects around screen as in the 1-player version. I have a vertical line (also a physics object) down the middle of the screen that splits the screen in two.

My problem is sometimes it only spawns one item on the lefthand (Player 1) side, but then the rest of the objects on the righthand (Player 2) side, and vice versa. Any idea how to distribute them more evenly across both sides using code? As you know, I am randomly generating objects using this code from an earlier post above:

nonMatchImageContainer[i].x = math.random(50, display.contentWidth - 50)  nonMatchImageContainer[i].y = math.random(50, display.contentHeight - 50)

Thanks,

Marlon