Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 2 of 2

Thread: How to use buttons and sounds with flash CS3

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    How to use buttons and sounds with flash CS3

    flash templates
    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 !

  2. #2
    Junior Member Settled In sneskid is on a distinguished road
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    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...

+ Reply to Thread

Similar Threads

  1. Active State for flash buttons
    By basteelerfan75 in forum Flash English
    Replies: 0
    Last Post: 14-04-10, 19:42
  2. Replies: 0
    Last Post: 16-03-10, 18:46
  3. Replies: 4
    Last Post: 11-04-08, 01:51
  4. Library Sounds with Flash CS3
    By Flep in forum Tutorials
    Replies: 0
    Last Post: 25-09-07, 15:36
  5. Sounds da libreria con Flash CS3
    By Flep in forum Articoli e tutorials
    Replies: 0
    Last Post: 21-09-07, 07:19

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts