Hi, I’m trying to make a pool-shot effect on my first app, and I’m having difficulties, the latest one that I can’t figure out is this. The error is: main.lua:86: attempt to call method ‘addEventListener’ (a nil value)
My code:
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0, 0)
physics.setScale( 60 )
local n = 0
–Hiding status bar
display.setStatusBar( display.HiddenStatusBar )
– Setting background color
display.setDefault( “background”, 100,255,250 )
–Code for balls
local Ball = display.newImage(“Ball.png”)
Ball.x = 200
Ball.y = 250
physics.addBody( Ball, “dynamic” )
local Ball2 = display.newImage(“Ball.png”)
Ball2.x = 200
Ball2.y = 50
physics.addBody( Ball2, “dynamic” )
local PlayerChar = display.newImage( “PlayerChar.png”)
PlayerChar.x = 50
PlayerChar.y = 160
physics.addBody( PlayerChar, “dynamic” )
PlayerChar.linearDamping = 0.3
PlayerChar.angularDamping = 0.8
PlayerChar.isBullet = true --force continue collision detection to stop really fast shots from breaking things
PlayerChar = { density=0.8, friction=0.2, bounce=0.5, radius=15 }
target = display.newImage( “target.png” )
target.x = PlayerChar.x; target.y = PlayerChar.y; target.alpha = 0
local function shot( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
--stop current ball motion, if any
t:setLinearVelocity( 0, 0 )
t.angularVelocity = 0
target.x = t.x
target.y = t.y
startRotation = function()
target.rotation = target.rotation + 4
end
Runtime:addEventListener( “enterFrame”, startRotation )
local showTarget = transition.to( target, { alpha = 0.4, xScale=0.4, yScale=0.4, time=200 } )
myLine = nil
elseif t.isFocus then
if “moved” == phase then
if( myLine ) then
myLine.parent:remove( myLine ) --erase previous line, if any
end
elseif “ended” == phase or “cancelled” == phase then
Runtime:removeEventListener( “enterFrame”, startRotation )
end
local hideTarget = transition.to( target, { alpha=0, xScale=1.0, time=200, onComplete=stopRotation } )
if( myLine ) then
myLine.parent:remove( myLine )
end
end
return true
end
PlayerChar:addEventListener( “touch”, shot )[/lua]