Hi, i try to make a memory game using corona. I doesn’t how to make the image disappear after it matched. I wish to add sound according to the image that flipped. The code that i using is from the online sample code.
--------------------------------------------------------------------------------- -- -- scene1.lua -- --------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- local image,text1,belajar,bermain -- Touch event listener for background image local numPages = 1 -- number of pages in the project local sceneGroup = nil -- main group for all page elements local W = display.contentWidth; -- full width of the page local H = display.contentHeight; -- full height of the page local curPage = 1 -- number of the current page -- Button names local but\_cover1 local but\_cover2 local but\_cover3 local but\_cover4 local but\_cover5 local but\_cover6 local but\_cover7 local but\_cover8 -- Action names local act\_completed -- Variables names local flipped = {} -- holds the flipped cards local flips = 1 -- Holds the number of flips (max 2) local tList = {1,1,2,2,3,3,4,4} -- Holds the number of each card local randList = {} -- Holds the random list of cards in the board local isCompleted = false -- local paired = 0 -- local totPairs = 4 -- math.randomseed(os.time()) -- All layers on a table local layer = {} -- All audio files on a table local allAudios = {} local function homeButtonTouch( self, event ) if event.phase == "began" then composer.gotoScene( "menu", "fade", 400 ) return true end end -- Calculates anchor points local function repositionAnchor( object, newAnchorX, newAnchorY ) local origX = object.x; local origY = object.y if newAnchorX ~= 0.5 or newAnchorY ~= 0.5 then local width = object.width; local height = object.height local xCoord = width \* (newAnchorX - .5) local yCoord = height \* (newAnchorY - .5) object.x = origX + xCoord; object.y = origY + yCoord object.oriX = object.x; object.oriY = object.y end end -- Called when the scene's view does not exist: function scene:create( event ) local sceneGroup = self.view homeButton = display.newImage( "button/home.png" ) homeButton.x = 960 homeButton.y = 50 sceneGroup:insert( homeButton ) homeButton.touch = homeButtonTouch print( "\n1: create event") -- BEFORE variables flipped = {} -- holds the flipped cards flips = 1 -- Holds the number of flips (max 2) tList = {1,1,2,2,3,3,4,4} -- Holds the number of each card randList = {} -- Holds the random list of cards in the board isCompleted = false -- paired = 0 -- totPairs = 4 -- -- Capture and set group position local function groupPos(obj) local minX, minY for i = 1, obj.numChildren do local currentRecord = obj[i] if i == 1 then minX = currentRecord.x - currentRecord.contentWidth \* 0.5 minY = currentRecord.y - currentRecord.contentHeight \* 0.5 end local mX = currentRecord.x - currentRecord.contentWidth \* 0.5 if mX \< minX then minX = mX end local mY = currentRecord.y - currentRecord.contentHeight \* 0.5 if mY \< minY then minY = mY end end obj.x = minX + obj.contentWidth \* 0.5 obj.y = minY + obj.contentHeight \* 0.5 end -- Multilingual items -- (TOP) External code will render here -- Shuffle cards while #tList ~= 0 do local ind = math.random(#tList) table.insert(randList, tList[ind]) table.remove(tList,ind) end -- Positions each photo local cards = { {141,189}, {387,189}, {635,189}, {881,189}, {141, 378}, {387,378}, {635,378}, {881,378} } --olds each card position in the table local card = {} -- table to draw each card for i = 1,#randList do card[i] = display.newImageRect( composer.imgDir.. "p1\_photo"..randList[i]..".png", 220, 165 ); card[i].x = cards[i][1] card[i].y = cards[i][2] sceneGroup:insert(card[i]) end for i,v in ipairs(randList) do print(i,v) end -- Layer\_1 positioning layer.Layer\_1 = display.newImage( "background/UI\_sebutanABC.png" ) layer.Layer\_1.oriX = layer.Layer\_1.x; layer.Layer\_1.oriY = layer.Layer\_1.y layer.Layer\_1.oriXs = layer.Layer\_1.xScale; layer.Layer\_1.oriYs = layer.Layer\_1.yScale layer.Layer\_1.alpha = 1; layer.Layer\_1.oldAlpha = 1 sceneGroup:insert( 1, layer.Layer\_1); sceneGroup.Layer\_1 = layer.Layer\_1 -- cover1 positioning layer.cover1 = display.newImageRect( composer.imgDir.. "p1\_cover1.png", 220, 165 ); layer.cover1.x = 141; layer.cover1.y = 189; layer.cover1.alpha = 1; layer.cover1.oldAlpha = 1 layer.cover1.oriX = layer.cover1.x; layer.cover1.oriY = layer.cover1.y layer.cover1.oriXs = layer.cover1.xScale; layer.cover1.oriYs = layer.cover1.yScale layer.cover1.name = "cover1" sceneGroup:insert(layer.cover1); sceneGroup.cover1 = layer.cover1 -- cover2 positioning layer.cover2 = display.newImageRect( composer.imgDir.. "p1\_cover1.png", 220, 165 ); layer.cover2.x = 387; layer.cover2.y = 189; layer.cover2.alpha = 1; layer.cover2.oldAlpha = 1 layer.cover2.oriX = layer.cover2.x; layer.cover2.oriY = layer.cover2.y layer.cover2.oriXs = layer.cover2.xScale; layer.cover2.oriYs = layer.cover2.yScale layer.cover2.name = "cover2" sceneGroup:insert(layer.cover2); sceneGroup.cover2 = layer.cover2 -- cover3 positioning layer.cover3 = display.newImageRect( composer.imgDir.. "p1\_cover1.png", 220, 165 ); layer.cover3.x = 635; layer.cover3.y = 189; layer.cover3.alpha = 1; layer.cover3.oldAlpha = 1 layer.cover3.oriX = layer.cover3.x; layer.cover3.oriY = layer.cover3.y layer.cover3.oriXs = layer.cover3.xScale; layer.cover3.oriYs = layer.cover3.yScale layer.cover3.name = "cover3" sceneGroup:insert(layer.cover3); sceneGroup.cover3 = layer.cover3 -- cover4 positioning layer.cover4 = display.newImageRect( composer.imgDir.. "p1\_cover1.png", 220, 165 ); layer.cover4.x = 881; layer.cover4.y = 189; layer.cover4.alpha = 1; layer.cover4.oldAlpha = 1 layer.cover4.oriX = layer.cover4.x; layer.cover4.oriY = layer.cover4.y layer.cover4.oriXs = layer.cover4.xScale; layer.cover4.oriYs = layer.cover4.yScale layer.cover4.name = "cover4" sceneGroup:insert(layer.cover4); sceneGroup.cover4 = layer.cover4 -- cover5 positioning layer.cover5 = display.newImageRect( composer.imgDir.. "p1\_cover1.png", 220, 165 ); layer.cover5.x = 141; layer.cover5.y = 378; layer.cover5.alpha = 1; layer.cover5.oldAlpha = 1 layer.cover5.oriX = layer.cover5.x; layer.cover5.oriY = layer.cover5.y layer.cover5.oriXs = layer.cover5.xScale; layer.cover5.oriYs = layer.cover5.yScale layer.cover5.name = "cover5" sceneGroup:insert(layer.cover5); sceneGroup.cover5 = layer.cover5 -- cover6 positioning layer.cover6 = display.newImageRect( composer.imgDir.. "p1\_cover1.png", 220, 165 ); layer.cover6.x = 387; layer.cover6.y = 378; layer.cover6.alpha = 1; layer.cover6.oldAlpha = 1 layer.cover6.oriX = layer.cover6.x; layer.cover6.oriY = layer.cover6.y layer.cover6.oriXs = layer.cover6.xScale; layer.cover6.oriYs = layer.cover6.yScale layer.cover6.name = "cover6" sceneGroup:insert(layer.cover6); sceneGroup.cover6 = layer.cover6 -- cover7 positioning layer.cover7 = display.newImageRect( composer.imgDir.. "p1\_cover1.png", 220, 165 ); layer.cover7.x = 635; layer.cover7.y = 378; layer.cover7.alpha = 1; layer.cover7.oldAlpha = 1 layer.cover7.oriX = layer.cover7.x; layer.cover7.oriY = layer.cover7.y layer.cover7.oriXs = layer.cover7.xScale; layer.cover7.oriYs = layer.cover7.yScale layer.cover7.name = "cover7" sceneGroup:insert(layer.cover7); sceneGroup.cover7 = layer.cover7 -- cover8 positioning layer.cover8 = display.newImageRect( composer.imgDir.. "p1\_cover1.png", 220, 165 ); layer.cover8.x = 881; layer.cover8.y = 378; layer.cover8.alpha = 1; layer.cover8.oldAlpha = 1 layer.cover8.oriX = layer.cover8.x; layer.cover8.oriY = layer.cover8.y layer.cover8.oriXs = layer.cover8.xScale; layer.cover8.oriYs = layer.cover8.yScale layer.cover8.name = "cover8" sceneGroup:insert(layer.cover8); sceneGroup.cover8 = layer.cover8 -- text positioning layer.text = display.newImageRect( composer.imgDir.. "p1\_text.png", 305, 30 ); layer.text.x = 184; layer.text.y = 47; layer.text.alpha = 1; layer.text.oldAlpha = 1 layer.text.oriX = layer.text.x; layer.text.oriY = layer.text.y layer.text.oriXs = layer.text.xScale; layer.text.oriYs = layer.text.yScale layer.text.name = "text" sceneGroup:insert( layer.text); sceneGroup.text = layer.text end function scene:show( event ) local phase = event.phase if "did" == phase then print( "1: show event, phase did" ) -- remove previous scene's view -- Update Lua memory text display homeButton:addEventListener( "touch", homeButton ) -- Check for previous bookmarks if (tonumber(composer.kBookmark) == 1) then local path = system.pathForFile( "book.txt", system.DocumentsDirectory ) local file = io.open( path, "w+" ) file:write ( curPage.."\n1" ) io.close( file ) end -- Check for for auto play settings if (tonumber(composer.kAutoPlay) \> 0) then local function act\_autoPlay(event) if(curPage \< numPages) then if(composer.kBidi == false) then composer.gotoScene( "page\_"..curPage+1, { effect = "fromRight"} ) else composer.gotoScene( "page\_"..curPage-1, { effect = "fromLeft"} ) end end end composer.timerStash.timer\_AP = timer.performWithDelay( composer.kAutoPlay\*1000, act\_autoPlay, 1 ) end -- Actions (functions) act\_completed = function (event) if (paired == totPairs) then composer.gotoScene(composer.getSceneName( "current" ),{recreate=true}) end end --End Actions (functions) -- Button event listeners local function oncover1Event(event) but\_cover1(layer.cover1) return true end layer.cover1:addEventListener("tap", oncover1Event ) local function oncover2Event(event) but\_cover2(layer.cover2) return true end layer.cover2:addEventListener("tap", oncover2Event ) local function oncover3Event(event) but\_cover3(layer.cover3) return true end layer.cover3:addEventListener("tap", oncover3Event ) local function oncover4Event(event) but\_cover4(layer.cover4) return true end layer.cover4:addEventListener("tap", oncover4Event ) local function oncover5Event(event) but\_cover5(layer.cover5) return true end layer.cover5:addEventListener("tap", oncover5Event ) local function oncover6Event(event) but\_cover6(layer.cover6) return true end layer.cover6:addEventListener("tap", oncover6Event ) local function oncover7Event(event) but\_cover7(layer.cover7) return true end layer.cover7:addEventListener("tap", oncover7Event ) local function oncover8Event(event) but\_cover8(layer.cover8) return true end layer.cover8:addEventListener("tap", oncover8Event ) -- Button functions function but\_cover1(self) --External code --External code print("flips: "..flips) -- verifies number of flips if (flips \< 2) then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) -- blocks tentatives to press the same card if flipped[1] ~= self then --check if the entry is the same as the first one flips = flips + 1; if flips \> 2 then flips = 2 end -- updates the number of flips table.insert(flipped,self) -- updates the content of flipped table end elseif flips == 2 then flips = flips + 1 --; if flips \> 2 then flips = 2 end -- two flips made -- blocks tentatives to press the same card if flipped[1] ~= self then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) table.insert(flipped,self) -- updates the content of flipped table if (randList[tonumber(string.sub(string.reverse(flipped[1].name),1,1))] == randList[tonumber(string.sub(string.reverse(flipped[2].name),1,1))]) then --equal images, move buttons to out of screen flipped[1].x = 5000 flipped[2].x = 5000 flips = 1 -- returns the number of flips to 1 flipped = {} --cleans the flipped table paired = paired + 1 act\_completed() else -- cards don't match local function hideAgain() composer.trans.newTransition\_898 = transition.to( flipped[1], {alpha=1, time=1000, delay=0}) composer.trans.newTransition\_888 = transition.to( flipped[2], {alpha=1, time=1000, delay=0}) local function zeroesFlip() flips = 1 flipped = {} print("flips:"..flips) end composer.timerStash.timer\_fl = timer.performWithDelay( 1000, zeroesFlip, 1 ) --timer to block the cards again, with the cover image end composer.timerStash.timer\_AP = timer.performWithDelay( 2000, hideAgain, 1 ) --timer to block the cards again, with the cover image end end end end function but\_cover2(self) --External code --External code print("flips: "..flips) -- verifies number of flips if (flips \< 2) then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) -- blocks tentatives to press the same card if flipped[1] ~= self then --check if the entry is the same as the first one flips = flips + 1; if flips \> 2 then flips = 2 end -- updates the number of flips table.insert(flipped,self) -- updates the content of flipped table end elseif flips == 2 then flips = flips + 1 --; if flips \> 2 then flips = 2 end -- two flips made -- blocks tentatives to press the same card if flipped[1] ~= self then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) table.insert(flipped,self) -- updates the content of flipped table if (randList[tonumber(string.sub(string.reverse(flipped[1].name),1,1))] == randList[tonumber(string.sub(string.reverse(flipped[2].name),1,1))]) then --equal images, move buttons to out of screen flipped[1].x = 5000 flipped[2].x = 5000 flips = 1 -- returns the number of flips to 1 flipped = {} --cleans the flipped table paired = paired + 1 act\_completed() else -- cards don't match local function hideAgain() composer.trans.newTransition\_898 = transition.to( flipped[1], {alpha=1, time=1000, delay=0}) composer.trans.newTransition\_888 = transition.to( flipped[2], {alpha=1, time=1000, delay=0}) local function zeroesFlip() flips = 1 flipped = {} print("flips:"..flips) end composer.timerStash.timer\_fl = timer.performWithDelay( 1000, zeroesFlip, 1 ) --timer to block the cards again, with the cover image end composer.timerStash.timer\_AP = timer.performWithDelay( 2000, hideAgain, 1 ) --timer to block the cards again, with the cover image end end end end function but\_cover3(self) --External code --External code print("flips: "..flips) -- verifies number of flips if (flips \< 2) then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) -- blocks tentatives to press the same card if flipped[1] ~= self then --check if the entry is the same as the first one flips = flips + 1; if flips \> 2 then flips = 2 end -- updates the number of flips table.insert(flipped,self) -- updates the content of flipped table end elseif flips == 2 then flips = flips + 1 --; if flips \> 2 then flips = 2 end -- two flips made -- blocks tentatives to press the same card if flipped[1] ~= self then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) table.insert(flipped,self) -- updates the content of flipped table if (randList[tonumber(string.sub(string.reverse(flipped[1].name),1,1))] == randList[tonumber(string.sub(string.reverse(flipped[2].name),1,1))]) then --equal images, move buttons to out of screen flipped[1].x = 5000 flipped[2].x = 5000 flips = 1 -- returns the number of flips to 1 flipped = {} --cleans the flipped table paired = paired + 1 act\_completed() else -- cards don't match local function hideAgain() composer.trans.newTransition\_898 = transition.to( flipped[1], {alpha=1, time=1000, delay=0}) composer.trans.newTransition\_888 = transition.to( flipped[2], {alpha=1, time=1000, delay=0}) local function zeroesFlip() flips = 1 flipped = {} print("flips:"..flips) end composer.timerStash.timer\_fl = timer.performWithDelay( 1000, zeroesFlip, 1 ) --timer to block the cards again, with the cover image end composer.timerStash.timer\_AP = timer.performWithDelay( 2000, hideAgain, 1 ) --timer to block the cards again, with the cover image end end end end function but\_cover4(self) --External code --External code print("flips: "..flips) -- verifies number of flips if (flips \< 2) then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) -- blocks tentatives to press the same card if flipped[1] ~= self then --check if the entry is the same as the first one flips = flips + 1; if flips \> 2 then flips = 2 end -- updates the number of flips table.insert(flipped,self) -- updates the content of flipped table end elseif flips == 2 then flips = flips + 1 --; if flips \> 2 then flips = 2 end -- two flips made -- blocks tentatives to press the same card if flipped[1] ~= self then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) table.insert(flipped,self) -- updates the content of flipped table if (randList[tonumber(string.sub(string.reverse(flipped[1].name),1,1))] == randList[tonumber(string.sub(string.reverse(flipped[2].name),1,1))]) then --equal images, move buttons to out of screen flipped[1].x = 5000 flipped[2].x = 5000 flips = 1 -- returns the number of flips to 1 flipped = {} --cleans the flipped table paired = paired + 1 act\_completed() else -- cards don't match local function hideAgain() composer.trans.newTransition\_898 = transition.to( flipped[1], {alpha=1, time=1000, delay=0}) composer.trans.newTransition\_888 = transition.to( flipped[2], {alpha=1, time=1000, delay=0}) local function zeroesFlip() flips = 1 flipped = {} print("flips:"..flips) end composer.timerStash.timer\_fl = timer.performWithDelay( 1000, zeroesFlip, 1 ) --timer to block the cards again, with the cover image end composer.timerStash.timer\_AP = timer.performWithDelay( 2000, hideAgain, 1 ) --timer to block the cards again, with the cover image end end end end function but\_cover5(self) --External code --External code print("flips: "..flips) -- verifies number of flips if (flips \< 2) then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) -- blocks tentatives to press the same card if flipped[1] ~= self then --check if the entry is the same as the first one flips = flips + 1; if flips \> 2 then flips = 2 end -- updates the number of flips table.insert(flipped,self) -- updates the content of flipped table end elseif flips == 2 then flips = flips + 1 --; if flips \> 2 then flips = 2 end -- two flips made -- blocks tentatives to press the same card if flipped[1] ~= self then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) table.insert(flipped,self) -- updates the content of flipped table if (randList[tonumber(string.sub(string.reverse(flipped[1].name),1,1))] == randList[tonumber(string.sub(string.reverse(flipped[2].name),1,1))]) then --equal images, move buttons to out of screen flipped[1].x = 5000 flipped[2].x = 5000 flips = 1 -- returns the number of flips to 1 flipped = {} --cleans the flipped table paired = paired + 1 act\_completed() else -- cards don't match local function hideAgain() composer.trans.newTransition\_898 = transition.to( flipped[1], {alpha=1, time=1000, delay=0}) composer.trans.newTransition\_888 = transition.to( flipped[2], {alpha=1, time=1000, delay=0}) local function zeroesFlip() flips = 1 flipped = {} print("flips:"..flips) end composer.timerStash.timer\_fl = timer.performWithDelay( 1000, zeroesFlip, 1 ) --timer to block the cards again, with the cover image end composer.timerStash.timer\_AP = timer.performWithDelay( 2000, hideAgain, 1 ) --timer to block the cards again, with the cover image end end end end function but\_cover6(self) --External code --External code print("flips: "..flips) -- verifies number of flips if (flips \< 2) then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) -- blocks tentatives to press the same card if flipped[1] ~= self then --check if the entry is the same as the first one flips = flips + 1; if flips \> 2 then flips = 2 end -- updates the number of flips table.insert(flipped,self) -- updates the content of flipped table end elseif flips == 2 then flips = flips + 1 --; if flips \> 2 then flips = 2 end -- two flips made -- blocks tentatives to press the same card if flipped[1] ~= self then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) table.insert(flipped,self) -- updates the content of flipped table if (randList[tonumber(string.sub(string.reverse(flipped[1].name),1,1))] == randList[tonumber(string.sub(string.reverse(flipped[2].name),1,1))]) then --equal images, move buttons to out of screen flipped[1].x = 5000 flipped[2].x = 5000 flips = 1 -- returns the number of flips to 1 flipped = {} --cleans the flipped table paired = paired + 1 act\_completed() else -- cards don't match local function hideAgain() composer.trans.newTransition\_898 = transition.to( flipped[1], {alpha=1, time=1000, delay=0}) composer.trans.newTransition\_888 = transition.to( flipped[2], {alpha=1, time=1000, delay=0}) local function zeroesFlip() flips = 1 flipped = {} print("flips:"..flips) end composer.timerStash.timer\_fl = timer.performWithDelay( 1000, zeroesFlip, 1 ) --timer to block the cards again, with the cover image end composer.timerStash.timer\_AP = timer.performWithDelay( 2000, hideAgain, 1 ) --timer to block the cards again, with the cover image end end end end function but\_cover7(self) --External code --External code print("flips: "..flips) -- verifies number of flips if (flips \< 2) then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) -- blocks tentatives to press the same card if flipped[1] ~= self then --check if the entry is the same as the first one flips = flips + 1; if flips \> 2 then flips = 2 end -- updates the number of flips table.insert(flipped,self) -- updates the content of flipped table end elseif flips == 2 then flips = flips + 1 --; if flips \> 2 then flips = 2 end -- two flips made -- blocks tentatives to press the same card if flipped[1] ~= self then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) table.insert(flipped,self) -- updates the content of flipped table if (randList[tonumber(string.sub(string.reverse(flipped[1].name),1,1))] == randList[tonumber(string.sub(string.reverse(flipped[2].name),1,1))]) then --equal images, move buttons to out of screen flipped[1].x = 5000 flipped[2].x = 5000 flips = 1 -- returns the number of flips to 1 flipped = {} --cleans the flipped table paired = paired + 1 act\_completed() else -- cards don't match local function hideAgain() composer.trans.newTransition\_898 = transition.to( flipped[1], {alpha=1, time=1000, delay=0}) composer.trans.newTransition\_888 = transition.to( flipped[2], {alpha=1, time=1000, delay=0}) local function zeroesFlip() flips = 1 flipped = {} print("flips:"..flips) end composer.timerStash.timer\_fl = timer.performWithDelay( 1000, zeroesFlip, 1 ) --timer to block the cards again, with the cover image end composer.timerStash.timer\_AP = timer.performWithDelay( 2000, hideAgain, 1 ) --timer to block the cards again, with the cover image end end end end function but\_cover8(self) --External code --External code print("flips: "..flips) -- verifies number of flips if (flips \< 2) then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) -- blocks tentatives to press the same card if flipped[1] ~= self then --check if the entry is the same as the first one flips = flips + 1; if flips \> 2 then flips = 2 end -- updates the number of flips table.insert(flipped,self) -- updates the content of flipped table end elseif flips == 2 then flips = flips + 1 --; if flips \> 2 then flips = 2 end -- two flips made -- blocks tentatives to press the same card if flipped[1] ~= self then composer.trans.newTransition\_743 = transition.to( self, {alpha=0, time=1000, delay=0}) table.insert(flipped,self) -- updates the content of flipped table if (randList[tonumber(string.sub(string.reverse(flipped[1].name),1,1))] == randList[tonumber(string.sub(string.reverse(flipped[2].name),1,1))]) then --equal images, move buttons to out of screen flipped[1].x = 5000 flipped[2].x = 5000 flips = 1 -- returns the number of flips to 1 flipped = {} --cleans the flipped table paired = paired + 1 act\_completed() else -- cards don't match local function hideAgain() composer.trans.newTransition\_898 = transition.to( flipped[1], {alpha=1, time=1000, delay=0}) composer.trans.newTransition\_888 = transition.to( flipped[2], {alpha=1, time=1000, delay=0}) local function zeroesFlip() flips = 1 flipped = {} print("flips:"..flips) end composer.timerStash.timer\_fl = timer.performWithDelay( 1000, zeroesFlip, 1 ) --timer to block the cards again, with the cover image end composer.timerStash.timer\_AP = timer.performWithDelay( 2000, hideAgain, 1 ) --timer to block the cards again, with the cover image end end end end -- swipe this page with spacer of 120 in normal direction composer.Gesture.activate( layer.Layer\_1, {swipeLength=120} ) composer.pageSwap = function (event ) local options if event.phase == "ended" and event.direction ~= nil then local wPage = curPage if event.direction == "left" and composer.kBidi == false then wPage = curPage + 1 if wPage \> numPages then wPage = curPage end options = { effect = "fromRight"} elseif event.direction == "left" and composer.kBidi == true then wPage = curPage - 1 if wPage \< 1 then wPage = 1 end options = { effect = "fromLeft"} elseif event.direction == "right" and composer.kBidi == true then wPage = curPage + 1 if wPage \> numPages then wPage = curPage end options = { effect = "fromRight"} elseif event.direction == "right" and composer.kBidi == false then wPage = curPage - 1 if wPage \< 1 then wPage = 1 end options = { effect = "fromLeft"} end if tonumber(wPage) ~= tonumber(curPage) then composer.gotoScene("page\_"..wPage, options) end end end layer.Layer\_1:addEventListener( composer.Gesture.SWIPE\_EVENT, composer.pageSwap ) composer.test = composer.pageSwap -- (BOTTOM) External code will render here end --ends phase did end function scene:hide( event ) local phase = event.phase if "will" == phase then print( "1: hide event, phase will" ) -- remove touch listener for image composer.cancelAllTimers(); layer.Layer\_1:removeEventListener( composer.Gesture.SWIPE\_EVENT, composer.pageSwap ); homeButton:removeEventListener( "touch", homeButton ) elseif event.phase == "did" then composer.test = nil end end function scene:destroy( event ) print( "((destroying scene 1's view))" ) end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene
Beside that, I also using another sample code from online sources but the object stay on the screen after i change screen. Can anyone help me have a look on it . Thank you. Below is the code.
--------------------------------------------------------------------------------- -- -- scene1.lua -- --------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- \_W = display.contentWidth; \_H = display.contentHeight; -- Touch event listener for background image --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} --Declare and prime a last button selected variable local lastButton = display.newImage("2.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 local function homeButtonTouch( self, event ) if event.phase == "began" then audio.pause(bsound) audio.pause(bola) composer.gotoScene( "menu", "fade", 400 ) return true end end 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(1250, function() matchText.text = " "; checkForMatch = false; secondSelect = 0; buttonCover[lastButton.number].isVisible = true; buttonCover[object.number].isVisible = true; end, 1) --If buttons DO match, remove buttons elseif(lastButton.myName == object.myName) then matchText.text = "Match Found!"; timer.performWithDelay(1250, 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 -- Called when the scene's view does not exist: function scene:create( event ) local sceneGroup = self.view image = display.newImage( "background/UI\_sebutanABC.png" ) image.x = 515 image.y = 385 sceneGroup:insert( image ) homeButton = display.newImage( "button/home.png" ) homeButton.x = 960 homeButton.y = 50 sceneGroup:insert( homeButton ) homeButton.touch = homeButtonTouch for count = 1,2 do x = x + 250 y = 160 for insideCount = 1,2 do y = y + 160 --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 print( "\n1: create event") end function scene:show( event ) local phase = event.phase if "did" == phase then print( "1: show event, phase did" ) -- remove previous scene's view -- Update Lua memory text display homeButton:addEventListener( "touch", homeButton ) end end function scene:hide( event ) local phase = event.phase if "will" == phase then print( "1: hide event, phase will" ) -- remove touch listener for image homeButton:removeEventListener( "touch", homeButton ) end end function scene:destroy( event ) print( "((destroying scene 1's view))" ) end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene