I’m a newbie, and learning stuff by practicing code. I’ve made a simple app (too simple ;experimental), and I feel like I’ve been repeating codes again and again. Can someone help me simplify the code, so that I can learn from the examples… :) My app works like this: When you click/touch the background, a physical object circle is created and falls due to gravity. The display has borders(rectangles to prevent the balls from falling off screen) and a few rectangles in the middle just for fun :P . After 10 seconds, the balls start to remove from display(but not sure if it is freed from memory). Balls have different colors when clicked/touched at different fractions of the width. Also help me to add a new color to the last fraction of the width. When I attempt it, the color doesn’t appear. I want to see four colors but instead I am getting three. Thank you for anyone who put in effort to help me learn.
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- display.setStatusBar(display.HiddenStatusBar) local physics = require("physics") physics.start() physics.setGravity(0,50) local background = display.newImageRect( "background.png", 800, 600 ) background.x = display.contentCenterX background.y = display.contentCenterY local function gameLoop(event) if ( event.x \< display.contentCenterX / 2) then local shape = display.newCircle( event.x, event.y, 5 ) physics.addBody( shape, "dynamic", {density=1, radius=6}) shape:setFillColor(0.2, 0.7, 1, 0.5) local function removeCircle() display.remove(shape) end timer.performWithDelay( 10000, removeCircle) elseif ( event.x \< display.contentCenterX ) then local shape = display.newCircle( event.x, event.y, 5 ) physics.addBody( shape, "dynamic", {density=1, radius=6}) shape:setFillColor(1, 0, 0, 0.5) local function removeCircle() display.remove(shape) end timer.performWithDelay( 10000, removeCircle) elseif ( event.x \< display.contentCenterX \* 2) then local shape = display.newCircle( event.x, event.y, 5 ) physics.addBody( shape, "dynamic", {density=1, radius=6}) shape:setFillColor(0.349, 0.156, 0.423, 0.5) local function removeCircle() display.remove(shape) end timer.performWithDelay( 10000, removeCircle) end end background:addEventListener("touch", gameLoop) --[[timer.performWithDelay(10, gameLoop, -1) --]] local rectangle1 = display.newRect( display.contentCenterX / 4, display.contentCenterY / 4, display.contentWidth, 10 ) rectangle1.rotation = 20 local rectangle2 = display.newRect( display.contentWidth / 1.3, display.contentCenterY/ 1.3, display.contentWidth, 10 ) rectangle2.rotation = -20 local rectangle3 = display.newRect( display.contentCenterX / 4, display.contentCenterY / 0.7, display.contentWidth, 10 ) rectangle3.rotation = 20 local rectangle4 = display.newRect( display.contentWidth / 1.3, display.contentCenterY / 0.5, display.contentWidth, 10 ) rectangle4.rotation = -20 local rectangle5 = display.newRect( display.contentCenterX, display.contentHeight + 40, display.contentWidth, 10 ) rectangle5.rotation = 0 local rectangle6 = display.newRect( display.contentWidth - 5, display.contentCenterY, display.contentHeight + 70, 10 ) rectangle6.rotation = 90 local rectangle7 = display.newRect( 5, display.contentCenterY, display.contentHeight + 70, 10 ) rectangle7.rotation = 90 local rectangle8 = display.newRect( display.contentCenterX, -40, display.contentWidth, 10 ) rectangle8.rotation = 0 physics.addBody( rectangle1, "static") physics.addBody( rectangle2, "static") physics.addBody( rectangle3, "static") physics.addBody( rectangle4, "static") physics.addBody( rectangle5, "static") physics.addBody( rectangle6, "static") physics.addBody( rectangle7, "static")