Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Library Sounds with Flash CS3

This is a discussion on Library Sounds with Flash CS3 within the Tutorials forums, part of the English Forums category; Do you remember attachSound of Actionscript 2.0' We could attach sounds from the library such as MovieClip even though ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  4 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 25-09-07, 16:36
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,446
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!
__________________

 


I recommend: Essential Actionscript 3.0

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

Last edited by Flep; 28-08-08 at 07:05..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
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 16:11
after removeChild(); FLV sounds in the external file keeps playing... Vudrok Flash CS3 generale 4 11-04-08 02:51
How to use buttons and sounds with flash CS3 Flep Tutorials 1 02-02-08 01:32
Attach image from library with Flash CS3 and Actionscript 3.0 Flep Tutorials 0 09-10-07 19:14
Sounds da libreria con Flash CS3 Flep Articoli e tutorials 0 21-09-07 08:19


All times are GMT. The time now is 22:04.


Powered by vBulletin versione 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406