I made a simple “Angry Birds” type physics “game” (if you want to call it that) for practice. It’s really basic, just creates a circle and a stack of crates. The circle is pulled back and launched to knock the crates down. I wanna be able to make the crates “drag and drop”, so that I could stack them up again without restarting the game.
Here’s what I got:
– Time to throw in the crates.
for i = 0, 10 do
local crate = display.newImageRect(“images/crate2.png”, 25, 25)
crate.anchorX = 0.0
crate.x = display.contentCenterX + 50
crate.y = (display.contentCenterY - 100) + 10*i
physics.addBody(crate, “dynamic”, {density = 1.0, friction = 0.3, bounce= 0.0, isSensor = false})
end
function circleTouched(event)
if event.phase == “began” then
display.getCurrentStage():setFocus(circle)
elseif event.phase == “ended” then
circle:applyLinearImpulse(((event.xStart - event.x) / 4), ((event.yStart - event.y) / 4), circle.x, circle.y)
display.getCurrentStage():setFocus(nil)
end
end
function crateTouched(event)
if event.phase == “began” then
self.markX = self.x
self.markY = self.y
elseif event.phase == “moved” then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
end
return true
end
– Add event listener
circle:addEventListener(“touch”, circleTouched)
crate:addEventListener(“touch”, crateTouched)
I’m getting the error on the bottom (where the event listener is) that crate is a nil value. I’d imagine because the crate variable is within the “for” function, and cannot be called anywhere else? If that’s the case, how would I go about fixing this? This is only my third day messing around with Corona, so sorry if I come off as a noob.
Any help would be appreciated.
P.S. If necessary, I will post the entire code…which isn’t very long at all.