collision

Hello everyone, I’m putting this code to see if you can help me. what’s the problem? I try to make a collision that the first time it works but not the second time that I struck the objects collide they do not go into the collision again.
This code is just a sample of a much bigger project (I do not put the code of the other project because it would be very complicated for you to understand, nor I understand :slight_smile: lol)
The code is basically for when the laser collide with the square (in the case) call the collision method but that is not always what happens.
And also noticed that after the second time that I believe in the fire button he enters the “moveLaser” method twice
Can you give me a hope to do?
And do not put the document link (as it is happening) and asking me to read it, because I read it and some things (if not many) I did not understand!
Thank you for the comprehension

[lua]local widget = require( “widget” )

local physics = require(“physics”)
physics.start()

inimigo = display.newRect(0, 0, 20, 20)
inimigo.x = 150
inimigo.y = 200
physics.setGravity(0,0)
physics.addBody( inimigo, {friction = 1, bounce = 0} )

nave = display.newCircle(0, 0, 10, 10)
nave.x = 150
nave.y = 700

function onCollision(event)
print(“entrou”)
if event.phase == “began” then
event.target:removeSelf()
end
end
function createLaser(event) – cria o laser

print(“criou”)
laser = display.newImage(“Lazer.png”)
physics.addBody( laser, “dynamic”)
laser.x = nave.x
laser.y = nave.y - 50
laser.collType = “laser”
laser.name = “laser”
laser:addEventListener(“collision”, onCollision)
tm = timer.performWithDelay(10,moverLaser,0)

end

ButtonFire = widget.newButton({label=“Fire”,width= 20,height =40, x = display.contentWidth/2 - 100, y = display.contentHeight/2 + 200, shape=“circle”, fillColor = { default={ 1, 0, 1, 1 }, over={ 0, 1, 1, 1} }, onPress = createLaser} )

function moverLaser()

if laser.y ~= nil then
if laser.y <= 10 then
print(“removeu”)
display.remove( laser )
else
laser:translate( 0, -25 )

end
end
end

[/lua]

If you’re going to use the physics engine for collision detection then you should really use the physics engine to move physics bodies, such as your laser. Try laser:setLinearVelocity(0, -100) instead of the moveLaser function. Also, you are adding a new timer every time you create a laser without ending the old one, so you end up with multiple timers each moving the laser, making each laser faster than the last.

It worked for me, but I have a problem now to try to make a check for when the laser stays on y <= 10 it is removed.

The code with the changes is here.

[lua]local widget = require( “widget” )

local physics = require(“physics”)
physics.start()
inimigo = display.newRect(0, 0, 20, 20)
inimigo.x = 150
inimigo.y = 200
physics.setGravity(0,0)
physics.addBody( inimigo, {friction = 1, bounce = 0}, “dynamic” )

nave = display.newCircle(0, 0, 10, 10)
nave.x = 150
nave.y = 700

function onCollision(event)
print(“entrou”)
if event.phase == “began” then
event.target:removeSelf()
end
end
function createLaser(event) – cria o laser

print(“criou”)
laser = display.newImage(“Lazer.png”)
physics.addBody( laser, “dynamic”)
laser.x = nave.x
laser.y = nave.y - 50
laser:addEventListener(“collision”, onCollision)
laser:setLinearVelocity(0, -300)
– tm = timer.performWithDelay(10,moverLaser,0)

end

ButtonFire = widget.newButton({label=“Fire”,width= 20,height =40, x = display.contentWidth/2 - 100, y = display.contentHeight/2 + 200, shape=“circle”, fillColor = { default={ 1, 0, 1, 1 }, over={ 0, 1, 1, 1} }, onPress = createLaser} )

–[[

function moverLaser()

if laser.y <= 10 then
print(“removeu”)
display.remove( laser )
else
laser:translate( 0, -25 )

end
end

]]

[/lua]

The code with the alteration that I thought to remove when the y is <= 10 is the one here

[lua]function createLaser(event) – cria o laser

if ButtonFireValidacao == true then
print(“criou”)
ButtonFireValidacao = false
laser = display.newImage(“Lazer.png”)
physics.addBody( laser, “dynamic”)
laser.x = image.x
laser.y = image.y - 50
laser.collType = “laser”
laser.name = “laser”
laser:setLinearVelocity(0, -300)
laser:addEventListener(“collision”, onCollision)
tm = timer.performWithDelay(10,moverLaser,0)
end

