Basic Computer Concepts (Storage, Selection, Repetition)
1) Store items in a variable.
2) An Array is a related list of items.
3) A conditional (if/else) statement functions like an on/off switch
4) A "for" loop is a counter loop
EXAMPLE 01:
• index variable
• Pushing data into an Array;
• text field/variable output
• Trace output
// The script is on an instance of a movie clip
on (press) {
// variable num is rooted at Stage
// add item to the Array
var num:Number = _root.myData.push(this._name)-1;
// OUTPUT to STAGE: text field on the screen
_root.whatclicked = "num: " + num + newline +
"_root.myData[num]: " + _root.myData[num] + newline +
"_root.myData: " +_root.myData;
// TRACE window output
trace("num: " + num);
trace("_root.myData[num]: " + _root.myData[num]);
trace("_root.myData: " +_root.myData);
trace(_root.myData.length);
}
---------------------------------------------------------
EXAMPLE 02:
• Repeat loop
• index variable "i"
• condition using length
• increment i++
• Array item
on(release) {
// clear the text field
_root.whatclicked = "";
// output to the text field the items in the Array
// each item is placed on a new line
for (var i:Number = 0; i<_root.myData.length; i++) {
_root.whatclicked += _root.myData[i] + newline;
}
}
---------------------------------------------------------
EXAMPLE 03:
• startDrag()
• x and y locations
• stopDrag()
• setting a variable + 1
• duplicate this movie clip
• assign a location x and y
on(press) {
this.startDrag();
whereX = this._x;
whereY = this._y;
}
on(release) {
this.stopDrag();
_root.iteration++
trace(i);
// this is what is duplicated, new name, depth
this.duplicateMovieClip("clip=" + _root.iteration, _root.iteration);
this._x = whereX;
this._y = whereY;
}
---------------------------------------------------------
EXAMPLE 04:
• Drag and drop album covers to play mp3 file
• Selection structure (if else)
• FRAME SCRIPT example for movie clip instances
// all in FRAME SCRIPT
var dancing:Sound = new Sound();
// ALBUM #1 ****************************************************//
album1_mc.onPress = function():Void {
this.startDrag(true);
reply_txt.text = "";
reply_txt.text = "Selected Album#1";
// snapback code begins ************************ //
if (album1_mc._x == squareTarget_mc._x) {
snapback = true;
}else {
// start position
startpositionX = album1_mc._x;
startpositionY = album1_mc._y;
}
// snapback code ends ************************ //
album1_mc.swapDepths(this.getNextHighestDepth());
// xstart = this._x;
// start = this._y;
dancing.stop();
};
//ALBUM #1
album1_mc.onRelease = function():Void {
this.stopDrag();
// snapback code begins ************************ //
if(snapback) {
album1_mc._x = startpositionX;
album1_mc._y = startpositionY;
snapback = false;
}else {
// snapback code ends ************************ //
if (eval(this._droptarget) == squareTarget_mc) {
reply_txt.text = "Playing Album#1";
// this.enabled = false;
// snap album cover to the movie clip target //
album1_mc._x = squareTarget_mc._x;
album1_mc._y = squareTarget_mc._y;
// ----------------------------------------- //
counter++;
dancing.loadSound("1.mp3", true); // true is for streaming mp3 file
dancing.start(0,1);
onoff = true;
} else {
reply_txt.text = "Try Again";
this._x = xstart;
this._y = ystart;
}
} // include this closure of the snapback else
};
// end of ALBUM #1 **********************************************//