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 !
Bookmarks