Skip navigation
Marcos Ostos
Currently Being Moderated

Play music after an object drags

Jun 8, 2012 6:58 AM

Hello all,

 

I´m trying to do a music player that will start playing music when an object move towards the Y axis.

 

So basically, I have an object, a circle in this case, that will move only 56 px up and down with a startDrag, what I need is that when the circle is at Y = -56 px (it´s higger position) music starts (Play),

and when the circle is dragged down, music should stop.

 

At the moment, music plays on every click, the boolean is not working either, so every time I click music overlap.

 

I past my code in case that helps.

 

import flash.media.Sound;

import flash.media.SoundChannel;

import flash.media.SoundMixer;

/* Arrastrar y colocar

Permite que la instancia del símbolo especificado se pueda mover con una acción de arrastrar y colocar.

*/

var fl_SC:SoundChannel;

var fl_ToPlay:Boolean = true;

 

 

 

 

vinilo_click_drop.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);

 

 

function fl_ClickToDrag(event:MouseEvent):void

{

          vinilo_click_drop.startDrag(false, new Rectangle(0,-56,0,56));

          if ((vinilo_click_drop.Y !=0) && (fl_ToPlay))

          {

                    var s:Sound = new Sound(new URLRequest("http://www.helpexamples.com/flash/sound/song1.mp3"));

                    fl_SC = s.play();

          }

 

          else if (fl_ToPlay)

          {

                    fl_SC.stop();

          }

          fl_ToPlay = !fl_ToPlay;

}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);

 

 

function fl_ReleaseToDrop(event:MouseEvent):void

{

          vinilo_click_drop.stopDrag();

}

 

Thank you all for the help.

 
Replies
  • Currently Being Moderated
    Jun 8, 2012 7:13 AM   in reply to Marcos Ostos

    I don't see anywhere where you test to see if the y property of vinilo_click_drop is equal to -56 such that the sound would be triggered to play. 

     

    If the intention is to play based on that specific value, then you need to continuously monitor its position when dragging is occuring.  You need to have a MOUSE_MOVE or ENTER_FRAME listener for that.

     

    If the intention is to wait until dragging stops to decide whether to play the sound or not, then you need to check the y property in the stopDrag end of things.

     

    I am not sure what the intent is for the line that includes:  vinilo_click_drop.Y

    but Y is not y if that is supposed to be the position property.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2012 9:29 AM   in reply to Marcos Ostos

    You only want to have the MOUSE_MOVE listener assigned when you are dragging.  The MOUSE_MOVE event handler function is where you need to be checking the y property, not the drag function.  THe drag function will only execute that check of the y property once.  The MOUSE_MOVE will check it anytime the mouse is moved.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2012 10:33 AM   in reply to Marcos Ostos

    You do not want to be creating a new sound object every time you start playing.  Create it outisde of your functions and play/stop just the one..

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points