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 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]