[Resolved] Memory Match Game issue

Hi, I am new to Corona and Lua in general. I am going through the Memory Match Tutorial by Daniel on mobile tuts. I see a problem whereI am able to click on the same card twice and it’ll match.

I am using the following code.
[lua]–Set Global width and height variables
_W = display.contentWidth;
_H = display.contentHeight;

–Hide status bar
display.setStatusBar(display.HiddenStatusBar);

–Declare a totalButtons variable to track number of buttons on screen
local totalButtons = 0

–Declare variable to track button select
local secondSelect = 0
local checkForMatch = false

–Declare button, buttonCover, and buttonImages table
local button = {}
local buttonCover = {}
local buttonImages = {1,1, 2,2, 3,3, 4,4, 5,5, 6,6}

–Declare and prime a last button selected variable
local lastButton = display.newImage(“1.png”);
lastButton.myName = 1;

–Notify player if match is found or not
local matchText = display.newText("", 0, 0, native.systemFont, 26)
matchText:setReferencePoint(display.CenterReferencePoint)
matchText:setTextColor(0, 0, 0)
matchText.x = _W/2

–Set starting point for button grid
x = -20

–insert background
local bg = display.newImage(“bg.png”)
–add a bumber for each card. Used so we dont select same card for match
–Set up game function
function game(object, event)
if(event.phase == “began”) then
if(checkForMatch == false and secondSelect == 0) then
–Flip over first button
buttonCover[object.number].isVisible = false;
lastButton = object
checkForMatch = true
elseif(checkForMatch == true) then
if(secondSelect == 0) then
–Flip over second button
buttonCover[object.number].isVisible = false;
secondSelect = 1;
–If buttons do not match, flip buttons over
if(lastButton.myName ~= object.myName) then
matchText.text = “Match Not Found!”;
timer.performWithDelay(200, function()
matchText.text = " ";
checkForMatch = false;
secondSelect = 0;
buttonCover[lastButton.number].isVisible = true;
buttonCover[object.number].isVisible = true;
end, 1)
–If buttons DO match, remove button
elseif(lastButton.myName == object.myName) then
–if same uniqueName clicked, then dont do anything

matchText.text = “Match Found!”;
timer.performWithDelay(200, function()
matchText.text = “”;
checkForMatch = false;
secondSelect = 0;
lastButton:removeSelf();
object:removeSelf();
buttonCover[lastButton.number]:removeSelf();
buttonCover[object.number]:removeSelf();
end, 1)

end
end
end
end
end

–Place buttons on screen
for count = 1,3 do
x = x + 90
y = 20

for insideCount = 1,4 do
y = y + 90

–Assign each image a random location on grid
temp = math.random(1,#buttonImages)
–print(temp);

button[count] = display.newImage(buttonImages[temp] … “.png”);

–Position the button
button[count].x = x;
button[count].y = y;

–Give each a button a name
button[count].myName = buttonImages[temp]
button[count].number = totalButtons

–Remove button from buttonImages table
table.remove(buttonImages, temp)

–Set a cover to hide the button image
buttonCover[totalButtons] = display.newImage(“blankCard.png”);
buttonCover[totalButtons].x = x; buttonCover[totalButtons].y = y;
totalButtons = totalButtons + 1

–Attach listener event to each button
button[count].touch = game
button[count]:addEventListener( “touch”, button[count] )
end
end[/lua]

I see that the problem is that to match the cards the card needs the same name/ID. But therein is the problem that allows you to click the same card twice.

My question is, how do I go about making sure that the same card isnt clicked twice. I tried looking around the forum, google, and whatnot and could not find a resolution. Any help? [import]uid: 85045 topic_id: 15980 reply_id: 315980[/import]

just set a flag on the card as clicked, so you cannot click it again if it is already clicked.

read up on this article here

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15980 reply_id: 59233[/import]

Yes i made it that way in my Match’em Party game…

Also if you use animations you should have a global flag to get out of tap events if the animation is running, and when finished you get the flag on and people can tap again (at least it work well on my game that way) [import]uid: 75034 topic_id: 15980 reply_id: 59236[/import]

Thanks JayantV.

forgive my newbishness, but I am totally new to this.

I guess I could do something here:
[lua]–Flip over first button
buttonCover[object.number].isVisible = false;
lastButton = object
checkForMatch = true [/lua]
and add something like [lua]lastButton.isChecked = true[/lua] and then here: [lua]–Flip over second button
buttonCover[object.number].isVisible = false;
secondSelect = 1; [/lua] add the if lastButton.isChecked = true then end? or use some kind of goto(dont know if lua has that) to return back up top to recheck new card. [import]uid: 85045 topic_id: 15980 reply_id: 59235[/import]

Ok, i got it now.

Removed event listener here
[lua] if(checkForMatch == false and secondSelect == 0) then
–Flip over first button
buttonCover[object.number].isVisible = false;
lastButton = object
checkForMatch = true
lastButton:removeEventListener( “touch”, lastButton )[/lua]
and readded it in the un-matched portion and matched portion.ex:
[lua]lseif(checkForMatch == true) then
if(secondSelect == 0) then
–Flip over second button
buttonCover[object.number].isVisible = false;
secondSelect = 1;
–If buttons do not match, flip buttons over
if(lastButton.myName ~= object.myName) then
lastButton:addEventListener( “touch”, lastButton )[/lua]

This seems to work fine. Thanks for all the help gents. [import]uid: 85045 topic_id: 15980 reply_id: 59240[/import]

idkokos, is that like the Wolfenstein/Doom mod of Cocos? :wink: forget that joke…

right, I will give you some pointers so you can read and learn about it.

the touch event has a target, the object that is touched.
just use in the touch began phase

local target = event.target  
  
if target.isFlipped == true then return true  
  
 target.isFlipped = true  

Hope that gives you some direction to pursue and learn.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15980 reply_id: 59241[/import]

OK,

So being new to lua and Corona and put together this game from various tutorials. I wanted to release this for Halloween. But ran into some issues. So here is the code. The main card game is from the mobile tuts tutorial on a memory game, and the blood splatter effects are from the Fruit Samurai example. Took me about a week, which is pretty good considering how new I am. The best way to learn is just to take examples and add to them and see what works.

Also, please comment if you find a better way to do things, this may be the ugliest coding you’ve ever seen.

[lua]module(…, package.seeall)
require (“physics”)
require(“ui”)
require (“director”)

– Zombie Match
function new()
local localGroup = display.newGroup()

physics.start()
– physics.setDrawMode ( “hybrid” ) – Uncomment in order to show in hybrid mode
physics.setGravity( 0, 9.8 * 2)

–count to see if game is done
local isDone = 0

–Set Global width and height variables
_W = display.contentWidth;
_H = display.contentHeight;

–Hide status bar
display.setStatusBar(display.HiddenStatusBar);

–Declare a totalButtons variable to track number of buttons on screen
local totalButtons = 0

–Declare variable to track button select
local secondSelect = 0
local checkForMatch = false

–Declare button, buttonCover, and buttonImages table
local button = {}
local buttonCover = {}
local buttonImages = {1,1, 2,2, 3,3, 4,4, 5,5, 6,6}

–Declare and prime a last button selected variable
local lastButton = display.newImage(“1.png”);
lastButton.myName = 1;
localGroup:insert(lastButton)

–Notify player if match is found or not
local matchText = display.newText("", 0, 0, native.systemFont, 26)
matchText:setReferencePoint(display.CenterReferencePoint)
matchText:setTextColor(0, 0, 0)
matchText.x = _W/2
matchText.y = _H/2

–Set starting point for button grid
x = -20

–insert background
local bg = display.newImage(“bg.png”)
localGroup:insert(bg)

– Audio for exploded zombies
local blood = audio.loadSound(“blood.wav”)

–audio loading
local cardFlip= audio.loadSound(“zombieBite.wav”)
local zombieMoan = audio.loadSound(“zombieMoan.wav”) --endgame

– Gush filter should not interact with other stuff
local gushProp = {density = 1.0, friction = 0.3, bounce = 0.2, filter = {categoryBits = 4, maskBits = 8} }

– Splash properties
local splashFadeTime = 2500
local splashFadeDelayTime = 5000
local splashInitAlpha = .5
local splashSlideDistance = 50 – The amoutn of of distance the splash slides down the background

– Contains all the available splash images
local splashImgs = {}

– Gush properties
local minGushRadius = 10
local maxGushRadius = 25
local numOfGushParticles = 15
local gushFadeTime = 500
local gushFadeDelay = 500

local minGushVelocityX = -350
local maxGushVelocityX = 350
local minGushVelocityY = -350
local maxGushVelocityY = 350

– Groups for holding the fruit and splash objects
local splashGroup
local fruitGroup
splashGroup = display.newGroup()
fruitGroup = display.newGroup()
– Initialize splash images
table.insert(splashImgs, “splash1.png”)
table.insert(splashImgs, “splash2.png”)
table.insert(splashImgs, “splash3.png”)

localGroup:insert(splashGroup)
localGroup:insert(fruitGroup)


– The game function

–Set up game function
function game(object, event)
if(event.phase == “began”) then
if(checkForMatch == false and secondSelect == 0) then
–Flip over first button
buttonCover[object.number].isVisible = false;
lastButton = object
checkForMatch = true
audio.play(cardFlip)
lastButton:removeEventListener( “touch”, lastButton )

elseif(checkForMatch == true) then
if(secondSelect == 0) then
–Flip over second button
buttonCover[object.number].isVisible = false;
secondSelect = 1;
audio.play(cardFlip)
–If buttons do not match, flip buttons over
if(lastButton.myName ~= object.myName) then
lastButton:addEventListener( “touch”, lastButton )
–matchText.text = “Match Not Found!”;
timer.performWithDelay(200, function()
matchText.text = " ";
checkForMatch = false;
secondSelect = 0;
buttonCover[lastButton.number].isVisible = true;
buttonCover[object.number].isVisible = true;
end, 1)
–If buttons DO match, remove button
elseif(lastButton.myName == object.myName) then
lastButton:addEventListener( “touch”, lastButton )
–matchText.text = “Match Found!”;
isDone = isDone+1
timer.performWithDelay(200, function()
matchText.text = “”;
checkForMatch = false;
secondSelect = 0;
createSplash(lastButton)
audio.play(blood)
createGush(lastButton)
createSplash(object)
createGush(object)
lastButton:removeSelf();
object:removeSelf();
buttonCover[lastButton.number]:removeSelf();
buttonCover[object.number]:removeSelf();

end, 1)
–if done then display congrats
if(isDone == 6) then

timer.performWithDelay(1000,function()
matchText.text = “You’ve killed them all!”
localGroup:insert(matchText)
end,1)

end

end
end
end
end

end

–Place buttons on screen
for count = 1,3 do
x = x + 90
y = 20

for insideCount = 1,4 do
y = y + 90

–Assign each image a random location on grid
temp = math.random(1,#buttonImages)
–print(temp);

button[count] = display.newImage(buttonImages[temp] … “.png”);
–Position the button
button[count].x = x;
button[count].y = y;
–Give each a button a name
button[count].myName = buttonImages[temp]
localGroup:insert(button[count])

button[count].number = totalButtons

–Remove button from buttonImages table
table.remove(buttonImages, temp)

–Set a cover to hide the button image
buttonCover[totalButtons] = display.newImage(“blankCard.png”);
localGroup:insert(buttonCover[totalButtons])
buttonCover[totalButtons].x = x; buttonCover[totalButtons].y = y;
totalButtons = totalButtons + 1

–Attach listener event to each button
button[count].touch = game
button[count]:addEventListener( “touch”, button[count] )
end
end


– blood splatter effects
–example from fruit samurai

function getRandomSplash()

return display.newImage(splashImgs[math.random(1, #splashImgs)])
end

– Creates a gushing effect that makes it look like juice is flying out of the fruit
function createGush(fruit)

local i
for i = 0, numOfGushParticles do
local gush = display.newCircle( fruit.x, fruit.y, math.random(minGushRadius, maxGushRadius) )
gush:setFillColor(255, 0, 0, 255)

gushProp.radius = gush.width / 2
physics.addBody(gush, “dynamic”, gushProp)

local xVelocity = math.random(minGushVelocityX, maxGushVelocityX)
local yVelocity = math.random(minGushVelocityY, maxGushVelocityY)

gush:setLinearVelocity(xVelocity, yVelocity)

transition.to(gush, {time = gushFadeTime, delay = gushFadeDelay, width = 0, height = 0, alpha = 0, onComplete = function(event) gush:removeSelf() end})
end

end

function createSplash(fruit)

local splash = getRandomSplash()
splash.x = fruit.x
splash.y = fruit.y
splash.rotation = math.random(-90,90)
splash.alpha = splashInitAlpha
splashGroup:insert(splash)

transition.to(splash, {time = splashFadeTime, alpha = 0, y = splash.y + splashSlideDistance, delay = splashFadeDelayTime, onComplete = function(event) splash:removeSelf() end})

end

– title image
local title = display.newImage(“titleSmall2.png”)
title.x = _W/2
title.y = 40
localGroup:insert(title)

–backbutton
local backButton = display.newImage(“back.png”)
backButton.x = 260
backButton.y = 460
localGroup:insert(backButton)

–back button function
local function pressBack(event)
if event.phase == “ended” then
director:changeScene (“menu”,“fade”)
end
end
backButton:addEventListener (“touch”, pressBack)

return localGroup;
end[/lua] [import]uid: 85045 topic_id: 15980 reply_id: 60614[/import]

v [import]uid: 85045 topic_id: 15980 reply_id: 59244[/import]

idkokos ,

I tried to run it , I could not ,
would you please upload all the sample code with images , or email it to me

Eng.tareqali@hotmail.com

thank u [import]uid: 13061 topic_id: 15980 reply_id: 88965[/import]