So I tried to remove stuff and figure it out, but I am getting the same thing… This is a youtube video showing what is happening.
Ok, so i figured out the problem line… i just don’t understand why it’s not working.
transition.to( newLaser, { y=-40, time=500, onComplete = function() display.remove( newLaser ) end } )
This set of code is causing the issue… specifically, the onComplete section. If I comment out that line, it works great (except it never removes the lasers).
I think this has something to do with once other lasers make it to that -40 or (Completion point) , it is deleting all of the lasers and continues to do so until released. Since I am using a touch event instead of tap this seems to be causing an issue.
So, to fix this… do I need to make a table and keep track of these lasers some how and delete them in a timeloop or something?
Hey guys. You’re doing this wrong. You need to stop a channel.
See my second to last post in this thread (posted at 1:25 pm):
https://forums.coronalabs.com/topic/70793-how-to-stop-a-audio-after-it-has-finished/
is there something wrong with just calling the audio.play() each time I fire? I am not sure what the reason behind the loop is.
This is what I am using now (for sound) and it seems to work fine as best I can tell.
local function onTouch(event) local phase = event.phase local newLaser if phase == "began" then local function fireLaser() -- Play fire sound! audio.play( fireSound ) newLaser = display.newImageRect( mainGroup, objectSheet, 5, 14, 40 ) physics.addBody( newLaser, "dynamic", { isSensor=true } ) newLaser.isBullet = true newLaser.myName = "laser" newLaser.x = ship.x newLaser.y = ship.y newLaser:toBack() transition.to( newLaser, { y=-100, time=500, onComplete = function() display.remove( newLaser ) end } ) end bulletTimer = timer.performWithDelay(200, fireLaser, -1) --The 200 is the rate of fire, fastest posisble is 17 elseif phase == "ended" then if bulletTimer then timer.cancel(bulletTimer) end end end
This is purely a suggestion that I put together blind, but I hope it approximates what you’re trying to do.
ORIGINAL
-- -- ORIGINAL -- local function fireLaser() -- Play fire sound! audio.play( fireSound ) local newLaser = display.newImageRect( mainGroup, objectSheet, 5, 14, 40 ) physics.addBody( newLaser, "dynamic", { isSensor=true } ) newLaser.isBullet = true newLaser.myName = "laser" newLaser.x = ship.x newLaser.y = ship.y newLaser:toBack() transition.to( newLaser, { y=-40, time=500, onComplete = function() display.remove( newLaser ) end } ) end ship:addEventListener( "tap", fireLaser )
SUGGESTION
-- -- REPLACEMENT - Not Perfect; But should work OK... (may contain typos) -- -- NOTE: Assumes you already loaded the sound 'fireSound' local laserChannel = audio.findFreeChannel() -- Find a free channel to play sounds on audio.reserveChannels( laserChannel ) -- Lock it for use by laser sound ONLY. local firePeriod = 100 -- one shot every 100 ms ==\> 10 shot per second local lastTime = system.getTimer() local function fireLaser() audio.stop( laserChannel ) audio.play( fireSound, { channel = laserChannel } ) local newLaser = display.newImageRect( mainGroup, objectSheet, 5, 14, 40 ) physics.addBody( newLaser, "dynamic", { isSensor=true } ) newLaser.isBullet = true newLaser.myName = "laser" newLaser.x = ship.x newLaser.y = ship.y newLaser:toBack() transition.to( newLaser, { y=-40, time=500, onComplete = function() display.remove( newLaser ) end } ) end local function onEnterFrame( self ) if( ship.firing ) then local curTime = system.getTimer() local dt = lastTime - curTime if( dt \>= firePeriod ) then fireLaser() end end end local function onTouch( self, event ) local phase = event.phase local id = event.id if phase == "began" then display.getCurrentStage():setFocus( self, id ) self.isFocus = true self.firing = true elseif self.isFocus then if phase == "ended" then display.getCurrentStage():setFocus( self, nil ) self.isFocus = false self.firing = false audio.stop( laserChannel ) end end return false end ship.touch = onTouch ship.enterFrame = onEnterFrame ship:addEventListener( "touch" ) Runtime:addEventListner( "enterFrame", ship )
So the problem occurred because you made the newLaser var. local outside of the fireLaser function. Make inside of it. (Actually it was my mistake. But I edited the post later.)
OMG, silly me… thanks! that fixed it. You guys are awesome.