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.
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.
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.
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.
Thank you again Ned,
I really like that you teach me how to do it rather than give me all the code change so I just have to copy and paste. Thanks.
I have done some changes, but I´m sure I´m doing something wrong, probably I´m not understanding you properly, english is not my mother tonge.
With the changes I have done, as the object gets the higger possition music starts, but everytime I move the mouse around the object music keeps starting over the previous music.
Also, when I move the drag the object down, music doesn´t stop.
What I´m doing wrong? As you can see I´m beggining with AS3, and even though I love it, it drives me mad.
Here is my coding;
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)
if (vinilo_click_drop.y ==-56)
{
var s:Sound = new Sound(new URLRequest("http://www.helpexamples.com/flash/sound/song1.mp3"));
fl_SC = s.play();
}
else if ((vinilo_click_drop.y ==-56) && (fl_ToPlay))
{
fl_SC.stop ();
}
fl_ToPlay = !fl_ToPlay;
}
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));
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
vinilo_click_drop.stopDrag();
}
Honestly thank you very much for all your help.
Right, I have moved forward another step, but still is not working properly.
Now when I load the video, music starts automatically and as soon as I do mouse over on the object, music stops and It not playing again.
Why is doing that? Why the conditional is not working?
Here is the code again.
var fl_SC:SoundChannel;
var fl_ToPlay:Boolean = true;
var s:Sound = new Sound(new URLRequest("http://www.helpexamples.com/flash/sound/song1.mp3"));
var music = s.play();
vinilo_click_drop.addEventListener (MouseEvent.MOUSE_MOVE, move);
function move (event:MouseEvent):void
{
trace (event)
if (vinilo_click_drop.y ==-56)
{
music.play ();
}
else if (vinilo_click_drop.y !=-56)
{
music.stop ();
}
}
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));
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
vinilo_click_drop.stopDrag();
}
Thanks a lot
North America
Europe, Middle East and Africa
Asia Pacific