HELP! Weird Memory Leak..

Hi All,   Code pasted below, i have a restartlevel.lua which purges and removes the scene this code is in. Im pretty sure the leak is in the draw_line function.

Essentially, i draw the line on touch and a bunch of tables get populated with the line objects. When the scene gets restarted its about 30kb + bigger than the time before. 

As you can i have implemented some ridiculous loops to iterate through the tables and empty them

This is not the final code, but the issue is the same, touch the screen to draw the line, hit the play button then touch the reset button, the reste button kills all of the tables (lines), and restarts via the restartlevel.lua however as ive said, something is getting stuck in memory, any help would be AMAZING! Ive been a week on this leak and cannot find it, also, gone a bit mental :slight_smile:

You will see, im do a remove on loads of bits i probably dont need to, but ive put up as is, so you can see all the things im trying!

Code:

[lua]


– level1.lua


local storyboard = require( “storyboard” )

–storyboard.purgeOnSceneChange = true;

local scene = storyboard.newScene()

system.setAccelerometerInterval( 100 ) – set accelerometer to maximum responsiveness

– include Corona’s “physics” library

local physics = require (“physics”)

– include Corona’s “widget” library

local widget = require “widget”

–set gravity variable for start up

physics.start();

physics.pause();

physics.setGravity(0, 9.6)

–physics.setDrawMode( “hybrid” );


– Memory usage function ------------------------- ADDED


local memoryText

local monitorMem = function()

    collectgarbage()

    print( "MemUsage: " … collectgarbage(“count”) )

    

    local deviceMem = collectgarbage(“count”)

    local textMem = system.getInfo( “textureMemoryUsed” ) / 1000000

    print( "TexMem:   " … textMem )

    

    memoryText.text = deviceMem

    

end

local linesA = {};

local linesB = {};

local linesC = {};

local linesD = {};

local linesE = {};

local linesF = {};

local linesG = {};

