Why am I getting this addEventListener error on line 88?

I can’t figure out why I’m getting this error on v. 2025.3721. It appears to trigger on line 88.

14:11:01.060  ERROR: Runtime error
14:11:01.060  D:\a\corona\corona\platform\resources\init.lua:82: addEventListener: listener cannot be nil: nil
14:11:01.060  stack traceback:
14:11:01.060  	[C]: in function 'error'
14:11:01.060  	D:\a\corona\corona\platform\resources\init.lua:82: in function 'getOrCreateTable'
14:11:01.060  	D:\a\corona\corona\platform\resources\init.lua:115: in function 'addEventListener'
14:11:01.060  	D:\a\corona\corona\platform\resources\init.lua:301: in function 'addEventListener'
14:11:01.060  	D:\a\corona\corona\platform\resources\init.lua:350: in function 'addEventListener'
14:11:01.060  	C:\Users\USER\Documents\Corona Projects\asteroidTest\main.lua:88: in main chunk

Here is my code:

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

local screenWidth = display.contentWidth -- gets screen width
local screenHeight = display.contentHeight
local centerX = screenWidth/2
local centerY = screenHeight/2
local player
local playing = true
local spawnEnemy = 0
local enemies = {} -- table to track enemies, {} = table
--
local function hasCollided(obj1, obj2) -- crash handler checking for thing collisions
	if (obj1 == nil) then
		return false
	end
	if (obj2 == nil) then
		return false
	end
	--
	local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMax
	local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMax <= obj2.contentBounds.xMax
	local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMax
	local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMax <= obj2.contentBounds.yMax
	return (left or right) and (up or down)
end
local function handleCrash()
	playing = false
end
local function addEnemy()
	spawnEnemy = 0
	local enemy = display.newRect(math.random(20, screenWidth - 20), -50, 20, 20) -- randomize enemy spawn
	enemy.fill = {0, 1, 0}
	enemies[#enemies + 1] = enemy -- appends enemy count to enemies table
end
local function enterFrame() -- function to make changes per frame
	if playing then 
		if player.direction == 'left' then
			player.x = player.x -1
		elseif player.direction == 'right' then
			player.x = player.x + 1
		end
		--
		spawnEnemy = spawnEnemy + 1
		if spawnEnemy >= 60 then
			addEnemy()
		end
		--
		for i = 1, #enemies do -- get enemy from enemies table loop
			local enemy = enemies [i]
			if enemy ~= nil then -- checks if enemy is nil. ~= is the same as != in python
				enemy.y = enemy.y + 2 -- descends the enemy down the y axis
				if hasCollided(enemy) then -- if there is a collision bug use the handCrash crash handler
					handleCrash()
				end
				if enemy.y > screenHeight then -- tells the game to remove the enemies that travel off screen and
												-- and set their values to 'nil', empty
					enemy:removeSelf()
					enemies[i] = nil
				end
			end
		end
	end
	--
	local function handleTouch(event) -- function to change player direction on tap
		if event.phase == 'began' then
			if player.direction == 'left' then
				player.direction = 'right'
			else player.direction = 'left'
			end
		end
	end
end
--
local background = display.newRect(centerX, centerY, screenWidth, screenHeight)
background.fill = {0.1, 0.1, 0.1}

player = display.newRect(centerX, screenHeight - 50, 40, 40) -- makes a rectangle of specific size 
																-- (40x40) and in position (centerX, screenHeight)
player.direction = 'left'
player.fill = {1, 0, 0} -- color rectable red (R, G, B)

Runtime:addEventListener('enterFrame', enterFrame) -- listener listening for 'enterFrame' then running 
													-- the function enterFrame
Runtime:addEventListener('touch', handleTouch)

Any help is greatly appreciated.

handleTouch is local function of enterFrame function, need to move it out enterFrame

2 Likes

That worked thank you!