#1 (permalink)  
Old 25-09-07, 15:36
Administrator
Living At The FlepStudio!
 
Join Date: Jul 2007
Location: Cesenatico
Posts: 4,917
Rep Power: 6
Flep is on a distinguished road
Library Sounds with Flash CS3

Do you remember attachSound of Actionscript 2.0'
We could attach sounds from the library such as MovieClip even though they were mp3.
With Actionscript 3.0, the method attachSound has been removed (what a surprise you could think'), but no worry, it is still easy to use in runtime an mp3 file placed in our FLA's library.
After having seen how to assign a class to sounds in library , we will now see how to realise the same thing but without the need to create a class for each sound.  Very useful if you would find yourself with hundreds of sounds in library.
Let's see' I create a FLA and save it as 'main.fla'.
I import 10 different mp3 files to the library. In these examples, I use vocal sounds and I recommend you to use short sounds for testing purpose such as sound effects for buttons.
For each one of them, I assign a class the following way:
- right click the file in library and selection the property 'linkage'
- in the new opened window, select the option 'export for Actionscript' and in the text field Class, add 'Sound0'
- click OK. Flash will say that the assigned class has not been found and it will create one if accepted. We click OK once again.
We will go through the same process for all the 9 remaining sounds, assigning the class name Sound1, Sound2, Sound3'etc'etc

I drag on stage 10 instances of a button and assign them instance names such as button_0_btn, button_1_btn, button_2_btn'etc'etc.

I now create the Document Class, an AS file saved as 'Main.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.utils.getDefinitionByName;
	import flash.media.Sound;
	import flash.events.MouseEvent;
	
	public class Main extends MovieClip
	{
		public function Main()
		{
			initLabels();
		}
		
		private function initLabels():void
		{
			for( var i:int=0;i < 10;i++)
			{
				var b:*=this.getChildAt(i);
				var button:Button=b;
				button.label='Sound'+i;
				button.addEventListener(MouseEvent.CLICK,playSound);
			}
		}
		
		private function playSound(m:MouseEvent):void
		{
			var className:Class=getDefinitionByName(m.currentTarget.label)as Class;
			var sound:Sound=new className();
			sound.play();
		}
	}
}
The result:





Lets' analyse the code

Methods
initLabels();
using a cycle, I retrieve all the buttons placed on stage, assign a label name and assign a listener to the event CLICK which will call the method playSound
for( var i:int=0;i < 10;i++)
{
var b:*=this.getChildAt(i);
var button:Button=b;
button.label='Sound'+i;
button.addEventListener(MouseEvent.CLICK,playSound );
}

playSound();
using the getDefinitionByName Class I impost the label name of the button (m.currentTarget which caused the event, meaning the button clicked) as the Class name to be called
var className:Class=getDefinitionByName(m.currentTarge t.label) as Class;
I create an instance of that class which extends the Sound Class as we added it to the linkage
var sound:className=new className();
I start the sound
sound.play();


Stay tuned!
__________________
_________________________________________
VIDEO CORSI ACTIONSCRIPT 3.0 creati da FlepStudio
I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread please !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 06:05.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
sponsor links
Flashmint flash templates FlippingBook-PDF publisher Flash Media Server Hosting
sponsor links
Reply

Bookmarks

Tags
cs3, flash, library, sounds

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Flash PHP Create Library book system for Flash PHP clonez Flash CS3 | PHP | mySQL 0 21-10-08 15:11
after removeChild(); FLV sounds in the external file keeps playing... Vudrok Flash Italiano 4 11-04-08 01:51
How to use buttons and sounds with flash CS3 Flep Tutorials 1 02-02-08 00:32
Attach image from library with Flash CS3 and Actionscript 3.0 Flep Tutorials 0 09-10-07 18:14
Sounds da libreria con Flash CS3 Flep Articoli e tutorials 0 21-09-07 07:19



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
vBulletin Skin developed by: vBStyles.com
FlepStudio 2007-2009