Code written in ActionScript

getDistance

Posted by Sérgio Garcez 21 days ago
private function getDist(x1:Number, y1:Number, x2:Number, y2:Number):Number  
        {  
            var dx:Number = x2 - x1;  
            var dy:Number = y2 - y1;  
            return Math.sqrt(dx*dx + dy*dy);  
        }
Language ActionScript

InvisibleScroller

Posted by Anthony Lazaro about 1 year ago
import mx.transitions.OnEnterFrameBeacon;

/**
 * A scroller class with no visible interface
 * Uses masked area to calculate position
 */
class com.orazal.tools.InvisibleScroller {
	// Enter Frame Beacon
	static var __initBeacon = OnEnterFrameBeacon.init();
	// Target Clips
	private var mask:MovieClip;
	private var masked:MovieClip;
	// Properties
	private var dir:String = "vertical";
	private var offset:Object;
	private var oPos:Object;
	private var newPos:Number;
	private var easing:Number = 5;
	
	// Constructor
	public function InvisibleScroller(dire:String, mask_mc:MovieClip, masked_mc:MovieClip) {
		dir = dire;
		mask = mask_mc;
		masked = masked_mc;
		// Store original masked positions
		oPos = {x:masked_mc._x, y:masked_mc._y};
		offset = {x:mask._x - masked._x , y:masked._y - mask._y};
		// Start listening to mouse
		Mouse.addListener(this);
		// EnterFrame listener
		MovieClip.addListener(this);
	}
	
	/**
	 * Allows to change easing
	 */
	public function setEasing(amount):Void{
		easing = amount;
	}
	
	/**
	 * Enter Frame
	 */
	private function onEnterFrame():Void {
		if(dir == "vertical"){
				masked._y  += (newPos - masked._y )/ easing;
		}
		if(dir == "horizontal"){
				masked._x  += (newPos - masked._x )/ easing;
		}
	}
	
	/**
	 * Mouse listener
	 */
	private function onMouseMove() {
		var point:Object = {x:_root._xmouse, y:_root._ymouse};
 		mask.globalToLocal(point); 
		if(this.dir == "vertical"){
			var percent:Number = point.y/mask._height;
			var maskedTotal = offset.y + this.masked._height + 20
			var max:Number = maskedTotal - this.mask._height ;
			if(percent >= 0 && percent<=1){
				newPos =(-max*percent ) + oPos.y ;
			}
		}
		if(this.dir == "horizontal"){
			var percent:Number = point.x/mask._width;
			var maskedTotal = offset.x + this.masked._width + 20;
			var max:Number =  maskedTotal - this.mask._width ;
			if(percent >= 0 && percent<=1){
				newPos =(-max*percent ) + oPos.x;
			}
		}
			
		
	};
	
	public function destroy():Void{
		MovieClip.removeListener(this);
		Mouse.removeListener(this);
		delete this;
	}
}
Language ActionScript / Tagged with actionscript, scroller

Mixing in Actionscript V2 Components EventDispatcher class

Posted by Chad Humphries over 2 years ago
Import the package
import mx.events.EventDispatcher;

Declare the members to be mixed in later
function dispatchEvent() {};
function addEventListener() {};
function removeEventListener() {};

Withing your init() method, mix in the Event Dispatcher:
function init(){
	mx.events.EventDispatcher.initialize(this);
}

When you need to dispatch an event, simpy use dispatchEvent();
dispatchEvent({type:"change"});

Language ActionScript / Tagged with events

Static initialiser

Posted by Chad Humphries over 2 years ago
Actionscript does not have static initialisers in the java sense, but you can still fake it

private static var staticInitialised:Boolean = staticInit();
private static function staticInit():Boolean{
	//TODO: Initialise all static members
	return true;
}

Language ActionScript / Tagged with methods

XMLObject to DataSet

Posted by Chad Humphries over 2 years ago
//================================================
// XMLObject to DataSet
//================================================
// This allows you to use a standard XML object
// to load data into a datasest. Using the standard
// allows the use of a progress bar to show the
// loading of the XML data. With the XML connector
// component it seems that there is no easy way to
// display the load status. This is designed
// for Flash MX 2004 Pro.
//------------------------------------------------
// [NOTES]
// loaderPopup is a movieclip that has a progress
// bar component in it that is tied to the progress
// of the XML obj.  btnLoadBig_jea is a button
// component that is used to trigger the XML Load.
//------------------------------------------------
loaderPopup._visible = 0; // hide progressbar loader movieclip
var mainXML:XML = new XML();// define & strict type the main xml object

