Using a variable to allow only one video to play at a time - actionscript-3

I recently created a project that has four buttons. Each button adds an FLV player to the stage and plays a video. During testing, I noticed that clicking another button before the first function is finished loading the movie, TWO movies will play.
Is there a more efficient way of handling this? I feel like there is a lot of redundant code here, is there a better way to do the following:
Button One Pressed
Load Video One
Ignore all other event listeners until the movie ends, or the user dismisses the video (Clicks to dismiss)
I initially though it would be a good idea to create a two functions, one that removes all event listeners, and another that adds all the event listeners. When a button is pressed, before it loads the FLVPlayback, remove all event listeners. When the movie is COMPLETE, add all the listeners back. That seems taxing, and very inefficient.
I wrote the following code as an example of what I'm trying to do. (This outputs the same string "One Can play" every time though).
var canLaunchOne:Boolean;
var canLaunchTwo:Boolean;
var canLaunchThree:Boolean;
var canLaunchFour:Boolean;
buttonMCOne.addEventListener(MouseEvent.MOUSE_DOWN, playMovieOne);
buttonMCTwo.addEventListener(MouseEvent.MOUSE_DOWN, playMovieTwo);
buttonMCThree.addEventListener(MouseEvent.MOUSE_DOWN, playMovieThree);
buttonMCFour.addEventListener(MouseEvent.MOUSE_DOWN, playMovieFour);
trace(canLaunchTwo);
function playMovieOne(event:Event):void {
canLaunchOne = true;
canLaunchTwo = false;
canLaunchThree = false;
canLaunchFour = false;
trace(canLaunchOne);
playTheRightVideo();
}
function playMovieTwo(event:Event):void {
canLaunchOne = false;
canLaunchTwo = true;
canLaunchThree = false;
canLaunchFour = false;
playTheRightVideo();
}
function playMovieThree(event:Event):void {
canLaunchOne = false;
canLaunchTwo = false;
canLaunchThree = true;
canLaunchFour = false;
playTheRightVideo();
}
function playMovieFour(event:Event):void {
canLaunchOne = false;
canLaunchTwo = false;
canLaunchThree = false;
canLaunchFour = true;
playTheRightVideo();
}
function playTheRightVideo():void {
if(canLaunchOne = true){
trace("One Can Play"); // do all that video stuff for video one
} else if(canLaunchTwo = true){
trace("Two Can Play"); // do all that video stuff for video one
} else if(canLaunchThree = true){
trace("Three Can Play"); // do all that video stuff for video one
} else {
trace("Four Can Play");
}
}
I tried this code, and I got it "working", but every function will always set canLaunchMovie to true...
import flash.events.Event;
var canLaunchMovie:Boolean;
buttonMCOne.addEventListener(MouseEvent.MOUSE_DOWN, playMovieOne);
buttonMCTwo.addEventListener(MouseEvent.MOUSE_DOWN, playMovieTwo);
buttonMCThree.addEventListener(MouseEvent.MOUSE_DOWN, playMovieThree);
buttonMCFour.addEventListener(MouseEvent.MOUSE_DOWN, playMovieFour);
function playMovieOne(e:Event):void {
if(canLaunchMovie = true){
this.canLaunchMovie = false;
trace("One Can Play"); // do all that video stuff for video one
} else {
trace("I can't do that(play movie one) Dave");
}
}
function playMovieTwo(e:Event):void {
if(canLaunchMovie = true){
this.canLaunchMovie = false;
trace("Two Can Play"); // do all that video stuff for video one
} else {
trace("I can't do that(play movie two) Dave");
}
}
function playMovieThree(e:Event):void {
if(canLaunchMovie = true){
this.canLaunchMovie = false;
trace("Three Can Play"); // do all that video stuff for video one
} else {
trace("I can't do that(play movie three) Dave");
}
}
function playMovieFour(e:Event):void {
if(canLaunchMovie = true){
this.canLaunchMovie = false;
trace("Four Can Play"); // do all that video stuff for video one
} else {
trace("I can't do that(play movie four) Dave");
}
}

You don't need to have more than one flvplayback.
You only need to change the source of your flvplayback.

Related

How can I press keyboard to play vdo only once

