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
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | 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-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 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) |
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,
?:)
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?