Creating packages: Arranging your scriptsInterface - implementsSo far we have been dealing with one or two scripts, which have functions. However, what if we have many different functions in scripts, which we want to combine and arrange. As a rule in my opinion (some people may have different opinions) try to separate functions into modules like a preloader module, a module to create buttons, a module to arrange a slide on the screen etc. Then it becomes easy to overlook what you have done and especially when you have to make changes.
DO NOT PACK ALL THE INFORMATION INTO ONE LONG SCRIPT.
If your script becomes too long the compiler will complain that your script is too long and may not be executed properly. Now let's get going. The following example could also have been put into one long script without using any interface. However, I do not want to show a vbery complicated example how to use interface, instead this example will lead you into the right direction. Before you start ask yourself: What do we want to do? Goal: We want to show a slide. How do we go ahead, meaning what do we need to show a slide properly?
interface as_scripts.Mymc {
function createMC ():Void;
function preloader (s_movie:String):Void;
function adjust_xy (n_xdistance:Number, n_ydistance:Number, m_fClip:MovieClip):Void;
}
This contains all the methods, which I have listed but the methods are undefined. Now we have to create the methods. First we have to create a movieclip and put it on stage, which is done in this script.
class as_scripts.Fcmc implements as_scripts.Mymc {
public function Fcmc () {
}
function createMC ():Void {
var m_empty:MovieClip = _root.createEmptyMovieClip("m_empty",10);
}
function preloader(s_movie:String):Void {}
function adjust_xy (n_xdistance:Number, n_ydistance:Number, m_fClip:MovieClip):Void {}
}
When we create the class which contains the function, we have to add the line implements as_scripts.Mymc, which refers to the interface. The crucial word here is implements. As you can see this script contains also the other functions but they are empty. We have to add them. If however, you use a subfunction within your class, for which we have a separate script, then we do not need to add this function in the interface class. You find an example for this in the lesson6b folder. Next we want to adjust the x-y position on stage and center the slide loaded into the clip. We are using here one of the previous functions we created but we have to modify it adding the implement word and adding the other functions.
class as_scripts.Adjust implements as_scripts.Mymc {
private static var o_newTimer:Object;
private static var n_nTimer:Number;
public function Adjust () {
}
function adjust_xy (n_xdistance:Number, n_ydistance:Number, m_fClip:MovieClip):Void {
var nu_xdistance:Number = n_xdistance;
var nu_ydistance:Number = n_ydistance;
var mo_fclip:MovieClip = m_fClip;
//function to set the x value
function getFix():Number {
mo_fclip._x = (Stage.width-mo_fclip._width)/2 + nu_xdistance;
return mo_fclip._x;
}
//function to set the y value, if x and y are 0 object is the centered on stage
function getFiy():Number {
mo_fclip._y = (Stage.height-mo_fclip._height)/2 + nu_ydistance;
return mo_fclip._y;
}
//we need to call functions continously
o_newTimer = new Object();
o_newTimer.interval = function () {
getFix();
getFiy();
}
n_nTimer = setInterval (o_newTimer, "interval", 1);
}
function createMC ():Void {}
function preloader(s_movie:String):Void {}
}
Lastly we need the preloader, where also the slide is loaded into the actual movieclip.
class as_scripts.Flmc implements as_scripts.Mymc {
private static var o_myPreloader:Object;
public function Flmc () {
}
function preloader (s_movie:String):Void {
_root.createEmptyMovieClip("m_attachLoader", 100);
_root.createEmptyMovieClip("m_attachLoadtext", 101);
_root.createEmptyMovieClip("m_pload", 102);
_root.m_pload._x = 1000;
_root.m_pload._y = 1000;
_root.m_pload.loadMovie(s_movie);
var s_newmovie:String = s_movie;
var prelTimer:Number;
o_myPreloader = new Object();
o_myPreloader.interval = function() {
if (_root.m_pload._url != _root._url) {
var n_loadedBytes:Number = _root.m_pload.getBytesLoaded();
var n_totalBytes:Number = _root.m_pload.getBytesTotal();
_root.m_attachLoader.attachMovie("m_loaderBar", "m_nloaderBar", 52);
_root.m_attachLoadtext.attachMovie("m_cliploader", "m_nclipLoader", 53);
_root.m_attachLoader.m_nloaderBar._x = 220;
_root.m_attachLoader.m_nloaderBar._y = 340;
_root.m_attachLoadtext.m_nclipLoader._x = 220;
_root.m_attachLoadtext.m_nclipLoader._y = 360;
var n_bytes:Number = Math.ceil((n_loadedBytes/1024)*1000);
var n_percLoaded:Number = Math.ceil((n_loadedBytes/n_totalBytes)*100);
_root.m_attachLoadtext.m_nclipLoader.loadBox.text =
n_percLoaded+" % loaded";
var n_frame:Number = Math.ceil(n_loadedBytes/(n_totalBytes/100));
_root.m_attachLoader.m_nloaderBar.gotoAndStop(n_frame);
if (n_totalBytes>4 && n_loadedBytes>=n_totalBytes) {
clearInterval(prelTimer);
//removeMovieClip(_root.m_attachLoadtext.m_nclipLoader);
//removeMovieClip(_root.m_attachLoader.m_nloaderBar);
_root.m_empty.loadMovie(s_newmovie);
}
}
};
prelTimer = setInterval(o_myPreloader,"interval",20);
}
function createMC():Void {}
function adjust_xy (n_xdistance:Number, n_ydistance:Number, m_fClip:MovieClip):Void {}
}
Finally we have to put something into our fla file. Open the lesson6.fla. We create variables for all the functions we call but the variables are connected by a property value, which here is Mymc and is the interface class. This indicates that all functions belong to the interface. We could not write in that way if there was no interface. Then as we are already used to, we call the functions and add values for the arguments.
import as_scripts.*;
var fcMc:Mymc = new Fcmc();
var flMc:Mymc = new Flmc();
var aXY:Mymc = new Adjust();
Fcmc.createMC();
Flmc.preloader("../images/pic_1.jpg");
aXY.adjust_xy(0, -20, m_empty);
|
|
|