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

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);
}
}

Related

How to stop multiple Movieclips at a time by play/pause button in AS3

I have 4 Movieclips in timeline.these movieclips have inner animation. they are classically tween in timeline. I have to stop and play these four movieclips at a time by play/pause button. Noted that, I have used the following code for that purposes.
It can stop only one MovieClip which is in the upper layer, and the rest 3 Movieclips can't be stopped by play/ pause button. (I have tried all movie clips are in same instance name and also tried with different instance).
Here is my code:
import flash.events.MouseEvent;
var Playing: Boolean = false;
var lastPosition: Number = 0;
play_btn.addEventListener(MouseEvent.CLICK, onPlayClick);
pause_btn.addEventListener(MouseEvent.CLICK, onPauseClick);
function onPlayClick(event: MouseEvent):void {
if (Playing == false) {
Playing = true;
first_sl.play();
this.play();
}
}
function onPauseClick(event: MouseEvent):void {
Playing = false;
lastPosition = this.position;
first_sl.stop();
this.stop();
}
You can try like when you are stopping main timeline with stage.stop(); also use mc.stop(); ect. and when u play them use stage.play(); also mc.play();
import flash.events.MouseEvent;
var Playing: Boolean = false;
var lastPosition: Number = 0;
play_btn.addEventListener(MouseEvent.CLICK, onPlayClick);
pause_btn.addEventListener(MouseEvent.CLICK, onPauseClick);
function onPlayClick(event: MouseEvent):void {
if (Playing == false) {
Playing = true;
mc.play();
mc2.play();// other 2 mc's too
stage.play();
}
}
function onPauseClick(event: MouseEvent):void {
Playing = false;
lastPosition = this.position;
mc.stop();
mc2.stop(); // other mc's
stage.stop();
}
And if you are using last position for timeline also use it for mc's so it will be sync..
hope this on help tho :)
To stop the main timeline you can use stop() or MovieClip(root).stop() and to play it again, you can use play() or MovieClip(root).play() :
var playing: Boolean = false;
btn_play.addEventListener(MouseEvent.CLICK, onPlayClick);
btn_pause.addEventListener(MouseEvent.CLICK, onPauseClick);
function onPlayClick(event: MouseEvent):void
{
if (!playing) {
play(); // you can also use : MovieClip(root).play();
mc1.play();
mc2.play();
mc3.play();
mc4.play();
playing = true;
}
}
function onPauseClick(event: MouseEvent):void
{
if (playing) {
stop(); // you can also use : MovieClip(root).stop();
mc1.stop();
mc2.stop();
mc3.stop();
mc4.stop();
playing = false;
}
}
Hope that can help.

Using a variable to allow only one video to play at a time

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.

Flash CS5.5: Error 1046. I can't find another way to do it

