Page: 15
Please Log In or Register

Math Mapping Functions

I have not posted in a while, do to finals, vacations and excuses. But for my final I got into algorithmic animation and I used these functions a great deal to help me out. The values must be in the range to work correctly.

// returns a number between 0.0 and 1.0 //
public static function normalize(value:Number, low:Number, high:Number):Number {
   return value/(high + low);
}

// value should always be between 0.0 – 1.0. Places value into desired range of low – high //
public static function lerp(value:Number, low:Number, high:Number):Number {
   return value * (high – low) + low;
}

// convert the value parameter from range low1 to high1 to the range low2 to high2 //
public static function map(value:Number, low1, high1, low2, high2):Number {
   return normalize(value, low1, high1) * (high2 – low2) + low2;
}

// convert radians to degrees //
public static function toDegrees(radians:Number):Number {
   return radians * 180/Math.PI;
}

// convert degrees to radians //
public static function toRadians(degrees:Number):Number {
   return degrees * Math.PI / 180;
}

Download Class


AMFPHP 1.9 and Actionscript 3

I recently installed amfphp 1.9 and have gotten it to work thanks to some tutorials by Lee Brimelow on some introductions to amfphp.

Introduction to AMFPHP 1
Introduction to AMFPHP 2

He also did a blog post about some security issues regarding amfphp. These were great to get me started, but I ran into a few problems when I began to play around with it on my own.

First, I always place some returns at the top of my documents just for some space. However, this caused Flash to return a Server.badCall error, so make sure that there are no extra returns at the top of your document.

I had trouble finding the data that the responder returns by using the method that Lee described, but I found that if the amfphp function returns a single value, like “false” or “0”, then you can access it through :

function onTotal(responds:Object):void {
total = int( responds.serverInfo.initialData );
}

If you are dealing with a function that returns multiple values, such as a query to a database, I found that you can cycle through the results with this :

function onGetUserInfo(responds:Object):void {
for (var i:int = 0; i < responds.serverInfo.initialData.length; i++) { trace(responds.serverInfo.initialData[i]);
trace(responds.serverInfo.initialData[i][0]);
}
}

Another problem that I ran into was the calls returning null values. This was due to the fact that I was directly referencing the file name.
i.e. http://www.site.com/amfphp/gateway.php. This worked when the user was at www.site.com, however, when the user accessed the swf from site.com it would return null values. I found that a quick fix was to simply reference the gateway through “/amfphp/gateway.php”.

Lee chose to display examples from php 5 and the constructor function for the class that he mentions for php 5 is
public function __construct() {}
}
?>

If you are using php 4, the constructor function of the class is the same name as the class that contains it
function DB() {}
}?>

I am not very good at php so I hope that this helps someone, as some of these errors were tough to spot, mainly the class constructor and the url with www or not.


Newer Posts