end

function moverLaser()

if laser.y <= 10 then

display.remove( laser )

end
end
[/lua]

Doing the verification in the method moveLazer but is giving error speaking “attempt to compare nil with number stack traceback”

But I’m not sure how to avoid this.

can you help me?

Are you creating more than one laser at once? It looks like you’re using global variables (i.e. for “laser”) which can cause many problems. See the following tutorial on the danger of global variables and how to avoid using them:

Goodbye Globals!

Brent

If you are going to have many lasers, you should put them in a table, as below.

local lasers = {} local function createLaser() lasers[#lasers + 1] = display.newImage("laser.png") physics.addBody(lasers[#lasers], "dynamic") -- rest of function end

To deal with lasers that go off screen you could use an enterframe listener. https://docs.coronalabs.com/api/event/enterFrame/index.html

Then every frame you can use a loop to check which lasers have gone off-screen and get rid of them.

OK people

It helped a lot. THX

If you’re going to use the physics engine for collision detection then you should really use the physics engine to move physics bodies, such as your laser. Try laser:setLinearVelocity(0, -100) instead of the moveLaser function. Also, you are adding a new timer every time you create a laser without ending the old one, so you end up with multiple timers each moving the laser, making each laser faster than the last.

It worked for me, but I have a problem now to try to make a check for when the laser stays on y <= 10 it is removed.

The code with the changes is here.

[lua]local widget = require( “widget” )

local physics = require(“physics”)
physics.start()
inimigo = display.newRect(0, 0, 20, 20)
inimigo.x = 150
inimigo.y = 200
physics.setGravity(0,0)
physics.addBody( inimigo, {friction = 1, bounce = 0}, “dynamic” )

nave = display.newCircle(0, 0, 10, 10)
nave.x = 150
nave.y = 700

function onCollision(event)
print(“entrou”)
if event.phase == “began” then
event.target:removeSelf()
end
end
function createLaser(event) – cria o laser

print(“criou”)
laser = display.newImage(“Lazer.png”)
physics.addBody( laser, “dynamic”)
laser.x = nave.x
laser.y = nave.y - 50
laser:addEventListener(“collision”, onCollision)
laser:setLinearVelocity(0, -300)
– tm = timer.performWithDelay(10,moverLaser,0)

end

ButtonFire = widget.newButton({label=“Fire”,width= 20,height =40, x = display.contentWidth/2 - 100, y = display.contentHeight/2 + 200, shape=“circle”, fillColor = { default={ 1, 0, 1, 1 }, over={ 0, 1, 1, 1} }, onPress = createLaser} )

–[[

function moverLaser()

if laser.y <= 10 then
print(“removeu”)
display.remove( laser )
else
laser:translate( 0, -25 )

end
end

]]

[/lua]

The code with the alteration that I thought to remove when the y is <= 10 is the one here

[lua]function createLaser(event) – cria o laser

if ButtonFireValidacao == true then
print(“criou”)
ButtonFireValidacao = false
laser = display.newImage(“Lazer.png”)
physics.addBody( laser, “dynamic”)
laser.x = image.x
laser.y = image.y - 50
laser.collType = “laser”
laser.name = “laser”
laser:setLinearVelocity(0, -300)
laser:addEventListener(“collision”, onCollision)
tm = timer.performWithDelay(10,moverLaser,0)
end

end

function moverLaser()

if laser.y <= 10 then

display.remove( laser )

end
end
[/lua]

Doing the verification in the method moveLazer but is giving error speaking “attempt to compare nil with number stack traceback”

But I’m not sure how to avoid this.

can you help me?

Are you creating more than one laser at once? It looks like you’re using global variables (i.e. for “laser”) which can cause many problems. See the following tutorial on the danger of global variables and how to avoid using them:

Goodbye Globals!

Brent

If you are going to have many lasers, you should put them in a table, as below.

local lasers = {} local function createLaser() lasers[#lasers + 1] = display.newImage("laser.png") physics.addBody(lasers[#lasers], "dynamic") -- rest of function end

To deal with lasers that go off screen you could use an enterframe listener. https://docs.coronalabs.com/api/event/enterFrame/index.html

Then every frame you can use a loop to check which lasers have gone off-screen and get rid of them.

OK people

It helped a lot. THX