Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

How to use buttons and sounds with flash CS3

This is a discussion on How to use buttons and sounds with flash CS3 within the Tutorials forums, part of the Flash English category; The attachSound method has also been removed. Substantially it worked like the attachMovie method. Assigning a linkage ID to the ...


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

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 27-09-07, 09:36
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
How to use buttons and sounds with flash CS3

The attachSound method has also been removed.
Substantially it worked like the attachMovie method. Assigning a linkage ID to the audio file in library, it would be attached to the stage in runtime and reproduced using the attachSound.
As from now on, to attach a sound and reproduce it we will need to assign a class as an ID to the audio file in library.
With this article, we will see how to assign sounds to a button.
I would like to precise that the classic method of adding a sound directly to each state of the button is still valid. As FlepStudio is all about Flash programming, I feel the need to realize an example only using Actionscript 3.0.
Let's see the example...

I create a FLA and save it as 'sounds.fla', into which I create an instance on stage of a button named 'bottone_btn'.
I import 3 audio files in the library, one for the 'over' event, one for the 'out' event and one for the 'click' event.
I create a Document Class, an AS file saved as 'Main.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Main extends MovieClip
	{
		public function Main()
		{
			initListener();
		}
		
		private function initListener():void
		{
			bottone_btn.addEventListener(MouseEvent.MOUSE_OVER,isOver);
			bottone_btn.addEventListener(MouseEvent.MOUSE_OUT,isOut);
			bottone_btn.addEventListener(MouseEvent.CLICK,isClick);
		}
		
		private function isOver(m:MouseEvent):void
		{
			var sound_over:Over=new Over();
		}
		
		private function isOut(m:MouseEvent):void
		{
			var sound_out:Out=new Out();
		}
		
		private function isClick(m:MouseEvent):void
		{
			var sound_click:Click=new Click();
		}
	}
}
Now, I create 3 classes which names will be used as the linkage ID for each audio file.
Three AS files:

Over.as
Code:
package
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	
	public class Over extends Sound
	{
		private var sound:SoundChannel;
		
		public function Over()
		{
			sound=play();
		}
	}
}
Out.as
Code:
package
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	
	public class Out extends Sound
	{
		private var sound:SoundChannel;
		
		public function Out()
		{
			sound=play();
		}
	}
}
Click.as
Code:
package
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	
	public class Click extends Sound
	{
		private var sound:SoundChannel;
		
		public function Click()
		{
			sound=play();
		}
	}
}
Back to the 'sound.fla', I assign to the choosen audio file for the 'mouse over' event, the ID 'Over'.
I assign to the choosen audio file for the 'mouse out' event, the ID 'Out'.
I assign to the choosen audio file for the 'click' event, the ID 'Click'.

Here is the result:







Let's analise the code:

Main.as Class
Methods

initListener();
I add a listener to the mouse event ( MOUSE_OVER, MOUSE_OUT and CLICK ) which will call the respective methods when the event takes place.
bottone_btn.addEventListener(MouseEvent.MOUSE_OVER ,isOver);
bottone_btn.addEventListener(MouseEvent.MOUSE_OUT, isOut);
bottone_btn.addEventListener(MouseEvent.CLICK,isCl ick);

isOver();
I create an instance of the Over class (which extends the Sound class and which is assigned to an audio file in library)
var sound_over:Over=new Over();

isOut();
I create an instance of the Out class (which extends the Sound class and which is assigned to an audio file in library)
var sound_out:Out=new Out();

isClick();
I create an instance of the Click class (which extends the Sound class and which is assigned to an audio file in library)
var sound_click:Click=new Click();

Over.as, Out.as and Click.as
Properties

an instance of the SoundChannel class
private var sound:SoundChannel;

Inside the respective building function ( public function Over() / Out() / Click() ) I tell SoundChannel to reproduce the sound
sound=play();
NB: this play() command refers to the class in which it is find, so, as the class extends the Sound class, it would be like saying SoundInstance.play();
NB: if you do not create the AS files, Flash will do it for you.

Stay tuned !
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 02-02-08, 01:32
Junior Member
 
Join Date: Feb 2008
Posts: 1
Rep Power: 0
sneskid is on a distinguished road
Re: How to use buttons and sounds with flash CS3

OK, it's nice to see someone can make sense of AS3 and it's backward-downgrade-ness.

How does all the code come together? I followed you steps but you never explained how it comes together to actually work. Can you post your sourse code, thanks.


I swear AS3 was screwed up on purpose just to sell books...
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
buttons Zombie Actionscript 3.0 newbies 14 19-08-08 16:32
after removeChild(); FLV sounds in the external file keeps playing... Vudrok Flash Italiano 4 11-04-08 02:51
buttons dissolvenza michele77 Flash Italiano 8 12-12-07 21:26
Library Sounds with Flash CS3 Flep Tutorials 0 25-09-07 16:36
Sounds da libreria con Flash CS3 Flep Articoli e tutorials 0 21-09-07 08:19


All times are GMT. The time now is 12:29.

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