feedback


Creating Flying Alien

Prerequisite

  • Installed the free SonoportCollection1 SDK. Read installation process.
  • Flash Professional CS4 and above. (Flash player 10 and above)
  • Basic knowledge of ActionScript 3.0.
Step 1: First Run
Download the sample code and test run the fla file. Let us now examine the basic code layout.
Step 2: Animating the alien
To create the 'sticky flying' effect of the alien to the mouse cursor, we add this code at the _everyFrame function and the _mouseMove function.
This piece of code will trail the alien as time passes when the mouse cursor is moving on the stage.
private function _everyFrame(e:Event):void 
{ 
    // Update the speed of the alien according to the mouse movement.
    // This create a "sticky" trailling effect.
    _alien.x = (0.9*_alien.x + 0.1*_targetX)-1;
    _alien.y = (0.9*_alien.y + 0.1*_targetY)-1;
} 
 
private function _mouseMove(e:MouseEvent):void 
{ 
    // Update the target values to the mouseX and mouseY values on the stage only.
    _mosX = e.stageX;
    _mosY = e.stageY;
    _targetX = _mosX;
    _targetY = _mosY;
} 
Step 3: Let's Start Adding Sound!
Import the Sonoport library into the project by calling com.sonoport.*
or com.sonoport.SpaceShipAmbience (of any Sound class).
Make sure your fla is linked up to the SonoportCollection1.swc or SonoportCollection1.mxp.
We will use the sound SpaceShipAmbience that has this squeaky spaceship zapping as the sound effect when alien is moving with the mouse cursor.

import com.sonoport.SpaceShipAmbience;

//initialising the SpaceShipAmbience soundmodel in your contructor or init() function
var _spaceSnd:SpaceShipAmbience;

_spaceSnd = new SpaceShipAmbience();

The SpaceShipAmbience's sound properties has some values already set by default.
You can change it by resetting the values according to the kind of SpaceShipAmbience you are going for in your project. In this case, we set a rather low tone as the initial sound.
// Setting some initial values 
_spaceSnd.gain = 1.0;
_spaceSnd.pan = 0;
_spaceSnd.speed = 0.2;
_spaceSnd.clarity = 0.2;

// Calling the play function to play the sound 
_spaceSnd.play();
Step 4: Panning a sound model
Each sound model has a unique set of dynamic properties which can be changed real-time. Let us first change the pan property, which ranges from -1 to 1. Panning will be based on the x-position of our spaceship.
// Dynamic panning
// Pan the sound left and right
var pan:Number = (_alien.x - wSWF/2)/(wSWF/2);
_spaceSnd.pan = pan;
Step 5: Changing the frequency of the sound based on the alien's speed
Now that you are more familiar with dynamically changing a sound model, let us change the 'speed' property based on the spaceship's speed.
You will notice a change in frequency of the sound as the spaceship moves about.
// Find the difference between the mouseX & mouseY from the alien
var dx:Number = Math.abs(_targetX - _alien.x);
var dy:Number = Math.abs(_targetY - _alien.y);

// Calculating the distance that the alien has travelled
var distance:Number = Math.sqrt(dx*dx + dy*dy);

// Change the speed of the sound with respect to the distance of the alien
_spaceSnd.speed = distance/300;

