Skip to main content
Participant
June 8, 2012
Question

Play music after an object drags

  • June 8, 2012
  • 1 reply
  • 1359 views

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.

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
June 8, 2012

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.

Participant
June 8, 2012

Hi Ned,

Thank you very much for your answer, I have change Y for y, I can´t belive I didn´t see it.

I have also add a MOUSE_MOVE listener, and everything seems to start running properly, but still doesn´t work as I want.

Right now ,when I click in the circle, and this is at y=-56 music starts. But I want to avoid that click and that the music start as soon as the object reach that position, and oposite when the object reach it´s original position.

Also everytime I click in that position, music starts again over the previous music, Why the boolean is not working?

I paste my coding again.

import flash.media.Sound;

import flash.media.SoundChannel;

import flash.media.SoundMixer;

import flash.events.MouseEvent;

/* 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_MOVE, move);

function move (event:MouseEvent):void

{

          trace (event)

}

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 !=-56)

                    {

                              fl_SC.stop ();

                    }

 

          else if ((vinilo_click_drop.y ==-56) && (fl_ToPlay))

                    

          {

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

                    fl_SC = s.play();

          }

          fl_ToPlay = !fl_ToPlay;

}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);

function fl_ReleaseToDrop(event:MouseEvent):void

{

          vinilo_click_drop.stopDrag();

}

Thank you very much.

Ned Murphy
Legend
June 8, 2012

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.