I'm here again for your help :{ I have a question which I tried to google it but can't find the answer. Well,..May be there is an answer but I just can't make it works? I'm in a process of learning AS3 so let's say I'm still new here.
What I'm doing is making a keyboatd to respond with the vdo files I have. It is a very simple idea as press-n-play. Each keys has their vdos to play and if you press another button while the first one is still pressing, it'll play another vdo of its key. I have make this as boolean with function of keydown and keyup like this:
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.net.NetStream;
import flash.net.NetConnection;
import flash.media.Video;
var isLeft:Boolean = false;
var isRight:Boolean = false;
var video;
var nc;
var ns;
stage.addEventListener(KeyboardEvent.KEY_DOWN,onDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onUP);
this.addEventListener(Event.ENTER_FRAME,playVid);
nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.client = this;
video = new Video(550,400);
addChild(video);
video.attachNetStream(ns);
function onDown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37 :
//ns.play(TomAndJerry.flv);
isLeft=true;
break;
case 39 :
//ns.play(westler.flv);
isRight = true;
break;
}
}
function onUP(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37 :
isLeft = false;
break;
case 39 :
isRight = false;
break;
}
}
function playVid(e:Event):void
{
if (isLeft)
{
trace(kk);
ns.play(westler.flv);
isLeft = false;
}
else if (isRight)
{
trace(PP);
ns.play(TomAndJerry.flv);
//isRight = false;
}
}
I have tried making a keydown function without using any boolean or those true of false things to just play a vdo. It worked but, I still have the same problem that I can't find a solution which is ....
When you hold down the keyboard button the vdo will keep start at the beginning.
All I want is to play the vdo even the key is press down. If the vdo ends then play again as loop but if the key is up the vdo will play until it ends.
And if there are more than one button are holding down just play the vdo of the lastest pressed button.
T-T"
Thanks.
Ps. I have tried removeEventListener but, it made every buttons' function gone.
your playvid function is called each frame so i think it's normal your video don't start, I think you could try to change your code as follow:
// add net status handler event to check the end of the video
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
// remove this line
//this.addEventListener(Event.ENTER_FRAME,playVid);
/** a key is pressed **/
function onDown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.LEFT:
// start the video just if the video don't play
if(!isLeft) ns.play("TomAndJerry.flv");
// video left is playing
isLeft = true;
// video right isn't playing
isRight = false;
break;
case Keyboard.RIGHT:
// start the video just if the video don't play
if(!isRight) ns.play("westler.flv");
// video rightis playing
isRight = true;
// video left isn't playing
isLeft = false;
break;
}
}
/** a key is released **/
function onUP(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.LEFT:
isLeft = false;
break;
case Keyboard.RIGHT:
isRight = false;
break;
}
}
/** net status change, verify if we reach the end of the video **/
function netStatusHandler(e:NetStatusEvent):void
{
// when netStatus code is NetStream.Play.Stop the video is complete
if (e.info.code == "NetStream.Play.Stop")
{
// right key is still pressed we loop the video
if( isRight ) ns.play("westler.flv");
// left key is still pressed we loop the video
else if( isLeft ) ns.play("TomAndJerry.flv");
}
}
I hope this will help you :)

AS3 for transfering pauseposition of MC1 to MC2

