How to put music in individual scenes and have it change when i select a new scene - actionscript-3

Alright I've managed to make an almost web page style thing in flash. I have all the coding to navigate the pages finished, but now I'm having the issue of when I change pages my main page music continues while the other pages music begins. I.,m unsure of how to code this. can anyone help? also I already have click sound effects going for my buttons and they seem to be working fine. Don't know if that helps at all. I'm also using flash cs6 and don't want to go to cc, because the removal of the bone tool.

To stop a sound, you will need to use a SoundChannel object when you tell the sound to play. Then, with the reference to the SoundChannel, you can stop the sound before you exit the section.
// save this reference for later so you can stop the sound when you want..
var mySoundChannel:SoundChannel = mySound.play(); // Get a sound channel.
// Call this later
mySoundChannel.stop();
Without a SoundChannel to control it, the sound object will do its default behavior, which is to play to completion.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundChannel.html

Related

Play continuous sound no matter where you are on the timeline

Is it possible in flash to keep playing the same sound even if I jump to new frames on the timeline but the sound doesn't jump.
So have your sound file in the first frame, select the frame with the sound in it and look at the properties tab, there is a drop-down window that says sound, inside is a sync option, set it to start and loop. That seems to make it play indefinitely.
you can do it grammatically
check this out
https://stackoverflow.com/a/5443790/3623547

Flash AS3 | Looping sound for each animation set

