PDA

View Full Version : can .as files just hold functions?


tazboy
07-18-2009, 05:52 AM
Can I use .as files for just about anything? Most people use them for classes though, correct?

If I'm making a program that has 20 functions would it be best to put those functions into .as files?

lordofduct
07-18-2009, 07:53 AM
yep

like this:


/**
* METHOD
*
* setText - written by Dylan Engelman a.k.a LordOfDuct
*
*
* set the text in a textfield
*/
package com.lordofduct.util.supplemental
{
import flash.text.TextField;

public function setText( text:String, field:TextField, align:String="left", condenseWhite:Boolean=true ):void
{
field.condenseWhite = condenseWhite;
field.htmlText = text;
if (align) field.autoSize = align;
field.condenseWhite = false;
}
}


the file has to be named the same as the function

this files name is: setText.as

ASWC
07-18-2009, 03:33 PM
yes that what include is for:
include "myasfile.as"
this allows you to keep procedural code well organized. You could include for example variables.as, then loading.as, and so on as your program flow goes. On the other hand classes are imported via the import keyword.

tazboy
07-18-2009, 03:52 PM
Thanks. Now what if I had two similar functions and wanted them to be inside the same .as file, kind of like math functions? Would I have to make a class? What would that look like?

Thanks.

ASWC
07-18-2009, 04:02 PM
you won't have to make it a class but if this is the kind of function that you will be reusing for other projects then it might be wiser to make it a class.

tazboy
07-18-2009, 04:20 PM
lord of duct said the file has to be named the same as the function so if that's true then I could have only one function for each .as file. So then how would I have multiple functions without making a class?

Thanks.

ASWC
07-18-2009, 04:28 PM
no for a procedural code .as file you can give any name you want. Only for classes you have to follow rules.

henke37
07-18-2009, 05:39 PM
Actually, that's not true. The naming rules applies to everything that you import, functions included.
Remember, include is just dumping the code right there and then. Import searches the class search path and something something.

ASWC
07-18-2009, 06:04 PM
the include keyword is not used to import classes but to simply include code. Such files can have any name you want.

lordofduct
07-18-2009, 08:06 PM
ASWC is referring to a different way of doing things.


To put it together:

The method I showed you creates a single package wide method for use. Once imported it can be used and takes up little space in memory. It is like a simple static member contained in a .as file.

If you want you can create a static member class to store multiple related functions in one place.


ASWC's method is completely different is more related to timeline coding. If you want to "include" external script into a series of timeline procedural code you can use that. The thing is every inclussion takes up more memory and it is specific to timeline procedural code.




Two completely different paradigms though as AS3 allows for either procedural or object oriented.

Oh and if you want an example of a static member class:


