最近,我正在编写一个新游戏,当我遇到一个我似乎无法解决的问题时。
这是代码:
function newPower()
rand = math.random( 100 )
if (rand < 80) then
powerup = display.newImage("power.png");
powerup.class = "powerup"
powerup.x = 60 + math.random( 160 )
powerup.y = -100
physics.addBody( powerup, { density=0.9, friction=0.3, bounce=0.3} )
powerup:addEventListener( "touch", handlePowerTouch )
end
end
local function handlePowerTouch( event )
if event.phase == "began" then
currentScore = currentScore * 2
currentScoreDisplay.text = string.format( "%06d", currentScore )
event.target:removeSelf()
return true
end
end
local function spawnpowers()
-- Spawn a new powerup every second until canceled.
spawnPower = timer.performWithDelay( 1000, newPower, -1 )
end任何帮助解决这个问题都将不胜感激!我遇到的问题是,当我单击"run“或"play”时,游戏开始工作,然后崩溃并显示以下消息:
addEventListener:监听器不能是零:在函数'addeventListener‘game.lua63: in function'_ listener’<--我在上面给了您game.lua:63。
谢谢
发布于 2016-03-08 20:58:48
powerup:addEventListener( "touch", handlePowerTouch )在这里,handlePowerTouch是0,因为函数定义在这一行之后。
将您的函数定义移到该行的前面,然后它就能工作了。
顺便说一句,你为什么有这么多的全局变量?您应该尽可能地使用局部变量。
https://stackoverflow.com/questions/35877071
复制相似问题