Trying to write function

Trying to write function so that when object collides with floor score resets to 0 and in case of any other collisions it adds to score.
Here is what I tried:

local function onCollision(event)
		if event.phase == "began" then
		audio.play(bounce)
			if event.object1.name == "floor" then
				local score = 0
			else Runtime:addEventListener("collision", addToScore)
			end
			print (event.object1.name.." and "..event.object2.name)
		end	
	end

It did not go as planned as this adds to score when objects collide with floor and app crashes when collide with something else. Giving error addEventListener: listener cannot be nil: nil

Here is my add to score function

local function addToScore()
	score = score + 1
	scoreText.text = "score: "..score
	end 

It is also set on touch event to add to score and that works fine.

The event listener needs to be outside the of the onCollision function

I changed it up a bit. *Replace body with whatever your object is and give a whirl

local function addToScore()
	score = score + 1
	scoreText.text = "score: "..score
end 

function body:collision(event)
		if event.phase == "began" then
		audio.play(bounce)
			if event.other.name == "floor" then
				local score = 0
			else 
              addToScore()
			end
			print (event.target.name.." and "..event.other.name)
		end	
	end

body:addEventListener( "collision")
1 Like

Yes there is event listener outside of it

Runtime:addEventListener("collision", onCollision)
Runtime:addEventListener("collision", addToScore)

Still getting same after changing code collision with floor adds to score and any other collision crashes app.

I think you need to show us all your code.

_W = display.contentWidth
_H = display.contentHeight

module ( ... , package.seeall ) 

