Memory Match game with Director Class

Greetings.

I would like to integrate this common Memory Match game into Director Class.

The code below is page two (screen2.lua) of a three page test.

For the purposes of this test, pages one and three (screen1.lua and screen3.lua) are currently a background image and nav buttons only.

I’d like to put a new Memory Match puzzle on each page.

The background and nav buttons function normally across all three pages. But the puzzle will not stay within its own Director page.

Any help would be greatly appreciated.

Thanks!

[lua]module(…, package.seeall)

– Main function - MUST return a display.newGroup()
new = function ()
local localGroup = display.newGroup()

–BACKGROUND

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


– MEMORY MATCH

–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 = {7,7, 8,8, 9,9, 10,10, 11,11, 12,12}

–Declare Chfoing
local chfoing = audio.loadSound(“chfoing.wav”)
local playSound = function()
audio.play(chfoing)
end

–Declare Woo
local woo = audio.loadSound(“woo.wav”)
local playWoo = function()
audio.play(woo)
end

–Notify player if match is found or not
local matchText = display.newText(" ", 160, 05, native.systemFont, 26)
matchText:setReferencePoint(display.CenterReferencePoint)
matchText:setTextColor(255, 255, 255)
–matchText.x = _W/2
–Set starting point for button grid
x = -20

–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 and lastButton ~= object) 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 = “Try Again!”;
timer.performWithDelay(1250, function()
matchText.text = " ";
checkForMatch = false;
secondSelect = 0;
buttonCover[lastButton.number].isVisible = true;
buttonCover[object.number].isVisible = true;

playWoo();

end, 1)

–If buttons DO match, remove buttons
elseif(lastButton.myName == object.myName) then
matchText.text = “Good Job!”;
timer.performWithDelay(1250, function()
matchText.text = " ";
checkForMatch = false;
secondSelect = 0;
lastButton:removeSelf();
object:removeSelf();
buttonCover[lastButton.number]:removeSelf();
buttonCover[object.number]:removeSelf();

playSound();

end, 1)
end
end
end
end
end

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

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

–Assign each image a random location on grid
temp = math.random(1,#buttonImages)
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(“button.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

– NEW PUZZLE BUTTON 1

local newPuzzle = display.newImage(“p1.png”)
local function newPuzzlet ( event )
if event.phase == “ended” then
director:changeScene(“screen1”,“moveFromLeft”)

end
end
newPuzzle:addEventListener(“touch”,newPuzzlet)
newPuzzle.x = 30
newPuzzle.y = 464
localGroup:insert(newPuzzle)

– NEW PUZZLE BUTTON 2

local new2Puzzle = display.newImage(“p2.png”)
–local function new2Puzzlet ( event )
–if event.phase == “ended” then
– director:changeScene(“screen2”,“moveFromLeft”)
–end
–end
–new2Puzzle:addEventListener(“touch”,new2Puzzlet)
new2Puzzle.x = 95
new2Puzzle.y = 463
localGroup:insert(new2Puzzle)
– NEW PUZZLE BUTTON 3

local new3Puzzle = display.newImage(“p3.png”)
local function new3Puzzlet ( event )
if event.phase == “ended” then
director:changeScene(“screen3”,“moveFromRight”)
end
end
new3Puzzle:addEventListener(“touch”,new3Puzzlet)
new3Puzzle.x = 160
new3Puzzle.y = 465
localGroup:insert(new3Puzzle)

– MUST return a display.newGroup()
return localGroup
end
[lua] [import]uid: 10653 topic_id: 29803 reply_id: 329803[/import]