Hi I'm quite a new on flash and need some code. I have 3 MCs say MC1, MC2 and MC3. I will have 3 dedicated buttons that will pause the music of one MC and transfer the frame position to another MC and start playing. so say for example if MC2 is playing and I press the MC3 button I would like it to take the pause position of MC2 (not MC1) and continue playing from that frame on MC3 as well as switching the visuals from MC2 to MC3. it's a Multilingual app and all 3 MC's have the same frame length. in other words I would like to switch between languages.
Thanks in advance, any help would be great.
EDIT: up till now I have
mtlyrvult.stop();
itlyrvult.stop();
engvult.addEventListener(MouseEvent.CLICK, playMC1);
function playMC1(e:MouseEvent):void {
itlyrvult.stop();
enlyrvult.gotoAndPlay(itlyrvult.currentFrame);
itlyrvult.gotoAndStop(1); //frame one is empty
engvult.mouseEnabled = false;
itvult.mouseEnabled = true;
}
itvult.addEventListener(MouseEvent.CLICK, playMC2);
function playMC2(e:MouseEvent):void {
enlyrvult.stop();
itlyrvult.gotoAndPlay(enlyrvult.currentFrame);
enlyrvult.gotoAndStop(1); //frame one is empty
itvult.mouseEnabled = false;
engvult.mouseEnabled = true;
}
This switches from one language to another. Now my client gave me another language. mtlyrvult. And I don't know how AS3 will recognise which mc is playing to take the the pauseposition/currentframe from it.
I stick to your code (no class, method, members, addChild, ...). I didn't try the final draft with Flash or SDK.
I assume that you have:
3 MovieClip objects : enlyrvult, itlyrvult, mtlyrvult;
3 InteractiveObject (SimpleButton, MovieClip, ...) objects : engvult, itvult, mtvult;
and that enlyrvult is playing when we begin here (if not: enlyrvult.play(); or enlyrvult.stop();).
itlyrvult.stop(); // or itlyrvult.gotoAndStop(1);
mtlyrvult.stop(); // or mtlyrvult.gotoAndStop(1);
engvult.addEventListener(MouseEvent.CLICK, playMC1);
itvult.addEventListener(MouseEvent.CLICK, playMC2);
mtvult.addEventListener(MouseEvent.CLICK, playMC3);
// play enlyrvult
function playMC1(e:MouseEvent):void {
// stop them
itlyrvult.stop();
mtlyrvult.stop();
// play me
enlyrvult.gotoAndPlay(mtlyrvult.currentFrame);
// hide them
itlyrvult.gotoAndStop(1); //frame one is empty
mtlyrvult.gotoAndStop(1); //frame one is empty
// My trigger is out, theirs are fine
engvult.mouseEnabled = false;
itvult.mouseEnabled = true;
mtvult.mouseEnabled = true;
}
// play itlyrvult
function playMC2(e:MouseEvent):void {
// stop them
enlyrvult.stop();
mtlyrvult.stop();
// play me
itlyrvult.gotoAndPlay(enlyrvult.currentFrame);
// hide them
enlyrvult.gotoAndStop(1); //frame one is empty
mtlyrvult.gotoAndStop(1); //frame one is empty
// My trigger is out, theirs are fine
itvult.mouseEnabled = false;
engvult.mouseEnabled = true;
mtvult.mouseEnabled = true;
}
// play mtlyrvult
function playMC3(e:MouseEvent):void {
// stop them
enlyrvult.stop();
itlyrvult.stop();
// play me
mtlyrvult.gotoAndPlay(itlyrvult.currentFrame);
// hide them
enlyrvult.gotoAndStop(1); //frame one is empty
itlyrvult.gotoAndStop(1); //frame one is empty
// My trigger is out, theirs are fine
mtvult.mouseEnabled = false;
engvult.mouseEnabled = true;
itvult.mouseEnabled = true;
}
OR, for as much (300?) as you want...
// add to your import:
import flash.utils.Dictionary;
// in your const/var section
const STARTING_FRAME:int = 1;
var dict = new Dictionary(); // mapping and memory
var currentTrack:MovieClip; // we will know who's last
initAll();
playTrack(enlyrvult, STARTING_FRAME, engvult);
function clickHandler(e:MouseEvent):void {
var playheadFrame:int = currentTrack.currentFrame; // we remember position
var trigger:InteractiveObject = (e.currentTarget as InteractiveObject); // who shot me ?
var nextTrack:MovieClip = (dict[trigger] as MovieClip); // who's next ?
resetAll(); // and again.. (http://en.wikipedia.org/wiki/Sisyphus)
playTrack(nextTrack, playheadFrame, trigger);
}
function playTrack(mc:MovieClip, fram:int, iObj:InteractiveObject):void {
currentTrack = mc;
currentTrack.gotoAndPlay(fram);
iObj.mouseEnabled = false;
}
function resetAll():void {
for (var key:InteractiveObject in dict) { key.mouseEnabled = true; }
for each (var value:MovieClip in dict) { value.gotoAndStop(1); } // diff-> each
}
function initAll():void {
dict[engvult] = enlyrvult;
dict[itvult] = itlyrvult;
dict[mtvult] = mtlyrvult;
//dict[avult] = alyrvult; //<- new one like this: dict[trigger]=lyrMC; add as much as you can!
for (var key:InteractiveObject in dict) {
key.addEventListener(MouseEvent.CLICK, clickHandler);
}
resetAll();
}

AS3: How do I play a sound when the currentframe ==?

My code below unfortunately results in the sound repeating at the framerate of the movie. I just need a simple "play a sound at a specific frame" function but can't figure this one out. Thanks for your help.
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2)
{
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
}
}
This seems about right. Are you sure the naturepage.ocean_btns movieclip is not stopped at the second frame or looping very few frames over and over?
Another option is to use a flag to see if you already played the sound:
var alreadyPlayedSound:Boolean = false;
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2 && !alreadyPlayedSound)
{
alreadyPlayedSound = true;
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
}
}
Or to remove the event listener once you played the sound:
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2)
{
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
removeEventListener(Event.ENTER_FRAME,soundFunction);
}
}