function new( )
	local localGroup = display.newGroup ( ) 
	
	local time_remain = 24
	time_up = false
	ready = false
	
	local time_remain = 24
	
	-- Display countdown text 
	local countdowntxt = display.newText( time_remain, 300, 11, native.systemFont, 16 )
	countdowntxt:setTextColor ( 255, 0, 0 )
 	
 	--Add funtion for countdown
	local function countdown (e)
    	if (time_remain == 24) then
        	ready = true
    	end
    	time_remain =time_remain -1
    	print(time_remain)
    	countdowntxt.text = time_remain
    	
    	if(time_remain == 0) then
    		time_up = true
    		
    	end
	end
 	
	gametimer = timer.performWithDelay ( 1000, countdown, 24 )
	
	
	--> Add physics engine, start up the engine, and apply gravity 
    local physics = require ("physics") 
    physics.start( ) 
     -- Set gravity to act "down" (ie, toward the bottom of the device) 
	physics.setGravity(0,  9.8) 
	
	local bounce = audio.loadSound("bbounce.mp3")
	
    --physics.setDrawMode("hybrid")
	
	local function onCollision(event)
		if event.phase == "began" then
		audio.play(bounce)
			if event.object1.name == "floor" then
				score = 0
			else 
				addToScore()
			end
			print (event.object1.name.." and "..event.object2.name)
		end	
	end

	-->  Add background image 
	local background = display.newImage("wp1.png")  
	background.x = display.contentCenterX
	background.y = display.contentCenterY
	
	localGroup:insert(background)	 
	
	--> Add ball to stage and position
	local ball = display.newImage("Basketball.png")
	ball.x = display.contentWidth/2
	ball.y = display.contentHeight/2
	--> Turn ball into physics body
	physics.addBody(ball, { bounce = 0.5, radius = 40, friction = 1.0 } )
	ball.name = "ball"
 	
 	localGroup:insert(ball)
 	
 	--> Add ball2
	local ball2 = display.newImage("Basketball.png")
	ball2.x = ball.x - 105
	ball2.y = display.contentHeight/2
	physics.addBody(ball2, {bounce = 0.5, radius = 40, friction = 1.0 } )
	ball2.name = "ball 2"
	
	localGroup:insert(ball2)
	

    --> Add ball3
	local ball3 = display.newImage("Basketball.png")
	ball3.x = ball.x + 105
	ball3.y = display.contentHeight/2
	physics.addBody (ball3, {bounce = 0.5, radius = 40, friction = 1.0 } )
	ball3.name = "ball 3"
	
	localGroup:insert(ball3)


	 system.activate( "multitouch" )

	-- Define wall graphics (rectangles)
	local leftWall = display.newRect(0, 0, 0.1, display.contentHeight )
	leftWall.y = display.contentCenterY
	leftWall.name = "left wall"
	local rightWall = display.newRect(display.contentWidth, 0, 0.1,  display.contentHeight)
	rightWall.y = display.contentCenterY
	rightWall.name = "right wall"
	local ceiling = display.newRect(0, 0, display.contentWidth, 0.1  )
	ceiling.x = display.contentCenterX
	ceiling.name = "ceiling"

	-- Turn wall graphics into physical bodies
	physics.addBody( leftWall, "static", { bounce = 0.1 } )
	physics.addBody( rightWall, "static", { bounce = 0.1 } )
	physics.addBody(ceiling, "static", { bounce = 0.1 } )

	--> Add floor image and position
	local floor = display.newImage("floor.png")
	floor.y = display.contentHeight - floor.contentHeight/2
	floor.x = display.contentCenterX
	floor.name = "floor"
	
	--> Turn floor into physics body
	physics.addBody(floor, "static",  { bounce = 0.2, friction = 1.0 } )
	
	localGroup:insert(floor)

	-- Define our touch event listner
	function moveBall(event)
		local ball = event.target
		ball:applyLinearImpulse( 0, -0.3, event.x, event.y )
	end
	
	-- Add the listener to our ball 
	
	ball:addEventListener("touch", moveBall)
	ball2:addEventListener("touch", moveBall) 	
	ball3:addEventListener("touch", moveBall)
	

 	
 	--Saving/Loading Stuff
	local ego = require "ego"
	local saveFile = ego.saveFile
	local loadFile = ego.loadFile

 	-- Create score text 
	local score = 0
	local scoreText = display.newText(score, 220, 11, native.systemFont, 16)
	scoreText:setTextColor( 255, 255, 255 )
	scoreText.text = "Score: "..score


     -- Function to add to score and update scoreText
	local function addToScore()
	score = score + 1
	scoreText.text = "Score: "..score
	end 
	
	--Load highscore value from file. (It will initally be a string.)
	highscore = loadFile ("highscore.txt")
	
	--If the file is empty (this means it is the first time you've run the app) save it as 0
	local function checkForFile ()
		if highscore == "empty" then
		highscore = 0
		saveFile("highscore.txt", highscore)
		end
	end
	checkForFile()
	
	--Print the current highscore
	print ("Highscore is: ", highscore)
	local highscoreText = display.newText(highscore, 80, 11, native.systemFont, 16)
	highscoreText:setTextColor( 255, 255, 0 )
	highscoreText.text = "Highscore is: "..highscore

	--When the app is quit (or simulator refreshed) save the new highscore
	--(If score > highscore the data will not be changed)

	local function onSystemEvent ()
		if score > tonumber(highscore) then --We use tonumber as highscore is a string when loaded
		saveFile("highscore.txt", score)
		local highscoreText = display.newText(highscore, 200, 0, native.systemFont, 16)
	highscoreText:setTextColor( 255, 255, 0)
	highscoreText.text = "Highscore is: "..highscore
		end
	end
	Runtime:addEventListener( "system", onSystemEvent )
	
	-- Add listeners to balls so it will add to score when they are touch
	ball:addEventListener("touch", addToScore)
	ball2:addEventListener("touch", addToScore)
	ball3:addEventListener("touch", addToScore)
	
        Runtime:addEventListener("collision", onCollision)
	Runtime:addEventListener("collision", addToScore)
	
	
	local function checkTime(e)
 		 if time_remain == 0 then
 		 	Runtime:removeEventListener("enterFrame", checkTime)
   	   	director:changeScene("gameover")

 		 end
	end 
	Runtime:addEventListener("enterFrame", checkTime)

	return localGroup
end


That is all my code from game.lua. It all worked good and without problems. Last function I did is onCollision first audio and print options and both worked. When I added if statement with floor problems started.

This is error I get in case of any other collision not involving floor.

Blockquote13:51:24.072 ERROR: Runtime error
13:51:24.072 C:\Users\omen\OneDrive\Documents\Corona Projects\BasketballBounce\game.lua:62: attempt to call global ‘addToScore’ (a nil value)
13:51:24.072 stack traceback:
13:51:24.072 C:\Users\omen\OneDrive\Documents\Corona Projects\BasketballBounce\game.lua:62: in function ‘func’
13:51:24.072 D:\a\corona\corona\platform\resources\init.lua:202: in function <D:\a\corona\corona\platform\resources\init.lua:189>

What is the purpose of this line?

module ( ... , package.seeall ) 

The error message seems to be generated from some code not included in the code in your previous post?

Is the code you shared from game.lua or somewhere else?

Where is new() being called?

function new( )

The addToScore() function is localized within the function new()

You will need to give it the ability to be accessed from outside of new().