// Change clarity texture of the sound with respect to the position of the alien
_spaceSnd.clarity = _alien.y / MAXTARGETY;
Step 6: Moving Further
You have successfully completed the tutorial! What’s next? Well, it’s time to get creative and have fun! The Sonoport API, located at http://docs.sonoport.com provides in- depth documentation on all the sound models in the library. Through this, you will redefine how audio is used within your Flash projects!
Full Sample code
package {
	
    import flash.display.MovieClip;
    import com.sonoport.SpaceShipAmbience;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.Timer;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFieldAutoSize;
    import flash.events.TimerEvent;

    public class Main extends MovieClip {

        // Stage height and width
        private const wSWF:Number = 700
        private const hSWF:Number = 400;

        private const MINTARGETX:Number = 30;
        private const MAXTARGETX:Number = wSWF - 30;
        private const MINTARGETY:Number = 30;
        private const MAXTARGETY:Number = hSWF - 25;

        private var _targetX:Number;
        private var _targetY:Number;
        private var _mosX:Number;
        private var _mosY:Number;
        private var _prevX:Number;
        private var _prevY:Number;
        private var _maxDistance:Number;
        private var _isMouseLeave:Boolean;

        private var _timer:Timer;

        private var _alien:SpaceAlien;

        private var _spaceSnd:SpaceShipAmbience;

        /** debug message on the stage **/
        private var _debugTxt:TextField;
        private var _mouseTxt:TextField;
        private var _txtFmt:TextFormat;

        public function Main() {

            _init();

            // Set a timer to turn off the sound after 1 second
            _timer = new Timer(1000);

            _timer.addEventListener(TimerEvent.TIMER, _soundOff);

            // Mouse events
            addEventListener(MouseEvent.MOUSE_MOVE, _mouseMove);
            stage.addEventListener(MouseEvent.MOUSE_OVER, _mouseOver);
            stage.addEventListener(MouseEvent.MOUSE_OUT, _mouseOut);

            addEventListener(Event.ENTER_FRAME, _everyFrame);

            _timer.start();

        }

        private function _init():void {

            // Set original target values to the center of the stage
            _targetX = wSWF/2;
            _targetY = hSWF/2;

            // Maximum Distance between (0,0) to (700, 400).
            _maxDistance = Math.sqrt(wSWF*wSWF+hSWF*hSWF);

            // Track if mouse has left the stage
            _isMouseLeave = false;

            // Get the mouse values
            _mosX = stage.mouseX;
            _mosY = stage.mouseY;
            _prevX = 0;
            _prevY = 0;

            // Alien
            _alien = new SpaceAlien();
            _alien.x = _targetX;
            _alien.y = _targetY;
            addChild(_alien);

            // Reset the default property values of the SpaceShipAmbience sound model.
            // Each Sonoport soundmodel has already its own default property values,
            // but you can reset the default for the kind of sound you want for your project.
            _spaceSnd = new SpaceShipAmbience();
            _spaceSnd.gain = 1.0;
            _spaceSnd.pan = 0;
            _spaceSnd.speed = 0.2;
            _spaceSnd.clarity = 0.2;

        }

        /* Create a timer event to turn the sound off */
        private function _soundOff(e:TimerEvent):void {
            _spaceSnd.stop();
        }

        private function _everyFrame(e:Event):void {
            // Set the previous mouse coordinates to the current mouse coordinates.
            _prevX = _mosX;
            _prevY = _mosY;

            // Update the speed of the alien according to the mouse movement.
            // This create a "sticky" trailling effect.
            _alien.x = (0.9*_alien.x + 0.1*_targetX)-1;
            _alien.y = (0.9*_alien.y + 0.1*_targetY)-1;

            var dx:Number = Math.abs(_targetX - _alien.x);
            var dy:Number = Math.abs(_targetY - _alien.y);
            // Calculating the distance that the alien has travelled
            var distance:Number = Math.sqrt(dx*dx + dy*dy);

            // Scale the alien according to the position of the alien
            // Set the maximum scale to 1
            _alien.scaleX = _alien.scaleY = Math.min(1, _alien.y /MAXTARGETY);

            /** Animate the sound properties **/
            // Change the speed of the sound with respect to the distance of the alien
            _spaceSnd.speed = distance/300;

            // Change clarity texture of the sound with respect to the position of the alien
            _spaceSnd.clarity = _alien.y / MAXTARGETY;

            // Pan the sound left and right
            var pan:Number = (_alien.x - wSWF/2)/(wSWF/2);
            _spaceSnd.pan = pan;

        }
        /* Create a mouse event to track if the mouse cursor has left the stage. 
        Then turn off the sound after 2 seconds. */
        private function _mouseOut(e:MouseEvent):void {
            _isMouseLeave = true;
            // Set the _targetX & _targetY to the previous mouse coordinates
            // this resolves the mouse coordinates bug when the swf is running in Firefox.
            _targetX = _prevX;
            _targetY = _prevY;
        }

        private function _mouseMove(e:MouseEvent):void {

            // Reset the timer counter and start counting again
            _timer.reset();
            _timer.start();

            // Update the target values to the mouseX and mouseY values on the stage only.
            _mosX = e.stageX;
            _mosY = e.stageY;
            _targetX = _mosX;
            _targetY = _mosY;


            // Set boundaries around the edge of the stage so that the alien do not 
            // go out of the stage.
            _targetX = Math.min(_targetX, MAXTARGETX);
            _targetX = Math.max(_targetX, MINTARGETX);
            _targetY = Math.min(_targetY, MAXTARGETY);
            _targetY = Math.max(_targetY, MINTARGETY);

        }


        /* Create an event to track if the mouse cursor is back on the stage 
        Turn on the sound. */
        private function _mouseOver(e:MouseEvent):void {
            _isMouseLeave = false;

            // Prevent repetitive playing whenever the event is dispatched
            if (!_spaceSnd.isPlaying) {
                _spaceSnd.play();
            }

        }

    }
	
}
        

Flying Alien

This example uses the Space Ship Ambience sound. It is only 35 kb in size!