/**
* LoDMath - written by Dylan Engelman a.k.a LordOfDuct
*
*
* Basic Math operations for use.
*/
package com.lordofduct.util
{
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;

public class LoDMath
{
/**
* CONSTANTS
*/
public static const ZERO_TOL:Number = 0.0001; // zero tolerance

public static const PI:Number = 3.141592653589793; //number pi
public static const PI_2:Number = 1.5707963267948965; //PI / 2 OR 90 deg
public static const PI_4:Number = 0.7853981633974483; //PI / 4 OR 45 deg
public static const PI_8:Number = 0.39269908169872413; //PI / 8 OR 22.5 deg
public static const PI_16:Number = 0.19634954084936206; //PI / 16 OR 11.25 deg
public static const TWO_PI:Number = 6.283185307179586; //2 * PI OR 180 deg
public static const THREE_PI_2:Number = 4.7123889803846895; //3 * PI_2 OR 270 deg
public static const E:Number = 2.71828182845905; //number e
public static const LN10:Number = 2.302585092994046; //ln(10)
public static const LN2:Number = 0.6931471805599453; //ln(2)
public static const LOG10E:Number = 0.4342944819032518; //logB10(e)
public static const LOG2E:Number = 1.442695040888963387; //logB2(e)
public static const SQRT1_2:Number = 0.7071067811865476; //sqrt( 1 / 2 )
public static const SQRT2:Number = 1.4142135623730951; //sqrt( 2 )
public static const DEG_TO_RAD:Number = 0.017453292519943294444444444444444; //Math.PI / 180;
public static const RAD_TO_DEG:Number = 57.295779513082325225835265587527; // 180.0 / Math.PI;

public static const ONE_THIRD:Number = 0.333333333333333333333333333333333; // 1.0/3.0;
public static const TWO_THIRDS:Number = 0.666666666666666666666666666666666; // 2.0/3.0;
public static const ONE_SIXTH:Number = 0.166666666666666666666666666666666; // 1.0/6.0;

public static const COS_PI_3:Number = 0.86602540378443864676372317075294;
public static const SIN_2PI_3:Number = 0.03654595;

public static const CIRCLE_ALPHA:Number = 0.5522847498307933984022516322796; //4*(Math.sqrt(2)-1)/3.0;

public static const ON:Boolean = true;
public static const OFF:Boolean = false;

public static function get EPSILON():Number
{
// Machine epsilon ala Eispack
var fourThirds:Number = 4.0/3.0;
var third:Number = fourThirds - 1.0;
var one:Number = third + third + third;
return Math.abs(1.0 - one);
}

/**
* NO CONSTRUCTOR
*/
public function LoDMath()
{
throw new Error("com.lordofduct.util::LoDMath - this is a static member class and doesn't ever need to be instantiated");
}

/**
* ratio of value to a range
*/
public static function percentageMinMax(val:Number, max:Number, min:Number = 0):Number
{
val -= min;
max -= min;

if (!max) return 0;
else return val / max;
}

/**
* a value representing the sign of the value.
* -1 for negative, +1 for positive, 0 if value is 0
*/
public static function sign(n:Number): int
{
if (n) return n / Math.abs(n);
else return 0;
}

/**
* wrap a value around a range, similar to modulus with a floating minimum
*/
public static function wrap(val:Number, max:Number, min:Number = 0):Number
{
val -= min;
max -= min;
val %= max;
val += min;
while (val < min)
val += max;

return val;
}

/**
* force a value within the boundaries of two values
*
* if max < min, min is returned
*/
public static function clamp(input:Number, max:Number, min:Number = 0):Number
{
return Math.max( min, Math.min( max, input ) );
}

/**
* a one dimensional linear interpolation of a value.
*/
public static function interpolateFloat(a:Number, b:Number, weight:Number):Number
{
return (b - a) * weight + a;
}

public static function radiansToDegrees( angle:Number ):Number
{
return angle * RAD_TO_DEG;
}

public static function degreesToRadians( angle:Number ):Number
{
return angle * DEG_TO_RAD;
}

/**
* closest angle between two angles
*/
public static function nearestAngleBetween( a1:Number, a2:Number, radians:Boolean = true ):Number
{
var rd:Number = (radians) ? PI : 180;

if ( a1 < -rd / 2 && a2 > rd / 2 ) a1 += rd * 2;
if ( a1 > rd / 2 && a2 < -rd / 2 ) a1 -= rd * 2;

return a2 - a1;
}

/**
* interpolate across the shortest arc between two angles
*/
public static function interpolateAngles( a1:Number, a2:Number, weight:Number, radians:Boolean = true ):Number
{
var rd:Number = (radians) ? PI : 180;

if ( a1 < -rd / 2 && a2 > rd / 2 ) a1 += rd * 2;
if ( a1 > rd / 2 && a2 < -rd / 2 ) a1 -= rd * 2;

return interpolateFloat( a1, a2, weight );
}

/**
* Random Helpers
*/
/**
* Random Number between minimum and maximum
*/
public static function randomMinMax(max:Number, min:Number = 0):Number
{
return Math.random() * (max - min) + min;
}

//returns a random integer between min and max (max included)
public static function randomIntMinMax(max:int, min:int = 0):int
{
return Math.floor(Math.random() * (max - min + 0.9999)) + min;
}

//returns a value between -1 and +1
public static function randomN1P1(...args):Number
{
return Math.random() * 2 - 1;
}

//returns either 0 or 1
public static function randomPop(...args):int
{
return Math.round(Math.random());
}

//kind of like randomPop, but returns the value as a Boolean
public static function randomBool(...args):Boolean
{
return randomPop() as Boolean;
}

//return either -1 or +1... good for "flipping" values randomly
public static function randomFlip(...args):int
{
if ( randomBool() ) return 1;
else return -1;
}

//returns a random angle in radians from -pi to +pi
public static function randomAngle(...args):Number
{
return Math.random() * 2 * PI - PI;
}

//returns -1, 0, or 1 randomly. This can be used for bizarre things like randomizing an array
public static function randomShift(...args):int
{
return randomIntMinMax( 1, -1 );
}

public static function randomizeArray( arr:Array ):Array
{
var rtn:Array = arr.concat();

rtn.sort( randomShift );

return rtn;
}

/**
* Awkward math methods
*/
/**
* Returns a new Rectangle describing the bounds of a Rectangle transformed by a Matrix.
* This will include rotation, translation, skewing, and scaling.
*/
public static function transformRectByMatrix( rect:Rectangle, matrix:Matrix ):Rectangle
{
var rtl:Point = matrix.transformPoint( rect.topLeft );
var rbl:Point = matrix.transformPoint( new Point( rect.left, rect.bottom ) );
var rbr:Point = matrix.transformPoint( rect.bottomRight );
var rtr:Point = matrix.transformPoint( new Point( rect.right, rect.top ) );

var left:Number = Math.min( rtl.x, rbl.x, rbr.x, rtr.x );
var right:Number = Math.max( rtl.x, rbl.x, rbr.x, rtr.x );
var top:Number = Math.min( rtl.y, rbl.y, rbr.y, rtr.y );
var bottom:Number = Math.max( rtl.y, rbl.y, rbr.y, rtr.y );

return new Rectangle( left, top, right - left, bottom - top );
}

/**
* Check if a value is prime.
*
* @param val - uint to check for primality
*
* In this method to increase speed we first check if the value is <= 1, because values <= 1 are not prime by definition.
* Then we check if the value is even but not equal to 2. If so the value is most certainly not prime.
* Lastly we loop through all odd divisors. No point in checking 1 or even divisors, because if it were divisible by an even
* number it would be divisible by 2. If any divisor existed when i > value / i then its compliment would have already
* been located. And lastly the loop will never reach i == val because i will never be > sqrt(val).
*/
static public function isPrime( val:int ):Boolean
{
//check if value is in prime number range
if (val < 2) return false;

//check if even, but not equal to 2
if (!(val % 2) && val != 2) return false;

//if 2 or odd, check if any nontrivial divisor exists
for (var i:int = 3; i <= val / i; i += 2)
{
if (!(val % i)) return false;
}

return true;
}
}
}


you'd use this like so:


import com.lordofduct.util.LoDMath;

var value:Number = LoDMath.randomMinMax(5,0);//get a random value between 0 and 5


very similar to the root Math class supplied with AS3.

ASWC
07-18-2009, 11:07 PM
you can also include .as files in your classes using the include keyword. ;)

lordofduct
07-18-2009, 11:50 PM
you can also include .as files in your classes using the include keyword. ;)

it's still a procedural concept though. My point by saing "timeline procedural methods" is that timeline is where you tend to utilize procedural paradigm, where as if you're writing your code on a package level, you tend to be using an object oriented paradigm.

ASWC
07-19-2009, 03:44 AM
I'm just trying to point what include is for. Basically it is for organizing code whether procedural or not much like a require() in PHP. This might come in handy from time to time. As an example you could write a class like this:
package{

import flash.display.*;

public class Include extends MovieClip{
include "include2.as"
}

}
where the .as file included could have all methods needed for the class:
public function Include(){
trace("hello");
}

tazboy
07-19-2009, 05:14 AM
Thanks for all the info. It's appreciated.