My Snippets

While developing, I realise that I need often the same code, pieces, lines, functions etc ... Coding mostly in Action Script 2, I mainly use SE|PY editor which offer a function for this. So I took everything on it and put it on the Web so it can be avaible everywhere I go.
I also added some classes from my own.

Some of those scripts have comments, some others, not.

Enjoy !

AsBroadcaster

Source

// This is a good Event Handling
//
var someObject:Object = new Object();
someObject.someEvent = function(pEvent, pEvent2) {
        trace("myListener1 received someEvent : "+ pEvent +" "+pEvent2);
};
// Initialize SomeObject to receive and send Events
AsBroadcaster.initialize(someObject);
someObject.addListener(someObject);
// ----
btn.onRelease = function(){
        // broadcastMessage( functionName, param1, param2 ... paramN);
        someObject.broadcastMessage("someEvent", 3,"hop");
}

TextFormat


var format2 = new TextFormat ();
format2.font = "Verdana";
format2.size = 11;
format2.bold = true;

On/Off Button Class

Source

A class to manage 2 MovieClip. One is the button, the other is the target.
The button must have 2 frames with a Stop() on each.


 /**
* @class BoutonOnOff
* @tooltip
* @author Thomahawk
* @version 0.3
*/

import mx.utils.Delegate;
dynamic class as.cla.BoutonOnOff
{
        // properties:
        var _obj : MovieClip;
        var _targ : MovieClip;
        public var _step : Number;
        //constructor:
        public function BoutonOnOff (pObj, pTarg)
        {
                _targ = pTarg;
                _targ._visible = false;
                _obj = pObj;
                _step = 0;
                _obj.onRelease = Delegate.create (this , release );
        }
        // methods:
        private function release ()
        {
                _targ._visible = !_targ._visible;
                if (_step == 0)
                {
                        _obj.play ();
                        _step = 1;
                } else
                {
                        _obj.gotoAndStop (1)
                        _step = 0;                     
                }
        }
        public function get step(){
                return _step;
        }
}
 

XML2TAB


var myTab = new Array();
myXML = new XML ();
myXML.ignoreWhite = true;
myXML.onLoad = function (success)
{
        if (success)
        {
                var tab = this.firstChild ;
                nb = tab.length;
                for (var i = 0; i < nb ; i ++)
                {
                        myTab.push(tab [i].firstChild.nodeValue);                      
                }
                myTab.reverse();
        }
}
myXML.load ("img.xml");
 

For ... in


for (var name in this)
{
        trace ("This is my " + name);
}
 

MovieClipLoader

Source

 // Call the function and specify the name of the file.
function loadMyPic (img)
{
        var mc : MovieClip = this.createEmptyMovieClip ("imgcont_mc", this.getNextHighestDepth ());
        mc._x = 0;
        mc._y = 0;
        // Path of the file
        var image : String = "book/" + img;
        var mclListener : Object = new Object ();
        mclListener.onLoadProgress = function (targ : MovieClip, Loaded : Number, Total : Number) : Void
        {
                var num1 = Math.round ((Loaded / Total) * 100);
                // Return a loading percentage
                trace (num1);
        };
        // Put anything you want after those events ...
        mclListener.onLoadStart = function (target_mc : MovieClip)
        {
        };
        mclListener.onLoadComplete = function (target_mc : MovieClip)
        {
        };
        mclListener.onLoadInit = function (target_mc : MovieClip)
        {
        };
        var image_mcl : MovieClipLoader = new MovieClipLoader ();
        //
        image_mcl.addListener (mclListener);
        image_mcl.loadClip (image, imgcont_mc);
}
 

Class structure


 /**
* @class Actions
* @tooltip Use this to create a new Class
* @author Thomahawk
* @version N/A
*/

dynamic class as.cla.Actions
{
        // properties:
        var _targ : MovieClip;
        //constructor:
        public function Actions (pTarg)
        {
                _targ = pTarg;
        }
        // methods:
        function caracActions (car : String, pts : Number)
        {

        }
}
 

Warn Class

Source

I use this class to display a message on screen, sometime for debbug, sometime for the user. On your stage you initialize it like this :
var myWarn = new Warn(theMc);
where theMc can be a background for your warn text. And :
myWarn.displayWarn("The string you want");


 /**
* @class Warn
* @tooltip No description available.
* @author Thomas
* @version 0.1
*/

import mx.utils.Delegate;
dynamic class Warn
{
        // properties:
        var _targ : MovieClip;
        var _text : String;
        //constructor:
        public function Warn (pTarg)
        {
                _targ = pTarg;
        }
        // methods:
        function displayWarn (pText : String)
        {
                _targ.warn.removeMovieClip ();
                _text = pText;
                var ref : MovieClip = _targ.createEmptyMovieClip ("warn", _targ.getNextHighestDepth ());
                var warnTxt : TextField = ref.createTextField ("warnText", ref.getNextHighestDepth () , 0, 0, 200, 30);
                warnTxt.text = pText;
                warnTxt.autoSize = true;
                ref.onRelease = function ()
                {
                        this.removeMovieClip ();
                }
        }
}
 

Created by Thomahawk