Hi, I’m eleven years old and a novice at Corona.
I’m having issues using globals with the “event.” prefix in collision events. I have it set so that if an object involved in the collision has a .myName of “coconut”, it will modify a variable called “weight”. On the other hand, if its .myName is “rock”, it will modify “weight” in a different manner.
The issue is that whenever I try calling a global using the “event.” prefix (in this case, “event.object1.myName”), it will cause an error in the terminal. Not a crash, just an error. It says that the global “event” is nil, when in fact it isn’t! Anybody know how to fix this? Here’s all the code in it (adapted from the “Bridge” sample code):
[code]
local physics = require(“physics”)
physics.start()
display.setStatusBar( display.HiddenStatusBar )
– Create some variables to say how much weight is being applied to the bridge and if it can be broken
local weight = 0
local breakable = true
– The final “true” parameter overrides Corona’s auto-scaling of large images
local background = display.newImage( “jungle_bkg.png”, 0, 0, true )
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
local pole1 = display.newImage( “bamboo.png” )
pole1.x = 50; pole1.y = 250; pole1.rotation = -12
physics.addBody( pole1, “static”, { friction=0.5 } )
local pole2 = display.newImage( “bamboo.png” )
pole2.x = 430; pole2.y = 250; pole2.rotation = 12
physics.addBody( pole2, “static”, { friction=0.5 } )
local instructionLabel = display.newText( “touch boards to break bridge, touch rocks to remove”, 22, 20, native.systemFont, 17 )
instructionLabel:setTextColor( 190, 255, 131, 150 )
local board = {}
local joint = {}
– A touch listener to “break” bridge joints
local breakJoint = function( event )
local t = event.target
local phase = event.phase
if “began” == phase then
if breakable == true then
local myIndex = t.myIndex
print( “breaking joint at board#” … myIndex )
joint[myIndex]:removeSelf() – destroy joint
breakable = false
end
end
– Stop further propagation of touch event
return true
end
– A touch listener to remove rocks
local removeBody = function( event )
local t = event.target
local phase = event.phase
if “began” == phase then
t:removeSelf() – destroy object
end
if t.myName == “coconut” then
weight = weight - 5
else
weight = weight - 10
end
– Stop further propagation of touch event
return true
end
for j = 1,16 do
board[j] = display.newImage( “board.png” )
board[j].x = 20 + (j*26)
board[j].y = 150
board[j].myIndex = j – for touch handler above
board[j]:addEventListener( “touch”, breakJoint ) – assign touch listener to board
board[j].myName = “board”
physics.addBody( board[j], { density=0.8, friction=0.3, bounce=0.3 } )
– damping the board motion increases the “tension” in the bridge
board[j].angularDamping = 5000
board[j].linearDamping = 0.7
– create joints between boards
if (j > 1) then
prevLink = board[j-1] – each board is joined with the one before it
else
prevLink = pole1 – first board is joined to left pole
end
joint[j] = physics.newJoint( “pivot”, prevLink, board[j], 6+(j*26), 150 )
end
– join final board to right pole
joint[#joint + 1] = physics.newJoint( “pivot”, board[16], pole2, 6+(17*26), 150 )
– A function for a collision to apply weight to the bridge
local function onCollision()
if event.object1.myName == “coconut” then
weight = weight + 5
elseif event.object1.myName == “rock” then
weight = weight + 10
end
end
Runtime:addEventListener( “collision”, onCollision )
local balls = {}
– function to drop random coconuts and rocks
local randomBall = function()
choice = math.random( 100 )
local ball
if ( choice < 80 ) then
ball = display.newImage( “coconut.png” )
ball.x = 40 + math.random( 380 ); ball.y = -40
physics.addBody( ball, { density=0.6, friction=0.6, bounce=0.6, radius=19 } )
ball.angularVelocity = math.random(800) - 400
ball.isSleepingAllowed = false
ball.myName = “coconut”
else
ball = display.newImage( “rock.png” )
ball.x = 40 + math.random( 380 ); ball.y = -40
physics.addBody( ball, { density=2.0, friction=0.6, bounce=0.2, radius=33 } )
ball.angularVelocity = math.random(600) - 300
ball.myName = “rock”
end
ball:addEventListener( “touch”, removeBody ) – assign touch listener to rock
balls[#balls + 1] = ball
end
– run the above function 14 times
timer.performWithDelay( 1500, randomBall, 14 )
– A weight listener to “break” bridge joints
local weightBreakJoint = function( event )
if breakable == true then
if weight >= 75 then
print( “Weight has broken the bridge” )
joint[(math.random(2,15))]:removeSelf() – destroy joint
breakable = false
end
end
end
Runtime:addEventListener(“enterFrame”, weightBreakJoint) [import]uid: 82408 topic_id: 17519 reply_id: 317519[/import]
[import]uid: 3826 topic_id: 17519 reply_id: 66570[/import]
[import]uid: 82408 topic_id: 17519 reply_id: 66578[/import]