-Site Map cristhian_alca@yahoo.com
cristhian_alca@yahoo.com
Flash ActionScript helpful code AND HINTS

Just some flash common code i found handy and i don't want to remember all the time :)

INDEX

- To dynamically create and manipulate sounds
- To move objects with mouse events


* To dynamically create and manipulate sounds (ie: mp3s)

Complete tutorial for sounds: http://www.flasherdot.org/T/T24.htm

  1. Import an mp3 to the library. Select the imported item and set the "linkage identifier" to sound1 so we can create an instance at running time with actionscript.
  2. IN the first frame of your movie clip you need to create an instance of the movie clip with a code like this:
    mysound = new Sound();
    mysound.attachSound("sound1");
    var playing = false; 
    mysound.onSoundComplete = function(){
    	playing = false;
    }
    			  
  3. Now create a "stop" button and "play button" and add these code pieces inside their respective ON RELEASE blocks:

    For the start button:
    if ( !playing ){
         mysound.start();
         playing = true;
    } 
    
    For the stop button:

    if ( playing ){
         mysound.stop("sound1");
         playing = false;
    } 
    

* To move objects with mouse events

To move some object with the mouse here is some basic code:

circ1.onMouseMove = function (){

  this._x = _root._xmouse;
  this._y = _root._ymouse;

 updateAfterEvent;
}

To retrieve and save info of a database using a movieclip;

/* #######################################################
######## ACTIONS OF BUTTON TO SHOW INFO OF USER
########################################################3
*/
button1.onRelease = function (){

loadFirstUser(_root.searchid.text);
}

/* get the first user that my php function returns */
function loadFirstUser(userid){
var auserVars = new LoadVars;
auserVars.onLoad = onUserLoaded;
auserVars.load(server + "getUsers_loaddata.php?id="+userid);
}

function onUserLoaded(){

//trace("received this: " + this.firstname );
_root.userclip1.firstname.text = this.firstname;
_root.userclip1.lastname.text = this.lastname;
_root.userclip1.email.text = this.email;
_root.userclip1.career.text = this.career;
_root.userclip1.user_id = this.id;

}
saveuser.onRelease = function (){

saveUser();
}

/* get the first user that my php function returns */
function saveUser(){
var saveuserVars = new LoadVars;
saveuserVars.onLoad = onsaveuser;

var args = "";

args += "id="+userclip1.user_id+"&";
args += "firstname="+userclip1.firstname.text+"&";
args += "lastname="+userclip1.lastname.text+"&";
args += "email="+userclip1.email.text+"&";
args += "career="+userclip1.career.text;

saveuserVars.load(server + "saveUsers_loaddata.php?"+args);
}

function onsaveuser(){


}

Example of XML Parsing

/*******************************************************
* Assign default Values to the variables used
*******************************************************/
function alb_intAlbum(){
    imgDepth  = 0 // level which contains no other elements.
    AlbumList = new Array()
    curAlbum  = 0
}

/*******************************************************
* Request XML page from server
*******************************************************/
function alb_loadPage(xmlFile){
    alb_intAlbum()
    albumXML = new XML();
    albumXML.onLoad = alb_parseXML;
    albumXML.load(xmlFile);
    albumXML.ignoreWhite = true;
}

/********************************************************
* Parse the incoming XML
********************************************************/  
function alb_parseXML(success){
        if(success){ //file loaded ok
            AlbumList = new Array()
            for(xalb = 0; xalb < albumXML.childNodes[0].childNodes.length;xalb++){
                AlbumList.push( albumXML.childNodes[0].childNodes[xalb] );
            }
            
            //create a movieclip to hold the album images.
            createEmptyMovieClip("album",imgDepth++);
            album.loadAlbum(albumList[curAlbum]);
            
            album._x = Stage.width/2  - album._width/2
            album._y = Stage.height/2 - album._height/2    
            
        }else{
            trace("ERROR: Could not load XML file"); //XML file could not be loaded
        }
}

Draggable Pictures loaded from empty movie clip

//X_list = new Array(52,128,204,280,356,432);
//Y_list = new Array(52,128,203,280,356);
depth = 0;
//loads multiple images - Blue

// key is to load on on each level
var thisbpic = _root.createEmptyMovieClip("bpic_"+i,++depth);
thisbpic.createEmptyMovieClip("picHolder",++depth);
thisbpic._x = 50;
thisbpic._y = 50;
thisbpic.picHolder.loadMovie("catbee.jpg");
thisbpic.onPress = dragOn;
thisbpic.onRelease = dragOff;

function dragOn() {
startDrag(this);
}
function dragOff() {
this.stopDrag();
}

Just drawing lines and filing areas.

function drawsomething( myclip:MovieClip ) {

myclip.moveTo(10,10);
//myclip.lineStyle(2,0xeffCC4f);
myclip.lineStyle(5, 0x00ff00 );
//myclip.beginFill( 0x00ff00, 0x0000ff);
myclip.beginFill( 0xff0000, 2);

myclip.lineTo(200,10);
myclip.lineTo(200,210);
myclip.lineTo(10,210);
myclip.lineTo(10,10);

myclip.endFill();

}