Essentially, I have 6 balls, and when my showBalls() function runs, I want each ball (each are a movieclip) to run its animation and tween to the correct place. I want them to do it in order though. So 1 ball tweens, then the next, then the next and so on.
I've used a bunch of if statements because switch wouldn't work, but now it just keeps throwing me Error 1046 and telling me that "Event" was not found or not compile-time constant.
I can't see anyway around this and it's frustrating me.
Here's some code for you all. It's probably messy as hell and there'll be a much easier way to do this. But I'm pretty new to AS3 so I can't see any other way.
I have tried to find the answer on here and somebody told me to un-nest the functions. So I did. I've not had a problem with one nest but I tried two here and it didn't work. So I un-nested, but to no avail. If there is a way around this, i'd be grateful for guidance.
So yeah, first ball shown, tweens, once tween reaches final frame, it stops and the number associated with it shows. Repeat for following 6 balls.
function showNumbers()
{
var count:int = 0;
var showTimer:Timer = null;
showTimer = new Timer(3125,8);
showTimer.start();
showTimer.addEventListener(TimerEvent.TIMER, showBalls);
function showBalls(Event:TimerEvent)
{
ball1.addEventListener(Event.ENTER_FRAME, ball1stop);
ball2.addEventListener(Event.ENTER_FRAME, ball2stop);
ball3.addEventListener(Event.ENTER_FRAME, ball3stop);
ball4.addEventListener(Event.ENTER_FRAME, ball4stop);
ball5.addEventListener(Event.ENTER_FRAME, ball5stop);
ball5.addEventListener(Event.ENTER_FRAME, ball6stop);
bonusBall.addEventListener(Event.ENTER_FRAME, bonusBallstop);
function ball1stop(event:Event):void
{
if (currentFrame == stopFrame1)
{
ball1.stop();
programNumber1.text = drawnArray[0];
ball1.removeEventListener(Event.ENTER_FRAME, ball1stop);
}
}
function ball2stop(event:Event)
{
if (currentFrame == stopFrame2)
{
ball2.stop();
programNumber2.text = drawnArray[1];
ball2.removeEventListener(Event.ENTER_FRAME, ball2stop);
}
}
function ball3stop(event:Event)
{
if (currentFrame == stopFrame3)
{
ball3.stop();
programNumber3.text = drawnArray[2];
ball3.removeEventListener(Event.ENTER_FRAME, ball3stop);
}
}
function ball4stop(event:Event)
{
if (currentFrame == stopFrame4)
{
ball4.stop();
programNumber4.text = drawnArray[3];
ball4.removeEventListener(Event.ENTER_FRAME, ball4stop);
}
}
function ball5stop(event:Event)
{
if (currentFrame == stopFrame5)
{
ball5.stop();
programNumber5.text = drawnArray[4];
ball5.removeEventListener(Event.ENTER_FRAME, ball5stop);
}
}
function bonusBallstop(event:Event)
{
if (currentFrame == stopFrame7)
{
bonusBall.stop();
programBonusNumber.text = bonusArray[0];
bonusBall.removeEventListener(Event.ENTER_FRAME, bonusBallstop);
showTimer.stop();
fadeAndSort();
}
}
if (count==0)
{
ball1.visible = true;
ball1.play();
var stopFrame1:int = 75;
ball1stop();
}
else if (count==1)
{
ball2.visible = true;
ball2.addEventListener(Event.ENTER_FRAME, ball2stop);
ball2.play();
var stopFrame2:int = 75;
ball2stop();
}
else if (count==2)
{
ball3.visible = true;
ball3.addEventListener(Event.ENTER_FRAME, ball3stop);
ball3.play();
var stopFrame3:int = 75;
ball3stop();
}
else if (count==3)
{
ball4.visible = true;
ball4.addEventListener(Event.ENTER_FRAME, ball4stop);
ball4.play();
var stopFrame4:int = 75;
ball4stop();
}
else if (count==4)
{
ball5.visible = true;
ball5.addEventListener(Event.ENTER_FRAME, ball5stop);
ball5.play();
var stopFrame5:int = 75;
}
else if (count==5)
{
ball6.visible = true;
ball6.addEventListener(Event.ENTER_FRAME, ball6stop);
ball6.play();
var stopFrame6:int = 75;
ball6stop();
}
else if (count==6)
{
bonusBall.visible = true;
bonusBall.addEventListener(Event.ENTER_FRAME, bonusBallstop);
bonusBall.play();
var stopFrame7:int = 75;
bonusballstop();
}
}
count++;
mainArray[0] = userNumber1.text;
mainArray[1] = userNumber2.text;
mainArray[2] = userNumber3.text;
mainArray[3] = userNumber4.text;
mainArray[4] = userNumber5.text;
mainArray[5] = userNumber6.text;
}
I bet this is the event he has problem with:
event.ENTER_FRAME
and should be Event.ENTER_FRAME
also I've found this in your "code"
function showBalls(Event:TimerEvent)
You need to put this line at the top of your file so flash knows where to look for the Event Class
You'll also need one for Timer, TimerEvent
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;

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.