Monthly Archives: February 2007

Vote for a Yahoo! Maps ActionScript 3 API

Want to use Yahoo! Maps in Flash 9 or Flex 2? Vote for it. Let your voice be heard! The Yahoo! Developer Network suggestion board has a topic with digg-like voting that is getting some strong interest, and I want to see the votes jump into the triple digits. If you’re unfamiliar with the current sitation, the Maps API targets ActionScript 2. There have been a thousand different ways to use LocalConnection to communicate with the AS2 version of Yahoo! Maps from Flash 9. Let me tell you, it feels like the big ugly hack that it is.

Now, though I work at Yahoo!, my team doesn’t control this particular API. As a result, we’re pushing hard to let the Maps team know that developers want a first-class API for Flex 2 and the cutting edge of the Flash Platform. To help our efforts, I urge you to vote for an ActionScript 3 Yahoo! Maps API. Not only will you have a proper API without hacks, but it will be running 100% in AVM2, and you should see a nice speed boost in your applications.

Advanced ActionScript Rounding Functions

Everyone knows about and uses Math.round(), Math.ceil(), and Math.floor(). They’re great, but what if you don’t want to round to the nearest whole number? For instance, you might want to round to the nearest even number, or even a fractional value like the nearest tenth. The following functions make rounding in ActionScript 3 a lot more robust:

package com.joshtynjala.utils
{

/**
 * Utility functions for the manipulation of numbers.
 */
public class MathUtils
{
	/**
	 * Rounds a target number to the nearest multiple of another number.
	 * @see Math#round
	 */
	public static function roundToNearest(numberToRound:Number, nearest:Number):Number
	{
		return Math.round(numberToRound / nearest) * nearest;
	}

	/**
	 * Rounds a target number up to the nearest multiple of another number.
	 * @see Math#ceil
	 */
	public static function roundUpToNearest(numberToRound:Number, nearest:Number):Number
	{
		return Math.ceil(numberToRound / nearest) * nearest;
	}

	/**
	 * Rounds a target number down to the nearest multiple of another number.
	 * @see Math#floor
	 */
	public static function roundDownToNearest(numberToRound:Number, nearest:Number):Number
	{
		return Math.floor(numberToRound / nearest) * nearest;
	}

	/**
	 * Rounds a target number to a specific number of decimal places.
	 * @see Math#round
	 */
	public static function roundToPrecision(number:Number, precision:int = 0):Number
	{
		var decimalPlaces:Number = Math.pow(10, precision);
		return Math.round(decimalPlaces * number) / decimalPlaces;
	}
}

}
/*
Copyright (c) 2007 Josh Tynjala

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

I’ve used various versions of this class on many projects, and the functions will always be in my toolbox. You can download the class, and use it under the terms of the MIT License.