with University done I've finally had time to go back to practicing my Flash work, and I need some help with getting sound to loop and change depending on what part of the animation is playing.
This is also partly a follow up question to an older one I asked, so look here to get more detail as to where I'm coming from if you don't understand! (I've solved that issue, the current one relates to following up on it!)
Flash AS3 | Finishing current animation set before code is executed
The situation is basic: I have two different scenes, one of a man walking and one of a man running. It loops seamlessly of the man walking until you hit a button, where it finishes the animation then starts looping the run animation. Likewise, hitting the button again, finishes the loop before going back to the looping walk animation. The code for the button is below.
import flash.events.MouseEvent;
Next2.addEventListener(MouseEvent.CLICK, Change_2);
function Change_2(event: MouseEvent): void
{
addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(e:Event):void {
if (currentFrame == 30) {
gotoAndPlay(31);
removeEventListener(Event.ENTER_FRAME, enterFrame);
}
}
}
My issue is trying to get a footstep sound to match up with these animations. So while the man is walking, the 'walking footsteps' sound clip will play and loop alongside the animation, and when the button is pushed and the animation moves into the running animation, the 'running footsteps'' sound will play.
Any help on this would be much appreciated. I'm terrible when it comes to anything audio/code based.
Well u can check about soundChannel. soundChannel can stop() and play() ur audio.
So import ur external sound or use internal one. ur code probably seems like this.
var mySound:Sound = new Sound(new URLRequest("YourSoundPath"));
var sc:SoundChannel = new SoundChannel();
//when u wanna play
sc = mySound.play();
//when u wanna stop
sc = mySound.stop();
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundChannel.html
also look this link for more info
If you want to really MATCH the footsteps to the animation (for example, the step sound should play when the player have put his feet down) the only sure way to do that is to place the sound on a different layer where your animation is and set the sound to play as "stream" (if i remember correctly).
Otherwise the animation might be out of sync because of different computer capabilities, current computer or graphics card load etc.
When the sound is set to play as "stream" the flash player is orienting on the sound and drop frames if the graphics are slow to keep the sound and animation in sync

Overlapping sound in as3

Okay, so I am making a game, and I can't figure out how to keep the sound track from overlapping. When the game comes to a lose or win screen, I will click "Play again" and the sound will overlap. I have the sound on it's own layer. Here's what I have so far.
var snd_SolidState = new SolidState();
snd_SolidState.play(0,1);
And on Layer 2 Frame 2 and 3, I have:
snd_SolidState.stop();
snd_SolidState = null;
It says "stop" is not a function. So how would I get the song to stop on the lose/win screens? SolidState is the song.
If the code is related to the click of the "Play again" Button, here is the code for those.
mc_again.addEventListener(MouseEvent.MOUSE_UP,upAgain);
function upAgain(e:MouseEvent){
gotoAndStop(1);
}
Can anyone help?
You need to use a SoundChannel to stop your music.
The variable snd_SolidState is a Sound object, Sound doesn't have a stop method implemented; The Sound.play() however, returns a SoundChannel object which does...
So you can store that (the SoundChannel object returned by snd_SolidState.play()), which you can later use to stop the song.
PS: I recommend you start typing your variables (ie: var snd_SolidState:Sound;), that way you force yourself to know which object type you're dealing with and can find reference for it; in this case Sound.play().

Actionscript to play/pause audio on different buttons

I've created a few buttons in Flash. I'm trying to make it so that if you click one button, the audio starts playing for that button. If you click another button, the active audio stops and the new audio of the button you clicked last start playing.
Any help please?
What you're describing is actually quite easy to do.
First things first, I recommend importing the audio into your Flash project. Alternatively, there is a way to play it directly from an external file. This is beyond the scope of my answer, so if you need help on that, you should post a question specifically covering it.
Assuming you have imported the audio file into your Flash project's library, make an as3 instance of it. (Right click the file in the library, click Properties --> ActionScript [tab] --> [Check] Export for ActionScript & [Enter name in] Class)
Now, create a definition of the sound in your code. (Assuming your two sounds were named "mySound1" and "mySound2" in the Class field of the previous step.)
var mySound1:Sound = new mySound1();
var mySound2:Sound = new mySound2();
Now, define your sound channel.
var mySoundChannel:SoundChannel = new SoundChannel();
There are two alternate ways of stopping one sound and playing another. The first is to create one function that does both every time. The second method is to create two formulas, one for "play" and one for "stop". You will need to decide which method works best for you. I'll use the two-function method below:
function stopSound():void
{
//This stops all sound in the sound channel.
//If there is nothing playing, nothing happens.
mySoundChannel.stop();
}
//In this function, we create an argument that allows us to tell the function
//what sound to we want it to play.
function playSound(soundname:String):void
{
mySoundChannel = this[soundname].play(0, 0);
}
[Note, you can tweak the play() properties to meet your needs, doing things like starting in the middle of the song, or looping it forever. 0,0 starts at the beginning, and doesn't loop. See the documentation for this.]
Now you hook up the event listeners for the buttons. (If you need help with event listeners, read the documentation.)
myButton1.addEventListener(Mouse.CLICK, btn1Click);
myButton2.addEventListener(Mouse.CLICK, btn2Click);
function btn1Click(evt:Event):void
{
stopSound();
playSound(mySound1);
}
function btn2Click(evt:Event):void
{
stopSound();
playSound(mySound2);
}
This should be enough information to get you started. In my game core, I actually have a custom class for dealing with sound playback that gives me the ability to repeat sounds, change volume, and keep sounds from conflicting with each other. I say that to emphasize that you can do quite a bit with the sound class. Do some digging in that documentation for ideas and help.
You may also consider putting a try-catch statement in the playSound function, since it will throw an reference error if you pass a name for a sound that doesn't exist.

How to ENTIRELY remove an flvplayback from stage when going to another flvplayback on another frame?

I'm trying to create an EXE projector using flash 5.5 AS3 where I have a few videos (FLVs) to show (their location is right next to the exe file in the same directory) - each load in a different frame, and all of those videos should also have a full screen option to them. Those are original videos that people WILL want to watch in full screen. It's essential for the experience...
The problem(s) I currently have (after fixing the sound that didn't stop after going to a different video) are hard to describe, but I'll try really hard.
Ok, so when I click the full screen button on a video and watch it in full screen, I will eventually want to exit the full screen, so I click on the same icon at the bottom to exit full screen (or ESC button, it's the same) and then click the navigation button to go to the SECOND FLV's frame to watch the other video. After watching the second video in full screen and then exiting full screen, flash takes me to the FIRST video's frame and that is a big problem. Also, now the button that takes me BACK to the second video's frame won't work. It's like flash is stuck.
I use the Components --> FLVPlayback 2.5 from the componant menu (I don't really know AS3 programming) and I fix its properties in the component parameters.
Also, I don't think that any of the followings are the reason for the bug, but I use these 3 scripts to stop all sound when navigating away from one frame (with an FLVplayback) to
another frame with another FLVplayback:
MyFLV.stop();
SoundMixer.stopAll();
MyFLV.addEventListener(Event.REMOVED_FROM_STAGE,xyz);
function xyz(e:Event):void{
MyFLV.stop();
}
I've found these online where people asked help for the sound bug I described.
The third script was suppose to remove the FLVplayback from the stage before going to another frame, but it works only when NOT GETTING INTO FULL SCREEN. I need something that will COMPLETELY remove the previous video from the stage so after exiting the SECOND viewed video, flash won't take me to a video that from some reason is still in its memory. I have something like 30 videos in my project and I need to remove each and every one of them off of the stage before navigating to the next frame to open a new FLVPlayback.
I tried to add a link to a demo I made with the problem so you can look at it, but it triggered a "oops, something went wrong" error, probably anti spam mechanism...
I would recommend using only one frame and only one flvplayback instance. Otherwise you have to deal with weird bugs like the one you are getting (usually caused by misplaced or forgotten code). Of course, using only one frame requires using more code, but with the number of hard-fixes it looks like you were making for the bugs, you may end up with less code.
Don't worry, I'll walk you through everything!
Reasons to use code (as opposed to multiple frames):
Easier to keep track of:
Know where all your code is so you can easily find and fix any problems.
Make changes more easily
You want to switch an existing video? edit a file reference and you are done.
Want to add a video? no more dragging a new flvplayback instance onto a new frame just add some very simple code and a button and you're done.
More customization
Reasons to use multiple frames (and multiple flvplayback instances):
easier to place visually
Some people find it easier when they have an actual movieclip that they can visually place on the stage
Less code
Here we go:
//import flv library
import fl.video.*;
This allows you to use ActionScript to manipulate the flv player
//video playback code-----------------------//
var myVideo:FLVPlayback = new FLVPlayback();
this creates an instance of FLVPlayback called myVideo (referenced from now on in the code as myVideo)
this next chunk shows many of the customizable features of the flv player. It is not necessary to include them.
//places the video player on stage at x,y
myVideo.x = 115;
myVideo.y = -10;
//uses SkinOverPlayFullscreen.swf for controls
myVideo.skin = "SkinOverPlayFullscreen.swf";
//color of controls
myVideo.skinBackgroundColor = 0x333333;
//hide controls and time it takes controls to fade and reappear (milliseconds)
myVideo.skinAutoHide=true;
myVideo.skinFadeTime=300;
//add the player to the stage
addChild(myVideo);
And now comes the important part. I have made buttons and added them to the stage. I gave each of the buttons a different instance name (box1_btn, box2_btn, and box3_btn). When someone clicks on a button, an "event" will occur.
//button listener code-------------------------//
//when button 1 is clicked throw button 1 event
box1_btn.addEventListener(MouseEvent.CLICK, clicked1);
//when button 2 is clicked throw button 2 event
box2_btn.addEventListener(MouseEvent.CLICK, clicked2);
//when button 3 is clicked throw button 3 event
box3_btn.addEventListener(MouseEvent.CLICK, clicked3);
//play different videos for different buttons---------//
//when button 1 event is thrown
function clicked1($e:MouseEvent):void
{
//play video 1.flv
myVideo.source = "1.flv";
}
//when button 2 event is thrown
function clicked2($e:MouseEvent):void
{
//play video 2.flv
myVideo.source = "2.flv";
}
//when button 3 event is thrown
function clicked3($e:MouseEvent):void
{
//play video 3.flv
myVideo.source = "3.flv";
}
This code will not have any sounds that keep playing because two videos cannot play at the same time in one instance of the player. Nor will it have any mess-ups when you come out of fullscreen because there is only one frame for the video to go back to.
Some possible problems you may run into:
It doesn't work at all:
Make sure you have added an instance of FLVPlayback to the library by adding an instance to the stage from the components menu (window>>components or ctrl+F7) and then deleting it from the stage (it should still appear in the library).
The playback buttons I want aren't showing up:
There is a great explanation of how to use As3 to manipulate FLVPlayback here:
http://www.republicofcode.com/tutorials/flash/as3flvplayback/
find the section about "Applying a Skin to the FLVPlayback Component" and follow it to use an adobe playback skin. If you want to make your own unique skin I would recommend opening and editing one of the pre-made skins. I found mine in
C:\Program Files (x86)\Adobe\Adobe Flash CS6\Common\Configuration\FLVPlayback Skins\FLA\ActionScript 3.0
I hope this helps!
Below would be simplest way to unload the FLVPlayback
removeChild(MyFLV);
it works fine for me
flvPlayBack.stop();
removeChild(flvPlayBack);
stops the sound and removes the playback.