Play a sound from library with the space bar in AS3

I've been trying to come up with a simple code that plays a sound "jump" from the library each time the spacebar is pressed, but no luck. Everything I try turns my fla/swf into a strobe light.
var spacebarDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, _keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, _keyHandler);
function _keyHandler(e:KeyboardEvent):void
{
if(e.keyCode == 32)
{
switch(e.type)
{
case KeyboardEvent.KEY_DOWN:
if(!spacebarDown)
{
spacebarDown = true;
// Play Sound.
var sfx:YourSound = new YourSound();
sfx.play();
}
break;
case KeyboardEvent.KEY_UP: spacebarDown = false; break;
}
}
}

music playing over and over in Actionscript 3

Greeting,
I I developed a website using flash with Actionscript 3.
I included a music as a background for the website and the music will loaded when the site loaded.
but the problem that when I click buttons to move between pages(frames) then go and click button_01 the music will play again so I will have music playing more than one time in the background
and the sound_btn will not work any more so even I click sound_btn the music will not stop.
the code I'm using is listed down.
Please advice me what I should modify to not allow the music play more than one time in the background while moving from one page(frame) to another.
Regards,
stop();
//number that is redefined when the pause button is hit
var pausePoint:Number = 0.00;
//a true or false value that is used to check whether the sound is currently playing
var isPlaying:Boolean;
//think of the soundchannel as a speaker system and the sound as an mp3 player
var soundChannel:SoundChannel = new SoundChannel();
var sound:Sound = new Sound(new URLRequest("music.mp3"));
//you should set the xstop and xplay values to match the instance names of your stop button and play/pause buttons
//mute_btn.addEventListener(MouseEvent.CLICK, clickStop);
sound_btn.addEventListener(MouseEvent.CLICK, clickPlayPause);
soundChannel = sound.play();
isPlaying = true;
myVideo.stop();
function clickPlayPause(evt:MouseEvent) {
if (isPlaying) {
pausePoint = soundChannel.position;
soundChannel.stop();
isPlaying = false;
} else {
soundChannel = sound.play(pausePoint);
isPlaying = true;
}
}
button_01.addEventListener(MouseEvent.CLICK, onClick1);
button_02.addEventListener(MouseEvent.CLICK, onClick2);
button_03.addEventListener(MouseEvent.CLICK, onClick3);
button_04.addEventListener(MouseEvent.CLICK, onClick4);
button_05.addEventListener(MouseEvent.CLICK, onClick5);
button_06.addEventListener(MouseEvent.CLICK, onClick6);
function onClick1(e:MouseEvent):void
{
gotoAndStop(1);
}
function onClick2(event:MouseEvent):void
{
gotoAndStop(2);
}
function onClick3(event:MouseEvent):void
{
gotoAndStop(3);
}
function onClick4(event:MouseEvent):void
{
gotoAndStop(4);
}
function onClick5(event:MouseEvent):void
{
gotoAndStop(5);
}
function onClick6(event:MouseEvent):void
{
gotoAndStop(6);
}
The problem is your code for the sound is initialized on the frame that you send the timeline to when clicking button_01. It will reinitialize each time you do that. Try initializing your sound code one frame earlier so that you do not land on that frame ever again once your page loads.
You also might find that wrapping your pages into movieclips, and using visible = true/false to change sections might be a better approach than advancing the timeline to change sections. That method would not result in the sound code reinitializing each time you changed sections. something like this:
function onClick1(e:MouseEvent):void
{
hideAll();
section_01.visible = true;
}
function onClick2(event:MouseEvent):void
{
hideAll();
section_02.visible = true;
}
function onClick3(event:MouseEvent):void
{
hideAll();
section_03.visible = true;
}
function onClick4(event:MouseEvent):void
{
hideAll();
section_04.visible = true;
}
function onClick5(event:MouseEvent):void
{
hideAll();
section_05.visible = true;
}
function onClick6(event:MouseEvent):void
{
hideAll();
section_06.visible = true;
}
function hideAll():void
{
section_01.visible = false;
section_02.visible = false;
section_03.visible = false;
section_04.visible = false;
section_05.visible = false;
section_06.visible = false;
}
If you wanted tweened transitions you could use a tweening class to handle the transitions by tweening the current section out in the hide function and then tweening the next section in in its respective onCLick function.