Calling a sound within an enterframe listener logic problem

Hi,

This has been somewhat difficult for me to get my head around for a few days and I keep putting it off but now is the time I need to address it.

  1. I have ship controlled by a joystick which is updated using enterframe.

  2. when the ship reaches 80px the thrust sprite is played, this is attache to the ship using enterframe not a weld joint

  3. the ship can also reverse meaning 80px becomes 440px so after440px thrust will play and below it will not.

  4. everything working fine

Now my issue I need to play a sound when the ship hits 80px (the thrust sound) and below 80px it stops the sound, problem is it gets called  60 times because of the enterframe listener.

I tried using a flag / functions outside the listener and all manner of things but it just isnt happening.

I know I could use and invisible collision area to trigger the sound but I have a listener already so want to do it without.

below is the piece of code with comments of what i need to achieve:

function update () &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player.x \> 80 and switchRotation == 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playerThrust.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -----------------------switch thrust sound on --------------------- &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif player.x \< 80 and switchRotation == 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerThrust.isVisible = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ----------------------switch thrust sound off---------------------- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player.x \> 440 and switchRotation == 1 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playerThrust.isVisible = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -----------------------switch thrust sound off---------------------&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif&nbsp; player.x \< 440 and switchRotation == 1 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerThrust.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -----------------------switch thrust sound on --------------------- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end end Runtime:addEventListener("enterFrame", update)

[lua]

function update ()

      local prevThrust = playerThrust.isVisible

      if player.x > 80 and switchRotation == 0 then

          playerThrust.isVisible = true

      elseif player.x < 80 and switchRotation == 0 then

          playerThrust.isVisible = false

      end

      if player.x > 440 and switchRotation == 1 then

          playerThrust.isVisible = false

      elseif  player.x < 440 and switchRotation == 1 then

                 playerThrust.isVisible = true

      end

      if playerThrust.isVisible ~= prevThrust then

        if playerThrust.isVisible then

          – play thrust sound

        else

          – turn off thrust sound

        end

      end

end

Runtime:addEventListener(“enterFrame”, update)

[/lua]

Cheers Nick,

I really need to be more clear when I ask for help…doh

anyway got it sorted in the end like so:

&nbsp; local isPlayingSound = false &nbsp;&nbsp;&nbsp; local function playSound() &nbsp;&nbsp;&nbsp;&nbsp; audio.play(thrustSound, {loops = -1, channel = 15}) &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local function stopSound() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; audio.stop(15) &nbsp;&nbsp;&nbsp; end &nbsp; function update() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player.x \> 80 and switchRotation == 0 and isPlayingSound == false then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playerThrust.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isPlayingSound = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playSound() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif player.x \< 80 and switchRotation == 0 and isPlayingSound == true then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playerThrust.isVisible = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isPlayingSound = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stopSound() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ----------------------switch thrust sound off---------------------- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player.x \> 440 and switchRotation == 1 and isPlayingSound == false then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playerThrust.isVisible = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playSound() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -----------------------switch thrust sound off--------------------- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif&nbsp; player.x \< 440 and switchRotation == 1 and isPlayingSound == true then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playerThrust.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stopSound() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -----------------------switch thrust sound on --------------------- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end end Runtime:addEventListener("enterFrame", update)

[lua]

function update ()

      local prevThrust = playerThrust.isVisible

      if player.x > 80 and switchRotation == 0 then

          playerThrust.isVisible = true

      elseif player.x < 80 and switchRotation == 0 then

          playerThrust.isVisible = false

      end

      if player.x > 440 and switchRotation == 1 then

          playerThrust.isVisible = false

      elseif  player.x < 440 and switchRotation == 1 then

                 playerThrust.isVisible = true

      end

      if playerThrust.isVisible ~= prevThrust then

        if playerThrust.isVisible then

          – play thrust sound

        else

          – turn off thrust sound

        end

      end

end

Runtime:addEventListener(“enterFrame”, update)

[/lua]

Cheers Nick,

I really need to be more clear when I ask for help…doh

anyway got it sorted in the end like so:

&nbsp; local isPlayingSound = false &nbsp;&nbsp;&nbsp; local function playSound() &nbsp;&nbsp;&nbsp;&nbsp; audio.play(thrustSound, {loops = -1, channel = 15}) &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local function stopSound() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; audio.stop(15) &nbsp;&nbsp;&nbsp; end &nbsp; function update() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player.x \> 80 and switchRotation == 0 and isPlayingSound == false then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playerThrust.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isPlayingSound = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playSound() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif player.x \< 80 and switchRotation == 0 and isPlayingSound == true then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playerThrust.isVisible = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isPlayingSound = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stopSound() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ----------------------switch thrust sound off---------------------- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player.x \> 440 and switchRotation == 1 and isPlayingSound == false then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playerThrust.isVisible = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playSound() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -----------------------switch thrust sound off--------------------- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif&nbsp; player.x \< 440 and switchRotation == 1 and isPlayingSound == true then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playerThrust.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stopSound() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -----------------------switch thrust sound on --------------------- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end end Runtime:addEventListener("enterFrame", update)