Just remade your boxes creation code so you can see that your approach is far from optimized.
There are infinite ways of doing it, I just made one so you can learn from:
My main file is:
display.setDefault( "anchorX", 0 ) display.setDefault( "anchorY", 0 ) local boxes=require ("boxes") local \_H=display.contentHeight local groundHeight=20 local boxClass=boxes.Box() -- creates the ground class local boxGroundInstance=boxClass.new({x=0, y=display.contentHeight-40, height=20, width=display.contentWidth, bodyType="static", body={friction=1.0}, name="ground", isSensor=false}) -- creates the ground object -- create the boxes that are used to check position local boxInstance1=boxClass.new({x=50,y=\_H-40-69, name="four", color={0.7,0.7, 0.7,1}, width=69, height=69, radius=3, bodyType="static", isSensor=true}) local boxInstance2=boxClass.new({x=200,y=\_H-40-69, name="seven", color={0.7,0.7, 0.7,1}, width=69, height=69, radius=3, bodyType="static", isSensor=true}) -- create the boxes that fall from the sky local boxInstance3=boxClass.new({x=50,y=300, name="four", color={0,0, 1,1}, width=67, height=67, radius=3, isSensor=false, colision=true, body={ density=2, friction=0.5, bounce=0.2}}) local boxInstance4=boxClass.new({x=200,y=50, name="seven", color={0,0, 1,1}, width=67, height=67, radius=3, isSensor=false, colision=true, body={ density=2, friction=0.5, bounce=0.2}})
My boxes.lua file:
local physics = require("physics") physics.start() physics.setGravity(0,9.8) local boxes={} function boxes.Box() local height=20 local \_M = { x=0, y=display.contentHeight-height, width=display.contentWidth, height=height, radius=0, name="", color={0.89, 0.498, 0.263, 1}, body=nil, anchorX=0, anchorY=0, bodyType="dynamic", isSensor=false, colision=false, } local mt = { \_\_index = \_M } function \_M.delete(self) display.remove(self.view) self.view=nil self=nil end local function onLocalCollision( self, event ) if ( event.phase == "began" ) then -- do whatever you want here end end local instance = {} function instance.new(params) params=params or {} setmetatable(params, mt) params.view=display.newRoundedRect( params.x, params.y, params.width, params.height, params.radius) params.view.name=params.name params.view:setFillColor(unpack(params.color)) params.view.anchorX=params.anchorX params.view.anchorY=params.anchorY physics.addBody(params.view, params.body ) params.view.isSensor=params.isSensor params.view.bodyType=params.bodyType if params.colision==true then params.view.collision = onLocalCollision params.view:addEventListener( "collision", params.view ) end return params end return instance end return boxes
my build.settings
-- -- For more information on build.settings, see the Project Build Settings guide at: -- https://docs.coronalabs.com/guide/distribution/buildSettings -- settings = { orientation = { -- Supported values for orientation: -- portrait, portraitUpsideDown, landscapeLeft, landscapeRight default = "portrait", supported = { "portrait", "portraitUpsideDown"}, }, -- -- Android section -- android = { usesPermissions = { "android.permission.INTERNET", }, }, -- -- iOS section -- iphone = { xcassets = "Images.xcassets", plist = { UIStatusBarHidden = false, UILaunchStoryboardName = "LaunchScreen", }, }, -- -- Plugins section -- plugins = { }, -- -- Project section -- excludeFiles = { -- Exclude unnecessary files for each platform all = { "Icon.png", "Icon-\*dpi.png", "Images.xcassets", }, android = { "LaunchScreen.storyboardc", }, }, }
My config.lua
-- Hard code the following without using Corona APIs. application = { content = { scale = "adaptive", fps = 60, imageSuffix = { ["@2x"] = 1.5, ["@3x"] = 2.5, } }, notification = { iphone = { types = { "badge", "sound", "alert" } }, google = { projectNumber = "" }, } }