Find the error!

Can you help me to find the error in this function??

function gameListeners(event)
if event == “add” then
Runtime:addEventListener(“accelerometer”, movePaddle)
paddle:addEventListener(“touch”, dragPaddle)

 

elseif event == “remove”
then
Runtime:removeEventListener(“accelerometer”, movePaddle)
paddle:removeEventListener(“touch”, dragPaddle)
 
end
end

First off, you haven’t said what error you’re getting when you run it which is important because there is obviously more code than this.

Second, unless you’re calling the gameListeners function directly, the event will not be a string, it will be a table and its .phase and .name properties will be strings.

Error:

attempt to index global paddle (a nil value)

Paddle is out of scope of function.

You may have set it local to a function instead of local to the module

How can i write the function??

Would need to see your code the problem may not be in the function

ok

here is the code

sorry but some words are written in italian:

display.setStatusBar(display.HiddenStatusBar)
system.setAccelerometerInterval(100)
local physics = require “physics”
physics.start()
physics.setGravity(0,0)

 

local mattoni = display.newGroup()
local larghezzaMattoni = 35
local altezzaMattoni = 15
local righe
local colonne
local score = 0
local scoreIncrise = 100
local currentLevel
local vx = 3
local vy = -3
local gameEvent = “”

 

 

local W = display.contentWidth / 2
local H = display.contentHeight / 2

 

 local schermataMenuGroup = display.newGroup()
 local schermata1 = display.newImage(“sfondoMain.jpg”, 0, 0, true)
 schermata1.x = W
 schermata1.y = H
 
 local bottonePlay = display.newImage(“play.png”)
 bottonePlay:setReferencePoint(display.CenterReferencePoint)
bottonePlay.x = W
bottonePlay.y = H
bottonePlay.name = “playbutton”

 

 

 

local isSimulator = “simulator” == system.getInfo(“environment”)

 

function main()
menuPrincipale()
end

 

 

 

function menuPrincipale()
 local schermataMenuGroup = display.newGroup()
 local schermata1 = display.newImage(“sfondoMain.jpg”, 0, 0, true)
 schermata1.x = W
 schermata1.y = H

 

local  bottonePlay = display.newImage(“play.png”)
bottonePlay:setReferencePoint(display.CenterReferencePoint)
bottonePlay.x = W
bottonePlay.y = H
bottonePlay.name = “playbutton”

 

schermataMenuGroup:insert(schermata1)
schermataMenuGroup:insert(bottonePlay)

 

end

 

main()

 

 

 

function loadGame(event)
if event.target.name == “playbutton” then
transition.to(schermataMenuGroup,{time=0, alpha=0, onComplete=addGameScreen})
bottonePlay:removeEventListener(“tap”, loadGame)
end
end

 

function addGameScreen()
local sfondo2 = display.newImage(“sfondo.jpg”, 0, 0, true)
sfondo2.x = W
sfondo2.y = H

 

local paddle = display.newImage(“paddle.png”)
paddle.x = 240; paddle.y = 300
paddle.name = “paddle”

 

local ball = display.newImage(“palla.png”)
ball.x = 240; ball.y = 275
ball.name = “ball”

 

local scoreText = display.newText(“score:”, 5, 2 , “Arial”, 14)
scoreText:setTextColor(255, 255, 255, 255)
local scoreNum = display.newText(“0”, 54, 2, “Arial”, 14)
scoreNum:setTextColor(255,255,255,255)

 

local levelText = display.newText(“level:”, 420, 2, “Arial”, 14)
levelText:setTextColor(255, 255, 255, 255)

 

local levelNum =  display.newText(“1”, 460, 2, “Arial”, 14)
levelNum:setTextColor(255,255,255,255)

 

gameLevel1()
alertScreen(“Game Start”, “first level”)
timer.performWithDelay(2000, removeAlert,1)
gameListeners(“add”)

 

end

 

bottonePlay:addEventListener(“tap”, loadGame)

 

function gameLevel1()
currentLevel =1
mattoni:toFront()
local numeroDiRighe = 5
local numeroDiColonne = 7
local piazzamentoMattone = {x = (W-(larghezzaMattoni*numeroDiColonne)/2), y = 50}
for righe = 0, numeroDiRighe - 1 do
for colonne = 0, numeroDiColonne - 1 do
local mattone = display.newImage(“mattone.png”)
mattone.name = “mattone”
mattone.x = piazzamentoMattone.x + (colonne*larghezzaMattoni)
mattone.y = piazzamentoMattone.y + (righe*altezzaMattoni)
physics.addBody(mattone,“static”, {density = 1, friction = 0, bounce = 0})
mattoni.insert(mattoni, mattone)
end
end
end
 
function alertScreen(title, message)
alertBox = display.newImage(“cornice.jpg”)
alertBox.x = W; alertBox.y = H
transition.from(alertBox, {time = 1500, xScale = 0.5, yScale=0.5, transition = easing.outExpo})
titoloMessaggio = display.newText(title, 0, 0, “Arial”, 38)
titoloMessaggio:setTextColor(255,255,255,255)
titoloMessaggio.xScale = 0.5
titoloMessaggio.yScale = 0.5
titoloMessaggio:setReferencePoint(display.CenterReferencePoint)
titoloMessaggio.x = display.contentCenterX
titoloMessaggio.y = display.contentCenterY - 15

 

testoMessaggio = display.newText(message, 0, 0, “Arial”, 24)
testoMessaggio:setTextColor(255,255,255,255)
testoMessaggio.xScale = 0.5
testoMessaggio.yScale = 0.5
testoMessaggio:setReferencePoint(display.CenterReferencePoint)
testoMessaggio.x = display.contentCenterX
testoMessaggio.y = display.contentCenterY + 15

 

alertDisplayGroup = display.newGroup()
alertDisplayGroup:insert(alertBox)
alertDisplayGroup:insert(titoloMessaggio)
alertDisplayGroup:insert(testoMessaggio)
end

 

function movePaddle(event)
paddle.x = display.contentCenterX - (display.contentCenterX * (event.yGravity*3))
if ((paddle.x - paddle.width * 0.5) < 0)then
paddle.x = paddle.width * 0.5
elseif((paddle.x + paddle.width * 0.5) > display.contentWidth) then
paddle.x = display.contentWidth - paddle.width * 0.5
end
end

 

function gameListeners(event)
if event == “add” then
Runtime:addEventListener(“accelerometer”, movePaddle)

 paddle:addEventListener(“touch”, dragPaddle)

 

elseif event == “remove”
then
Runtime:removeEventListener(“accelerometer”, movePaddle)
paddle:removeEventListener(“touch”, dragPaddle)
 
end
end

At top of code put local paddle and remove local from paddle=display.newImage…

now there is another error:

assertion failed!

stack traceback

that is caused also a by scope issue

you need to do same thing to ball,scoretext,leveltext,levelnum

then you may need to rearrange where your adding the event listeners

what should i do to rerrange the event listners?

could you show me the exact error msg

assertion failed
stack trecebeck
in function asset in function getorCreate table’ i
In function addEventListener
In function addEventListener

First off, you haven’t said what error you’re getting when you run it which is important because there is obviously more code than this.

Second, unless you’re calling the gameListeners function directly, the event will not be a string, it will be a table and its .phase and .name properties will be strings.

Error:

attempt to index global paddle (a nil value)

Paddle is out of scope of function.

You may have set it local to a function instead of local to the module

How can i write the function??