local function cancelAllLinesA()

    for key, item in pairs(linesA) do

        --print (“found a line”);

        --item:removeEventListener( “collision”, plasmaHit )

          display.remove(item);

          item = nil; key = nil;

          collectgarbage(“collect”);

    end

    

    print("HELLO - IM : " … #linesA);

    --lines = {};

    --lines = nil;

    collectgarbage();  

    return true;

end

local function nilTableLinesA()

    for i = #linesA, 1, -1 do

        --display.remove(linesA[i]);

        linesA[i] = nil;

        collectgarbage(“collect”);

    end

    

    print("HELLO - IM : " … #linesA);

    linesA = nil;

    collectgarbage(“collect”);

    return true;

end    

–remove the object lin table and all occurences of it…

local function cancelAllLinesB()

    for key, item in pairs(linesB) do

        --print (“found a line”);

          display.remove(item);

          item = nil; key = nil;

          collectgarbage();

    end

    

    print("HELLO - IM : " … #linesB);

    --lines2 = {};

    --lines2 = nil;

    collectgarbage(“collect”); 

    return true; 

end

local function nilTableLinesB()

    for i = #linesB, 1, -1 do

        --display.remove(linesB[i]);

        linesB[i] = nil;

        collectgarbage(“collect”);

    end

    

    

    print("HELLO - IM : " … #linesB);

    linesB = nil;

    collectgarbage(“collect”);

    return true;

end    

–remove the object lin table and all occurences of it…

local function cancelAllLinesC()

    for key, item in pairs(linesC) do

        --print (“found a line”);

          display.remove(item);

          item = nil; key = nil;

          collectgarbage();

    end

    --linesC = {};

    --linesC = nil;

    print("HELLO - IM : " … #linesC);

    collectgarbage(“collect”); 

    return true;

end

local function nilTableLinesC()

    for i = #linesC, 1, -1 do

        --display.remove(linesC[i]);

        linesC[i] = nil;

        collectgarbage(“collect”);

    end

    

    

    print("HELLO - IM : " … #linesC);

    linesC = nil;

    collectgarbage(“collect”);

    return true;

end    

–remove the object lin table and all occurences of it…

local function cancelAllLinesD()

    for key, item in pairs(linesD) do

        --print (“found a line”);

          display.remove(item);

          item = nil; key = nil;

          collectgarbage(“collect”);

    end

    --linesD = {};

    --linesD = nil;

    print("HELLO - IM : " … #linesD);

    collectgarbage(“collect”);  

    return true;

end

local function nilTableLinesD()

    for i = #linesD, 1, -1 do

        --display.remove(linesD[i]);

        linesD[i] = nil;

        collectgarbage(“collect”);

    end

    

    

    print("HELLO - IM : " … #linesD);

    linesD = nil;

    collectgarbage(“collect”);

    return true;

end    

–remove the object lin table and all occurences of it…

local function cancelAllLinesE()

    for key, item in pairs(linesE) do

        --print (“found a line”);

          display.remove(item);

          item = nil; key = nil;

          collectgarbage(“collect”);

    end

    --linesE = {};

    --linesE = nil;

    print("HELLO - IM : " … #linesE);

    collectgarbage(“collect”);

    return true; 

end

local function nilTableLinesE()

    for i = #linesE, 1, -1 do

        --display.remove(linesE[i]);

        linesE[i] = nil;

        collectgarbage(“collect”);

    end

    

    

    print("HELLO - IM : " … #linesE);

    linesE = nil;

    collectgarbage(“collect”);

    return true;

end    

–remove the object lin table and all occurences of it…

local function cancelAllLinesF()

    for key, item in pairs(linesF) do

        --print (“found a line”);

          display.remove(item);

          item = nil; key = nil;

          collectgarbage(“collect”);

    end

    --linesF = {};

    --linesF = nil;

    print("HELLO - IM : " … #linesF);

    collectgarbage(“collect”);  

    return true;

end

local function nilTableLinesF()

    for i = #linesF, 1, -1 do

        --display.remove(linesF[i]);

        linesF[i] = nil;

        collectgarbage(“collect”);

    end

    

    

    print("HELLO - IM : " … #linesF);

    linesF = nil;

    collectgarbage(“collect”);

    return true;

end    

–remove the object lin table and all occurences of it…

local function cancelAllLinesG()

    for key, item in pairs(linesG) do

        --print (“found a line”);

          display.remove(item);

          item = nil; key = nil;

          collectgarbage(“collect”);

    end

    --linesG = {}

    --linesG = nil;

    print("HELLO - IM : " … #linesG);

    collectgarbage(“collect”); 

    return true;

end

local function nilTableLinesG()

    for i = #linesG, 1, -1 do

        --display.remove(linesG[i]);

        linesG[i] = nil;

        collectgarbage(“collect”);

    end

    

    

    print("HELLO - IM : " … #linesG);

    linesG = nil;

    collectgarbage(“collect”);

    return true;

end    




– forward declarations and other locals ------------------------- ADDED


local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

local LevelLayer = display.newGroup();

local drawMode = true;

local UsedInk = 0;

local MaxInk = 150;

local TotalInk = 150;

local playBtn;

local resetBtn;

local roundnum = math.round;

local randnum = math.random;

local sqrt = math.sqrt;

local currx;

local curry;

local prev_x;

local prev_y;

local contWidth = display.contentHeight;

local contHeight = display.contentHeight;

local line_number = 1;




–function that resets everything -------------------------------

function resetAll()

    – go to level1.lua scene

    

    audio.play(buttonclick,{loops=0})

    

    physics.pause();

    physicsOnVar = false;

    playing = false;

    

    if(cancelAllLinesA())then

        if(cancelAllLinesB())then

            if(cancelAllLinesC())then

                if(cancelAllLinesD())then

                    if(cancelAllLinesE())then

                        if(cancelAllLinesF())then

                            if(cancelAllLinesG())then

                            

                            

                                if(nilTableLinesA())then

                                    if(nilTableLinesB())then

                                        if(nilTableLinesC())then

                                            if(nilTableLinesD())then

                                                if(nilTableLinesE())then

                                                    if(nilTableLinesF())then

                                                        if(nilTableLinesG())then

                                                            storyboard.gotoScene( “restartLevel”, “crossFade”, 500 )

                                                        end

                                                    end

                                                end

                                            end

                                        end

                                    end

                                end

                            

                            

                            end

                        end

                    end

                end

            end

        end

    end

    

    collectgarbage(“collect”);

    

    return true    – indicates successful touch

    

end



–function that swaps the play button with the reset button------

local function setReset()

    – create a widget button (which starts the physics on start)

    

    resetBtn = widget.newButton{

        labelColor = { default={255}, over={128} },

        defaultFile=“images/resetbutton.png”,

        overFile=“images/resetbutton_over.png”,

        width=58, height=58,

        onRelease = resetAll    – event listener function

    }

    

    resetBtn:setReferencePoint( display.CenterReferencePoint )

    resetBtn.x = (display.contentWidth - (resetBtn.width/2)) - 10

    resetBtn.y = (display.contentHeight - (resetBtn.height/2)) - 8

    

    GUI:insert(resetBtn);

    resetBtn.isVisible = true;

end


–sets every thing in motion at the start of the game------------

local function start(event)

    physics.start();

    physicsOnVar = true;

    setReset();

end


–function that draws the line on the screen---------------------

local function draw_line(e)

        

    currx = e.x;

    curry = e.y;    

        

      if e.phase == “began” then

        prev_x = currx

        prev_y = curry

    

      elseif e.phase == “moved” then

    

      --first check that we are allowed to draw

      if(drawMode == true)then

    

      --next only draw above ground level

            if(curry < contHeight)and(curry > 0)then

            

                if(currx > 0)and(currx < contWidth)then

    

                    if(UsedInk < TotalInk)then

                        

                        linesA[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesA[line_number]:setColor(255, 255, 255, 255)

                        linesA[line_number].width = 1

                        linesA[line_number].name = “plasma”;

                        

                        linesB[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesB[line_number]:setColor(255, 117, 254, 253)

                        linesB[line_number].width = 3

                        

                        linesC[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesC[line_number]:setColor(255, 60, 255, 255)

                        linesC[line_number].width = 5

                        

                        linesD[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesD[line_number]:setColor(255, 20, 247, 255)

                        linesD[line_number].width = 7    

                        

                        linesE[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesE[line_number]:setColor(255, 20, 247, 160)

                        linesE[line_number].width = 9    

                        

                        linesF[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesF[line_number]:setColor(255, 20, 247, 90)

                        linesF[line_number].width = 11

                        

                        linesG[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesG[line_number]:setColor(255, 20, 247, 10)

                        linesG[line_number].width = 13;

                        

                        LevelLayer:insert(linesG[line_number]);

                        LevelLayer:insert(linesF[line_number]);

                        LevelLayer:insert(linesE[line_number]);

                        LevelLayer:insert(linesD[line_number]);

                        LevelLayer:insert(linesC[line_number]);

                        LevelLayer:insert(linesB[line_number]);

                        LevelLayer:insert(linesA[line_number]);

                        local dist_x = e.x - prev_x

                        local dist_y = e.y - prev_y

                        

                        --Add a physics body that’s a flat polygon that follows each segment of the line

                        physics.addBody(linesA[line_number], “static”, { density = 0.8, friction = 0.4, bounce = 0.1, shape = {0, 0, dist_x, dist_y, 0, 0} } )

                        

                        prev_x = currx;

                        prev_y = curry;

                        line_number = line_number + 1

                        UsedInk = UsedInk + 1;

                    end

                    

                end

                

            end

        end

        

      elseif e.phase == “ended” then

      end

    

    

end






– BEGINNING OF YOUR IMPLEMENTATION

– 

– NOTE: Code outside of listener functions (below) will only be executed once,

–         unless storyboard.removeScene() is called.

– 


– Called when the scene’s view does not exist:

function scene:createScene( event )

        

    local group = self.view;    

        

    group:insert(LevelLayer)    

    – create a widget button (which starts the physics on start)

    playBtn = widget.newButton{

        defaultFile=“images/playbutton.png”,

        overFile=“images/playbutton_over.png”,

        width=58, height=58,

        onRelease = start    – event listener function

    }

    

    

    playBtn:setReferencePoint( display.CenterReferencePoint )

    playBtn.x = (0 + (playBtn.width/2)) + 10

    playBtn.y = (display.contentHeight - (playBtn.height/2)) - 8

    

    group:insert(playBtn)

    if (storyboard.getPrevious() == “restartLevel”)then

        storyboard.purgeScene( “restartLevel” );

        storyboard.removeScene( “restartLevel” );        

    end    

    

    Runtime:addEventListener( “touch”, draw_line );

    

    --Call Memory Monitor function -------------------------

    Runtime:addEventListener( “enterFrame”, monitorMem )

    --------------------------------------------------------

    

    ------ MEMORY TEST OUTPUT ------

    memoryText = display.newText(“Memory Usage”, 0, 10, “Komika Axis”, 12)

    memoryText:setTextColor(255, 255, 255)

    memoryText:setReferencePoint(display.TopRightReferencePoint);

    memoryText.x = display.contentWidth * 0.5

    memoryText.y = 0

    group:insert( memoryText )

    ---------------------------------

end

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )

    local group = self.view

end

– Called when scene is about to move offscreen:

function scene:exitScene( event )

    local group = self.view

    

    physics.stop()

    

    Runtime:removeEventListener( “touch”, draw_line );

    Runtime:removeEventListener( “enterFrame”, monitorMem )

    

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:

function scene:destroyScene( event )

    local group = self.view

    

    display.remove(playBtn);

    playBtn = nil

    collectgarbage();

    

    display.remove(resetBtn)–:removeSelf()    – widgets must be manually removed

    resetBtn = nil

    collectgarbage();

        

    package.loaded[physics] = nil

    physics = nil

    collectgarbage();    

    display.remove(LevelLayer);

    LevelLayer = nil;

    collectgarbage(“collect”);

    

    display.remove(GUI);

    GUI = nil;

    collectgarbage(“collect”);

    

    display.remove(InstructionsLayer);

    InstructionsLayer = nil;

    collectgarbage(“collect”);

    collectgarbage(“collect”);

    

end


– END OF YOUR IMPLEMENTATION


– “createScene” event is dispatched if scene’s view does not exist

scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished

scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins

scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be

– automatically unloaded in low memory situations, or explicitly via a call to

– storyboard.purgeScene() or storyboard.removeScene().

scene:addEventListener( “destroyScene”, scene )


return scene

[/lua]

Hi… Any ideas? Anything? Im stumped!

I think try to isolate the issue some more yourself and post a smaller snippet. Your asking someone to analyze over 600 lines of your code…

Sorry,was trying to show, that I’ve tried all the usual stuff, like clearing the tables etc, the problem is definitely in the draw_line function, something created in there is not getting cleared.

HI again, 

A shorter snippet… in case it help, im surprised no one has any ideas… gulp! im totally out of ideas…

[lua]


– level1.lua


local storyboard = require( “storyboard” )

–storyboard.purgeOnSceneChange = true;

local scene = storyboard.newScene()

system.setAccelerometerInterval( 100 ) – set accelerometer to maximum responsiveness

– include Corona’s “physics” library

local physics = require (“physics”)

–set gravity variable for start up

physics.start();

physics.pause();

physics.setGravity(0, 9.6)

–physics.setDrawMode( “hybrid” );

local linesA = {};

local linesB = {};

local linesC = {};

local linesD = {};

local linesE = {};

local linesF = {};

local linesG = {};




– forward declarations and other locals ------------------------- ADDED


local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

local LevelLayer = display.newGroup();

local drawMode = true;

local UsedInk = 0;

local MaxInk = 150;

local TotalInk = 150;

local playBtn;

local resetBtn;

local roundnum = math.round;

local randnum = math.random;

local sqrt = math.sqrt;

local currx;

local curry;

local prev_x;

local prev_y;

local contWidth = display.contentHeight;

local contHeight = display.contentHeight;

local line_number = 1;




–function that draws the line on the screen---------------------

local function draw_line(e)

        

    currx = e.x;

    curry = e.y;    

        

      if e.phase == “began” then

        prev_x = currx

        prev_y = curry

    

      elseif e.phase == “moved” then

    

      --first check that we are allowed to draw

      if(drawMode == true)then

    

      --next only draw above ground level

            if(curry < contHeight)and(curry > 0)then

            

                if(currx > 0)and(currx < contWidth)then

    

                    if(UsedInk < TotalInk)then

                        

                        linesA[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesA[line_number]:setColor(255, 255, 255, 255)

                        linesA[line_number].width = 1

                        linesA[line_number].name = “plasma”;

                        

                        linesB[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesB[line_number]:setColor(255, 117, 254, 253)

                        linesB[line_number].width = 3

                        

                        linesC[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesC[line_number]:setColor(255, 60, 255, 255)

                        linesC[line_number].width = 5

                        

                        linesD[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesD[line_number]:setColor(255, 20, 247, 255)

                        linesD[line_number].width = 7    

                        

                        linesE[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesE[line_number]:setColor(255, 20, 247, 160)

                        linesE[line_number].width = 9    

                        

                        linesF[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesF[line_number]:setColor(255, 20, 247, 90)

                        linesF[line_number].width = 11

                        

                        linesG[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesG[line_number]:setColor(255, 20, 247, 10)

                        linesG[line_number].width = 13;

                        

                        LevelLayer:insert(linesG[line_number]);

                        LevelLayer:insert(linesF[line_number]);

                        LevelLayer:insert(linesE[line_number]);

                        LevelLayer:insert(linesD[line_number]);

                        LevelLayer:insert(linesC[line_number]);

                        LevelLayer:insert(linesB[line_number]);

                        LevelLayer:insert(linesA[line_number]);

                        local dist_x = e.x - prev_x

                        local dist_y = e.y - prev_y

                        

                        --Add a physics body that’s a flat polygon that follows each segment of the line

                        physics.addBody(linesA[line_number], “static”, { density = 0.8, friction = 0.4, bounce = 0.1, shape = {0, 0, dist_x, dist_y, 0, 0} } )

                        

                        prev_x = currx;

                        prev_y = curry;

                        line_number = line_number + 1

                        UsedInk = UsedInk + 1;

                    end

                    

                end

                

            end

        end

        

      elseif e.phase == “ended” then

      end

    

    

end



– BEGINNING OF YOUR IMPLEMENTATION

– 

– NOTE: Code outside of listener functions (below) will only be executed once,

–         unless storyboard.removeScene() is called.

– 


– Called when the scene’s view does not exist:

function scene:createScene( event )

        

    local group = self.view;    

        

    group:insert(LevelLayer)    

    

    Runtime:addEventListener( “touch”, draw_line );

    

end

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )

    local group = self.view

end

– Called when scene is about to move offscreen:

function scene:exitScene( event )

    local group = self.view

    

    physics.stop()

    

    Runtime:removeEventListener( “touch”, draw_line );

    

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:

function scene:destroyScene( event )

    local group = self.view

            

    package.loaded[physics] = nil

    physics = nil

    collectgarbage();    

    display.remove(LevelLayer);

    LevelLayer = nil;

    collectgarbage(“collect”);

end


– END OF YOUR IMPLEMENTATION


– “createScene” event is dispatched if scene’s view does not exist

scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished

scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins

scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be

– automatically unloaded in low memory situations, or explicitly via a call to

– storyboard.purgeScene() or storyboard.removeScene().

scene:addEventListener( “destroyScene”, scene )


return scene

[/lua]

Hi… Any ideas? Anything? Im stumped!

I think try to isolate the issue some more yourself and post a smaller snippet. Your asking someone to analyze over 600 lines of your code…

Sorry,was trying to show, that I’ve tried all the usual stuff, like clearing the tables etc, the problem is definitely in the draw_line function, something created in there is not getting cleared.

HI again, 

A shorter snippet… in case it help, im surprised no one has any ideas… gulp! im totally out of ideas…

[lua]


– level1.lua


local storyboard = require( “storyboard” )

–storyboard.purgeOnSceneChange = true;

local scene = storyboard.newScene()

system.setAccelerometerInterval( 100 ) – set accelerometer to maximum responsiveness

– include Corona’s “physics” library

local physics = require (“physics”)

–set gravity variable for start up

physics.start();

physics.pause();

physics.setGravity(0, 9.6)

–physics.setDrawMode( “hybrid” );

local linesA = {};

local linesB = {};

local linesC = {};

local linesD = {};

local linesE = {};

local linesF = {};

local linesG = {};




– forward declarations and other locals ------------------------- ADDED


local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

local LevelLayer = display.newGroup();

local drawMode = true;

local UsedInk = 0;

local MaxInk = 150;

local TotalInk = 150;

local playBtn;

local resetBtn;

local roundnum = math.round;

local randnum = math.random;

local sqrt = math.sqrt;

local currx;

local curry;

local prev_x;

local prev_y;

local contWidth = display.contentHeight;

local contHeight = display.contentHeight;

local line_number = 1;




–function that draws the line on the screen---------------------

local function draw_line(e)

        

    currx = e.x;

    curry = e.y;    

        

      if e.phase == “began” then

        prev_x = currx

        prev_y = curry

    

      elseif e.phase == “moved” then

    

      --first check that we are allowed to draw

      if(drawMode == true)then

    

      --next only draw above ground level

            if(curry < contHeight)and(curry > 0)then

            

                if(currx > 0)and(currx < contWidth)then

    

                    if(UsedInk < TotalInk)then

                        

                        linesA[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesA[line_number]:setColor(255, 255, 255, 255)

                        linesA[line_number].width = 1

                        linesA[line_number].name = “plasma”;

                        

                        linesB[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesB[line_number]:setColor(255, 117, 254, 253)

                        linesB[line_number].width = 3

                        

                        linesC[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesC[line_number]:setColor(255, 60, 255, 255)

                        linesC[line_number].width = 5

                        

                        linesD[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesD[line_number]:setColor(255, 20, 247, 255)

                        linesD[line_number].width = 7    

                        

                        linesE[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesE[line_number]:setColor(255, 20, 247, 160)

                        linesE[line_number].width = 9    

                        

                        linesF[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesF[line_number]:setColor(255, 20, 247, 90)

                        linesF[line_number].width = 11

                        

                        linesG[line_number] = display.newLine(prev_x, prev_y, currx, curry) 

                        linesG[line_number]:setColor(255, 20, 247, 10)

                        linesG[line_number].width = 13;

                        

                        LevelLayer:insert(linesG[line_number]);

                        LevelLayer:insert(linesF[line_number]);

                        LevelLayer:insert(linesE[line_number]);

                        LevelLayer:insert(linesD[line_number]);

                        LevelLayer:insert(linesC[line_number]);

                        LevelLayer:insert(linesB[line_number]);

                        LevelLayer:insert(linesA[line_number]);

                        local dist_x = e.x - prev_x

                        local dist_y = e.y - prev_y

                        

                        --Add a physics body that’s a flat polygon that follows each segment of the line

                        physics.addBody(linesA[line_number], “static”, { density = 0.8, friction = 0.4, bounce = 0.1, shape = {0, 0, dist_x, dist_y, 0, 0} } )

                        

                        prev_x = currx;

                        prev_y = curry;

                        line_number = line_number + 1

                        UsedInk = UsedInk + 1;

                    end

                    

                end

                

            end

        end

        

      elseif e.phase == “ended” then

      end

    

    

end



– BEGINNING OF YOUR IMPLEMENTATION

– 

– NOTE: Code outside of listener functions (below) will only be executed once,

–         unless storyboard.removeScene() is called.

– 


– Called when the scene’s view does not exist:

function scene:createScene( event )

        

    local group = self.view;    

        

    group:insert(LevelLayer)    

    

    Runtime:addEventListener( “touch”, draw_line );

    

end

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )

    local group = self.view

end

– Called when scene is about to move offscreen:

function scene:exitScene( event )

    local group = self.view

    

    physics.stop()

    

    Runtime:removeEventListener( “touch”, draw_line );

    

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:

function scene:destroyScene( event )

    local group = self.view

            

    package.loaded[physics] = nil

    physics = nil

    collectgarbage();    

    display.remove(LevelLayer);

    LevelLayer = nil;

    collectgarbage(“collect”);

end


– END OF YOUR IMPLEMENTATION


– “createScene” event is dispatched if scene’s view does not exist

scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished

scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins

scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be

– automatically unloaded in low memory situations, or explicitly via a call to

– storyboard.purgeScene() or storyboard.removeScene().

scene:addEventListener( “destroyScene”, scene )


return scene

[/lua]