// Once the XML loads, do this..
function xmlPopDsJEA () {
        _root.myDataSet.disableEvents(); // turn off events on the dataset. Processes faster.
        _root.loaderPopup._visible = 0; // hide progresbar loader MC
        _root.btnLoadBig_jea.enabled = 0; // turn off load button [optional]
        _root.btnLoadBig_jea.label = "XML Loaded"; // change the text on the load button [optional]
        numPeople = _root.mainXML.firstChild.childNodes.length - 1; // how many rows
        for (i=0; i
Language ActionScript / Tagged with flash, xml

Elastic Effect

Posted by Chad Humphries over 2 years ago
MovieClip.prototype.elasticScale = function(target, convert, x, y, alpha) {
	var _loc1 = this;
	var _loc2 = convert;
	if (_loc1.accel < 6.000000E-001) {
		_loc1.accel = _loc1.accel + 2.000000E-001;
	}
	// end if                                    
	_loc1.xScale = _loc1.xScale * _loc1.accel + (target - _loc1._xscale) * _loc2;
	_loc1.yScale = _loc1.yScale * _loc1.accel + (target - _loc1._yscale) * _loc2;
	_loc1.x = _loc1.x * _loc1.accel + (x - _loc1._x) * _loc2;
	_loc1.y = _loc1.y * _loc1.accel + (y - _loc1._y) * _loc2;
	_loc1._xscale = _loc1._xscale + _loc1.xScale;
	_loc1._yscale = _loc1._yscale + _loc1.yScale;
	_loc1._x = _loc1._x + _loc1.x;
	_loc1._y = _loc1._y + _loc1.y;
	_loc1._alpha = _loc1._alpha + (alpha - _loc1._alpha) * 2.000000E-001;
};
MovieClip.prototype.ativaElastic = function(delay) {
	mv = this;                                 
	mv.c = 0;
	mv._alpha = 0;
	mv.delay = delay * 1;
	mv.xIni = int(mv._x);
	mv.yIni = mv._y;
	mv._x = mv._parent.ref._x;
	mv._y = mv._parent.ref._y;
	mv._xscale = mv._yscale = 0;
	mv.onEnterFrame = function() {
		++mv.c;
		if (mv.c > mv.delay) {
			mv._alpha = mv._alpha + (100 - mv._alpha) * 2.000000E-001;
			mv.elasticScale(100, 2.000000E-001, mv.xIni, mv.yIni, 100);
		}
		// end if                                    
		if (mv.c > 20 + mv.delay) {
			delete mv.onEnterFrame;
			mv._xscale = mv._yscale = 100;
		}
		// end if                                    
	};
};

//USE IN MOVIECLIP
onClipEvent (load) {
	this.ativaElastic(1);
}
Language ActionScript / Tagged with effects

setTimeout in Actionscript

setTimeout is a useful built-in Javascript function, not available for Flash MX and under. It waits for a number of milliseconds and then runs a specified function. Here is a snippet that defines setTimeout in Flash MX.

_global.setTimeout = function(a,b,c, args){
    // for a basic function call:
    if (typeof arguments[0] == "function"){
        args = arguments.slice(2);
        var ID, func = function(){
            a.apply(null, args);
            clearInterval(ID);
        }
        ID = setInterval(func, b, args);
        
    // for an object method call:
    }else{
        args = arguments.slice(3);
        var ID, func = function(){
            a[b].apply(a, args);
            clearInterval(ID);
        }
        ID = setInterval(func, c, args);
    }
    return ID;
}
_global.clearTimeout = clearInterval;

This snippet was originally taken from http://www.actionscript.org/forums/showpost.php3?p=116265&postcount=3. Thank you very much to senocular, who provided the code.
Language ActionScript / Tagged with functions

MVC for ActionScript

Model View Controler design pattern for ActionScript
Language ActionScript / Tagged with patterns

Flash source files with lots of F8 features

Posted by Chad Humphries over 2 years ago / Source: http://www.senocular.com/flash/source.php
Wild examples of features now available with Flash 8.
Language ActionScript