You need to mess around with the code a bit more, but you’re on the right track.
Below is a snippet showing a working sine wave with you newLaser object. Try taking this, making sure it works and you understand the moving pieces, then attempt to adapt it to your own project:
centerX = display.contentCenterX centerY = display.contentCenterY newLaser = display.newRect(50,50,50,50) newLaser.x = centerX-100 newLaser.y = centerY local y0=newLaser.y newLaser:toBack() local Amplitude = 100 local Theta = 7 local function moveLaser() Theta = Theta + .05 local x = Theta local y=math.sin(x)\*Amplitude newLaser.x = newLaser.x +.5 print(newLaser.y) newLaser.y = y+(centerY) if newLaser.x \> display.contentWidth then newLaser.x = 0 end end Runtime: addEventListener ("enterFrame",moveLaser)
Somebody? please? hehe my problem is when the code reach this point it make strange things. I just want to remove the “newlaser” when newlaser.y reach content.height screen! (y=-40 or when reach the limits of upscreen)
if (newLaser.y \> display.contentHeight) then display.remove( newLaser ) end
and here is the complete code , modified by me , in order to understand it and apply it to my game code.
centerX = display.contentCenterX centerY = display.contentCenterY + 100 newLaser = display.newRect(50,50,50,50) newLaser.x = centerX newLaser.y = centerY local y0=newLaser.x newLaser:toBack() local Amplitude = 50 local Theta = 7 local function moveLaser() --theta es la rapidez (no la vel) de movimiento en la sin que es de 0.5 lenta y 3 super rapida. Theta = Theta + 1 local y = Theta local x=math.sin(y)\*Amplitude --Velocidad de la sinusoidal modifcada por el valor 20 newLaser.y = newLaser.y - 20 print(newLaser.y) newLaser.x = x+(centerX) print ("SIN IS WORKING") if (newLaser.y \> display.contentHeight) then display.remove( newLaser ) end end Runtime: addEventListener ("enterFrame",moveLaser)
it doesnt enter in a loop, is like the image appears down the screen and go up, describing sinwaves, like you you taught me with your sample, just that in your sample, it repeats when reach “width” and return to 0, but in my modified sample, it just go up and never returns, no matter if I already put this:
if (newLaser.y \> display.contentHeight) then display.remove( newLaser ) end end Runtime: addEventListener ("enterFrame",moveLaser)
It continue going up till infinite, and I already check that with the debugger. It doesnt loop like in your sample. Do you think that, if I put the removelistener inside, it will help me to achieve remove the “newlaser” and be ready to call another?
If your most recent post is your working code, the issue is that your object is moving up the screen, whereas you are checking for a height threshold that would be reached if the object were moving down the screen.
If you change this:
if (newLaser.y \> display.contentHeight) then
to this:
if (newLaser.y \< 0) then
you would see the code executing with that condition. Keep in mind though, my previous comment still applies, in that if you remove the your object without removing an enterFrame listener that depends on that object, you will get an error. Check out the Corona scope guide that can give you a bit more insight when it comes to objects and how scope comes into play:
Thank you, Alex@Panc, you are awesome, now it works! and it actually removes the event listener!
Here is the code for future developers with the same questions.
centerX = display.contentCenterX centerY = display.contentCenterY + 100 newLaser = display.newRect(50,50,50,50) newLaser.x = centerX newLaser.y = centerY newLaser:toBack() local Amplitude = 50 local Theta = 7 local function moveLaser() --theta es la rapidez (no la vel) de movimiento en la sin que es de 0.5 lenta y 3 super rapida. Theta = Theta + 1 local y = Theta local x=math.sin(y)\*Amplitude --Velocidad de la sinusoidal modifcada por el valor 20 newLaser.y = newLaser.y - 20 print(newLaser.y) newLaser.x = x+(centerX) print ("SIN IS WORKING") if newLaser.y \< -100 then print ("It enters to if statemnet ") Runtime: removeEventListener ("enterFrame",moveLaser) print ("eventlistener stopped") end end Runtime: addEventListener ("enterFrame",moveLaser)
Sorry to bother you, again, Alex, Im just kinda new in LUA. The code with your sample, modified by me, was working like a charm. But, then, I used it in my code. It worked well, the sinwave appears and stops well, problem is when the sinwave collides with an object. It threw me this error.
19:36:41.604 C:\Users\usuario\Documents\Corona Projects\AAH! Space Zombies!\game.lua:432: attempt to perform arithmetic on field ‘y’ (a nil value)
19:36:41.604 stack traceback:
19:36:41.604 C:\Users\usuario\Documents\Corona Projects\AAH! Space Zombies!\game.lua:432: in function <C:\Users\usuario\Documents\Corona Projects\AAH! Space Zombies!\game.lua:425>
19:36:41.604 ?: in function <?:169>
elseif ( power== 3) then centerX = display.contentCenterX centerY = display.contentCenterY + 100 local newLaserW = display.newImageRect( mainGroup, objectSheet, 5, 14, 40 ) physics.addBody( newLaserW, "dynamic", { isSensor=true } ) newLaserW.isBullet = true newLaserW.myName = "laser" newLaserW.x = centerX newLaserW.y = centerY newLaserW:toBack() local amplitude = 50 local theta = 7 local function moveLaser() --theta es la rapidez (no la vel) de movimiento en la sin que es de 0.5 lenta y 3 super rapida. theta = theta + 1 local p= theta local x=math.sin(p)\*amplitude --Velocidad de la sinusoidal modifcada por el valor 20 newLaserW.y = (newLaserW.y) - 20 print(newLaserW.y) newLaserW.x = x+(centerX ) print ("SIN IS WORKING") if newLaserW.y \< -100 then print ("It enters to if statemnet ") Runtime: removeEventListener ("enterFrame",moveLaser) print ("eventlistener stopped") end end Runtime: addEventListener ("enterFrame",moveLaser) end end
** newLaserW.y = (newLaserW.y) - 20 **seems to be the problem, I read this happens because a variable is undefined. But, bro, I see the variable very defined hahah…
this is what happens when the laser hit and asteroid (collision) just in case :S :( :wacko: :unsure:
local function onCollision( event ) if ( event.phase == "began" ) then local obj1 = event.object1 local obj2 = event.object2 if ( ( obj1.myName == "laser" and obj2.myName == "asteroid" ) or ( obj1.myName == "asteroid" and obj2.myName == "laser" )) then print ("me estas tocando") ---- -- Remove both the laser and asteroid display.remove( obj1 ) display.remove( obj2 ) Runtime: removeEventListener ("enterFrame",moveLaser) -- Play explosion sound! audio.play( zombieSound ) -- for i = #asteroidsTable, 1, -1 do if ( asteroidsTable[i] == obj1 or asteroidsTable[i] == obj2 ) then table.remove( asteroidsTable, i ) break end end -- Increase score score = score + 100 scoreText.text = "Score: " .. score --LLama a scoreboss¡¡¡??? scoreBoss()
Nevermind, I solved it! It was a little tricky, I was declaring nil when it was colliding with an object, and enterframe use newlaser as nil, so I used an “if or” statement to fix this. Thank you, now is full working! This was a great experience, thank you so much, Alex.
Hope this topic can help another people in the future.