I just began experimenting with Corona today (and Lua for that matter) and am running into an issue that I’m sure is likely something simple I’m overlooking or not aware of. Anyways, I’m playing around with the physics and made a simple game level that has a floor, a ball that can be flung in a similar fashion to how Angry Birds operates, and random rectangles that are just stacked up.
The goal is to knock all the rectangles off of the screen and end the level when that occurs. Currently, I have a variable that holds that there are 10 objects to remove from the screen and when a rectangle is knocked out of the screen sides, it should decrement the value. For whatever reason, it only decrements down to 3 and I’m having issues pinpointing why. Here is the contents of my main.lua file…hopefully it’s not too long and/or formatted incorrectly:
[lua]
– Load & Start Physics
local physics = require(“physics”)
physics.start()
– Draw a circle and give it physics
local circle = display.newCircle(display.contentCenterX - 100, display.contentCenterY, 20)
physics.addBody(circle, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.2, radius = 20})
– Draw a floor and give it physics
local floor = display.newRect(display.contentCenterX, display.contentCenterY + 150, display.contentWidth + 100, 4)
physics.addBody(floor, “static”, {density = 1.0, friction = 0.3, bounce = 0.2})
–draw box level objects
local rect1 = display.newRect(display.contentCenterX + 50, (display.contentCenterY - 0) + (20 * 1), 20, 20)
local rect2 = display.newRect(display.contentCenterX + 50, (display.contentCenterY - 0) + (20 * 2), 20, 20)
local rect3 = display.newRect(display.contentCenterX + 50, (display.contentCenterY - 0) + (20 * 3), 20, 20)
local rect4 = display.newRect(display.contentCenterX + 50, (display.contentCenterY - 0) + (20 * 4), 20, 20)
local rect5 = display.newRect(display.contentCenterX + 50, (display.contentCenterY - 0) + (20 * 5), 20, 20)
local rect6 = display.newRect(display.contentCenterX + 50, (display.contentCenterY - 0) + (20 * 6), 20, 20)
local rect7 = display.newRect(display.contentCenterX + 50, (display.contentCenterY - 0) + (20 * 7), 20, 20)
local rect8 = display.newRect(display.contentCenterX + 50, (display.contentCenterY - 0) + (20 * 8), 20, 20)
local rect9 = display.newRect(display.contentCenterX + 50, (display.contentCenterY - 0) + (20 * 9), 20, 20)
local rect10 = display.newRect(display.contentCenterX + 50, (display.contentCenterY - 0) + (20 * 10), 20, 20)
–assign physics to the box level objects
physics.addBody(rect1, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.0})
physics.addBody(rect2, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.0})
physics.addBody(rect3, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.0})
physics.addBody(rect4, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.0})
physics.addBody(rect5, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.0})
physics.addBody(rect6, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.0})
physics.addBody(rect7, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.0})
physics.addBody(rect8, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.0})
physics.addBody(rect9, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.0})
physics.addBody(rect10, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.0})
–set number of removable of objects that are on the level/screen
local totalObjects = 10
– What to do when circle is touched
function circleTouched(event)
– so touch event will occur even when circle isn’t touched (ie, it focus targets it)
if event.phase == “began” then
display.getCurrentStage():setFocus(circle)
elseif event.phase == “ended” then
circle:applyLinearImpulse(((event.xStart - event.x) / 2), ((event.yStart - event.y) / 2), circle.x, circle.y)
display.getCurrentStage():setFocus(nil)
end
end
–What to do when a level object goes off screen
local function objectOffScreen(obj)
–tag object so it can be garbage collected and set it to nil
obj:removeSelf()
obj = nil
totalObjects = totalObjects - 1
print(totalObjects)
–check if all objects are cleared and clear level of remaining objects
if (totalObjects <= 0) then
physics.stop()
floor:removeSelf()
circle:removeSelf()
floor = nil
circle = nil
end
return obj
end
– Add event listener to detect touch event
circle:addEventListener(“touch”, circleTouched)
– Game Loop
–function that is fired each time a new frame is to be drawn
local function gameChecks(event)
–check whether circle exists
if (circle) then
–circle is colliding with left or right side of screen, so reverse X direction
if ((circle.x + (circle.contentWidth/2)) <= 0)
or ((circle.x - (circle.contentWidth/2)) >= display.contentWidth) then
local vx, vy = circle:getLinearVelocity()
circle:setLinearVelocity(vx*-1, vy)
–circle is colliding with top of screen, so reverse Y direction
elseif (circle.y <= 0) then
local vx, vy = circle:getLinearVelocity()
circle:setLinearVelocity(vx, vy*-1)
end
end
–check if any rects went off screen
if (rect1) then --make sure object exists
if (rect1.x <= 0) or (rect1.x >= display.contentWidth) then
rect1 = objectOffScreen(rect1)
end
end
if (rect2) then --make sure object exists
if (rect2.x <= 0) or (rect2.x >= display.contentWidth) then
rect2 = objectOffScreen(rect2)
end
end
if (rect3) then --make sure object exists
if (rect3.x <= 0) or (rect3.x >= display.contentWidth) then
rect3 = objectOffScreen(rect3)
end
end
if (rect4) then --make sure object exists
if (rect4.x <= 0) or (rect4.x >= display.contentWidth) then
rect4 = objectOffScreen(rect4)
end
end
if (rect5) then --make sure object exists
if (rect5.x <= 0) or (rect5.x >= display.contentWidth) then
rect5 = objectOffScreen(rect5)
end
end
if (rect6) then --make sure object exists
if (rect6.x <= 0) or (rect6.x >= display.contentWidth) then
rect6 = objectOffScreen(rect6)
end
end
if (rect7) then --make sure object exists
if (rect7.x <= 0) or (rect7.x >= display.contentWidth) then
rect7 = objectOffScreen(rect7)
end
end
if (rect8) then --make sure object exists
if (rect8.x <= 0) or (rect8.x >= display.contentWidth) then
rect8 = objectOffScreen(rect8)
end
end
if (rect9) then --make sure object exists
if (rect9.x <= 0) or (rect9.x >= display.contentWidth) then
rect9 = objectOffScreen(rect9)
end
end
if (rect10) then --make sure object exists
if (rect10.x <= 0) or (rect10.x >= display.contentWidth) then
rect10 = objectOffScreen(rect10)
end
end
end
–this fires the gameChecks function each time a new frame is to be drawn
Runtime:addEventListener(“enterFrame”, gameChecks)
[/lua]
When I toyed around with it a bit, it seems like it’s rect8, rect9, and rect10 that aren’t counting for some reason and I don’t really see why since they appear to be setup the same as all the others.