memory/code optimization help

I’m new to corona and game developing in general. I’m trying to make a drawing game. The idea is that when you touch the screen, a line that simulates a crayon is drawn.
In order to achieve this, i display a newRect in every touch position and then apply a png mask to get a crayon like texture. Problem is that after drawing some lines, i believe i use all my memory up and the game starts lagging…
Can anyone help me with this?
I’m posting the code. I know it’s far from being the best one but it is what i have achieved so far.

main.lua
[blockcode]

H = display.contentHeight
W = display.contentWidth
local background = display.newRect(0, 0, W, H)
local mask = graphics.newMask(“brush2.png”)
local mask_pincel = graphics.newMask(“mask_pincel.png”)
– local bk_mask = graphics.newMask(“backgroundmask.png”)
– background:setMask(bk_mask)
– background.isHitTestMasked = true
local r = math.random (0, 255)
local g = math.random ( 0, 255)
local b = math.random (0, 255)
local escala = 1
local delta = math.random (0, 360)
local cont = 0
local radio = 25
local prevx = 0
local prevy = 0
local x1 = nil
local x2 = nil
local y1 = nil
local y2 = nil
local x = nil
local y = 0
local list = {}

–dibuja menu
menu = display.newRect(0, 924, W, H)
menu:setFillColor(140, 140, 140)
–dibuja botones
botoncolor = display.newRect(162,944, 60, 60)
mbc=graphics.newMask(“botoncolor.png”)
botoncolor:setFillColor(r, g, b)
botoncolor:setMask(mbc)
botonwidth = display.newImageRect(“botonwidth.png”, 60, 60)
botonwidth.x = 354
botonwidth.y = 970
botontrazo = display.newImageRect(“botontrazo.png”, 60, 60)
botontrazo.x = 546
botontrazo.y = 970


–FUNCIONES: BOTONES–

function botoncolor:tap(event)
r = math.random (0, 255)
g = math.random ( 0, 255)
b = math.random (0, 255 )
botoncolor:setFillColor(r, g, b)
end

function botonwidth:tap(event)
if (cont == 4) then
cont = 0
escala = 0.5
else
cont = cont + 1
escala = escala + 1
end
end


–DIBUJAR–

–CRAYOLA–

function background:touch(event)
if event.phase == “began” then
prevx=event.x
prevy=event.y
end
if event.phase == “moved” then
x1=prevx
x=prevx
y1=prevy
x2 = event.x
y2 = event.y
if prevx<event.x then> print (“A(”,x1,",",x2,");B(",y1,",",y2,")")
for i=x1,x2 do
if x1 == x2 then break end
y = math.ceil(((x-x1)/(x2-x1))(y2-y1)+y1)
print (y)
local brush = display.newRect(0, 0, 50, 50)
brush.isVisible = false
brush:setFillColor(r,g,b)
brush.x = x
brush.y = y
if event.y < 924-25
escala then
brush.isVisible = true
end
brush:setMask(mask)
brush:scale(escala, escala)
brush.maskRotation = delta
brush.alpha = 0.05
delta = math.random (0, 360)
x=x+1
end
elseif prevx>event.x then
print (“A(”,x1,",",x2,");B(",y1,",",y2,")")
for i=x2,x1 do
if x1 == x2 then break end
y = math.ceil(((x-x1)/(x2-x1))(y2-y1)+y1)
print (y)
local brush = display.newRect(0, 0, 50, 50)
brush.isVisible = false
brush:setFillColor(r,g,b)
brush.x = x
brush.y = y
if event.y < 924-25
escala then
brush.isVisible = true
end
brush:setMask(mask)
brush:scale(escala, escala)
brush.maskRotation = delta
brush.alpha = 0.05
delta = math.random (0, 360)
x=x-1
end
elseif prevx==event.x then
y=prevy
if prevy<event.y then> for i=prevy,event.y do
local brush = display.newRect(0, 0, 50, 50)
brush.isVisible = false
brush:setFillColor(r,g,b)
brush.x = prevx
brush.y = y
if event.y < 924-25escala then
brush.isVisible = true
end
brush:setMask(mask)
brush:scale(escala, escala)
brush.maskRotation = delta
brush.alpha = 0.1
delta = math.random (0, 360)
y=y+1
end
elseif prevy>event.y then
for i=event.y, prevy do
local brush = display.newRect(0, 0, 50, 50)
brush.isVisible = false
brush:setFillColor(r,g,b)
brush.x = prevx
brush.y = prevy
if event.y < 924-25
escala then
brush.isVisible = true
end
brush:setMask(mask)
brush:scale(escala, escala)
brush.maskRotation = delta
brush.alpha = 0.1
delta = math.random (0, 360)
y=y-1
end
end
end
prevx=event.x
prevy=event.y
end
end
----------------------------------------------------
–LISTENERS–
----------------------------------------------------
background:addEventListener(“touch”, background)
botoncolor:addEventListener(“tap”, botoncolor)
botonwidth:addEventListener(“tap”, botonwidth)

[/blockcode]

config.lua
[blockcode]
application =
{
content =
{
fps = 60,
width = 768,
height = 1024,
scale = “zoomEven”,
},
}
[/blockcode] [import]uid: 98755 topic_id: 16853 reply_id: 316853[/import] </event.y></event.x>

I guess you will have to change from a point to a stroke, which means that instead of drawing every point and mask it, you will have to capture the points and when the touch ends, you will have to convert them all into a stroke that will be one element than a series of smaller elements taking up memory

I hope this helps you,

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16853 reply_id: 63113[/import]

Thanks for the reply.
I thought it would have to be that way.
How can i do that? Convert a list of points into a stroke? [import]uid: 98755 topic_id: 16853 reply_id: 63116[/import]

@fitogrowler1, search the Code Exchange, you will fins a couple of sample that talk about fitting points on a curve and the kind.

most of the code is plug and play code, you do not have to think much about how it works, it just works. so rather than duplicating the code, I am directing you to the Code Exchange.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16853 reply_id: 63540[/import]