Sound in Flash Actionscript

Flash

For using a sound in Flash Actionscript we must use Linkage properties and choose Export for Actionscript option. Look at this picture :

Flash Linkage Properties

Choose a suitable name for Identifier, because you will need it in Actionscript.

In next move we should make an object from our sound :

mySound = new Sound();

Then with the name we choose for Identifier before we attach it to our object:

mySound.attachSound("test");

With start method we can make our sound file play, the first parameter will tell Flash to start sound at the specific point (in seconds). Second parameter will specify number of loops :

mySound.start(0,99999);

Some of sound methods

  • stop methol will stop the sound.
  • position method will store the stop point of sound in milliseconds.
  • setVolume method will specify the sound volume.
  • getVolume will get the sound volume. (between 0 and 100)
  • setPan will specify on which channel should sound be played, the value is between 100 and -100
  • getPan will return the current Pan value.
  • setTransform method will use and object with 4 parameters.
  • duration method will return the length of the sound in milliseconds.

Load links from a text file in flash

Flash In this tutorial I am going to tell you how can you use a simple text file to add links to your buttons. With this method you can easily update your links without any need to change the source of flash file, for example if your client wants to change the links but they don’t know any actionscripting !

First create a new document in Flash and create your desired buttons, then create a new layer in root and name it actions, type these codes in there :

function loadlinks(loaded) {
	if (loaded) {
		home = mylinks.home;
		about = mylinks.about;
	}
}
mylinks = new LoadVars();
mylinks.onLoad = loadlinks;
mylinks.load("links.txt");

In above codes first we create a new object from LoadVars class, then with loadlinks function we load our links from the external text file into our Flash document.

Next create a new text document, for example we have 2 buttons with home and about links, we can type these codes in our text document :

home=http://www.mysite.com&about=http://www.mysite.com/about.php

Save this document with links.txt name. Now go to your Flash document and on your buttons type these codes, for home button :

on (press) {
	getURL(_root.home);
}

and for about button :

on (press) {
	getURL(_root.about);
}

Its finished, now you can easily update your links without changing the source of Flash file, remember to put your text file where your .swf file is.

The Best way to test the hit of objects in Flash

Flash In Flash there is a method named hitTest which will test if two objects hit each other or not, but it has a problem and that is because it tests with the object bounding box not the exact edge of the object.

To solve this problem we can easily use a simple mathematic formula which all of you are familiar with it, the Pythagorean formula. Look at this picture :

Distance

In this picture the hypotenuse is the distance between two objects, the difference between x1 and x2 will be one side of triangle and difference between y1 and y2 is another side. We can easily use this formula in Flash.

Create a new document in Flash, then create two objects and convert them to movieclips, create the instance name for them, mc1 and mc2. Create a text box and choose dynamic one, place a hittest in instance name box. Now copy and paste these codes in main frame :

mc1.startDrag(true);
onEnterFrame = function () {
	var dx:Number = mc2._x-mc1._x;
	var dy:Number = mc2._y-mc1._y;
	var dist:Number = Math.sqrt(dx*dx+dy*dy);
	if (dist<mc1._width/2+mc2._width/2) {
		_root.hittest.text = "Hit !";
	} else {
		_root.hittest.text = "";
	}
};

In the first line of code it will start to drag the mc1 movieclip, in next lines it will check the distance between two objects in every frame, and when the distance became lower than sum of the half width of two objects it will show the ‘Hit !’ message.

Hope it was helpful ! You can download the source from here :

Download File

Creating a Clock with Flash Actionscript

Flash
In this tutorial I am going to tell you how to create a clock with Adobe Flash Actionscript programming, you need to know some basic level knowledge of Flash and Actionscript programming to fully understand this.

Create Graphics

First create a new file in flash (AS 2.0), We need 4 movieclips, one for clock body, one for hour hand, one for minute hand, one for second hand and one for clock body. Also we need a text box for showing the time. Create some graphics as you like and convert them to movieclips, you can see this picture for example :

Flash Clock

Please note that when you are converting your graphics into movieclips, choose the right registration points like the picture that I showed you.
Read More