This code works fine and reports 0 texture memory until I add a Default.png to the
folder. It seems to be linked some how to the event. Is there a way to clear the
Default.png from texture memory after the game has loaded or am I doing something
else wrong.
I used the fish images. Code is not Beautified ;)
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 | local map = {} for x=1,8 do map[x] = {} for y=1,10 do map[x][y]={} end end local restart=0 local function gameEventLoop(event) if restart==1 then loadMap() restart=0 end end function onTouch( event) print("touch restart") local t = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true elseif t.isFocus then if "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false --loadMap() restart=1 end end return true end function destroyMap() for x=1,8 do for y=1,10 do if (map[x][y].image~= "blank") then if (map[x][y].button~=nil) then map[x][y].button:removeEventListener( "touch", onTouch ) map[x][y].button:removeSelf() map[x][y].button=nil end if (map[x][y].shadow~=nil) then map[x][y].shadow:removeSelf() map[x][y].shadow=nil end end end end if (map.imagegroup~=nil) then print("imagegroup "..map.imagegroup.numChildren) for i=map.imagegroup.numChildren,1,-1 do local child = map.imagegroup[i] child.parent:remove( child ) end end map.imagegroup=nil collectgarbage("collect") print(collectgarbage("count")) print( "TextureMemory clear: " .. system.getInfo("textureMemoryUsed") .. " bytes" ) end function loadMap() destroyMap() map.imagegroup =display.newGroup() for x=1,8 do for y=1,10 do map[x][y].button = display.newImageRect("Image1.png",40,40) map[x][y].shadow = display.newImageRect("Image2.png",40,40) map[x][y].shadow.alpha=0.5 map.imagegroup:insert(map[x][y].shadow) map.imagegroup:insert(map[x][y].button) map[x][y].button.x=(x)*39+5 map[x][y].button.y=(y)*39+46 map[x][y].shadow.x=(x)*39+11 map[x][y].shadow.y=(y)*39+12 map[x][y].button:addEventListener( "touch", onTouch ) end end collectgarbage("collect") print(collectgarbage("count")) print( "TextureMemory finished: " .. system.getInfo("textureMemoryUsed") .. " bytes" ) end loadMap() loadMap() loadMap() Runtime:addEventListener( "enterFrame", gameEventLoop ) |
Hmm, I don't know but I filed a bug for engineering to take a look (case 1802). If I learn anything new I'll post back. Thanks for the report.
Tim
Thanks Tim,
Cleaned the code a bit
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 | --make sure a Default.png exists gameState=0 function gameEventLoop(event) if gameState==1 then print("fourth run") loadMap() gameState=0 end end function onTouch( event) print("touch restart") local t = event.target local phase = event.phase if "began" == phase then local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true elseif t.isFocus then if "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false gameState=1 print("third run") loadMap() end end return true end function destroyMap() if (button~=nil) then button:removeEventListener( "touch", onTouch ) button:removeSelf() button=nil end if (imagegroup~=nil) then print("imagegroup "..imagegroup.numChildren) for i=imagegroup.numChildren,1,-1 do local child = imagegroup[i] child.parent:remove( child ) end end imagegroup=nil collectgarbage("collect") print("collect garbage "..collectgarbage("count")) print( "TextureMemory clear: " .. system.getInfo("textureMemoryUsed") .. " bytes" ) end function loadMap() destroyMap() imagegroup =display.newGroup() button = display.newImageRect("Image1.png",40,40) imagegroup:insert(button) button.x=50 button.y=50 button:addEventListener( "touch", onTouch ) collectgarbage("collect") print("collect garbage "..collectgarbage("count")) print( "TextureMemory finished: " .. system.getInfo("textureMemoryUsed") .. " bytes" ) end Runtime:addEventListener( "enterFrame", gameEventLoop ) print("first run") loadMap() print("second run") loadMap() print("click the fish") |
hey i want my game to restart when i press a button. I have added a button but don't know how to restart the whole game. Is their any function to for restart?
views:1582 update:2011/9/29 19:21:19