[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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
--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

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,

?:)

Thanks JayantV.

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

I guess I could do something here:

1
2
3
4
--Flip over first button
                        buttonCover[object.number].isVisible = false;
                        lastButton = object
                        checkForMatch = true        

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)

Ok, i got it now.

Removed event listener here

1
2
3
4
5
6
     if(checkForMatch == false and secondSelect == 0) then
                        --Flip over first button
                        buttonCover[object.number].isVisible = false;
                        lastButton = object
                        checkForMatch = true        
                                                lastButton:removeEventListener( "touch", lastButton )

idkokos, is that like the Wolfenstein/Doom mod of Cocos? ;) 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

1
2
3
4
5
local target = event.target
 
if target.isFlipped == true then return true
 
 target.isFlipped = true

v

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
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
views:1572 update:2011/10/19 14:58:09
corona forums © 2003-2011