Tagged with math

Snippet Saturday: isRoughlyEqual() PLUS: as3-utils

by

Last week, I started a (hopefully) weekly post where I throw out a (hopefully) useful piece of code for people to (hopefully) use in their projects. I’m calling this #SnippetSaturday.

John Lindquist commented almost immediately that there is a project on github designed to collect these sorts of useful little snippets of code. It’s called as3-utils. Check it out!

This week’s snippet, isRoughlyEqual(), provides a simple way to find out if two numbers are in the ballpark of each other.

I’ve added it to the as3-utils project too.
http://github.com/mimshwright/as3-utils/blob/master/src/utils/number/isRoughlyEqual.as

Tagged , ,

Snippet Saturday: limit() function

by

So I’m sitting on a bunch of pretty useful code and I’m not sure how to share it with people. I could create a massive library that combines all the miscellaneous bits into a single, poorly documented library, but after looking at the Flash Game Dojo’s wiki, I’m starting to believe more strongly in the idea of releasing code in smaller pieces that achieve a particular function. So, I’m going to try to kick off this idea of #SnippetSaturday. I’m sure I won’t manage to post something every week but I’ll do my best. If you’ve got a blog or just twitter, I’d encourage you to join in too!

Anyway, here’s my first contribution. It’s a simple little function that limits a number between an upper and lower limit.

BTW, I used gist.github.com for this but snipplr is good too. If you know of others, leave a comment!

Tagged , , ,

AS3 number to hex converter

by

I’ve been working on some code for converting colors lately and getting quite into bitwise operations. In one of the demos I was making, I needed a quick and easy way to see the results of a color calculation. Of course, simply tracing a number, even if it’s in the format 0xFF, will result in a digital number string “255″. I’ve always been a little stumped about how to do this calculation but I realized that it’s actually very easy to do with the & and >>> bitwise operators.


getNumberAsHexString(255); // 0xFF
getNumberAsHexString(0xABCDEF); // 0xABCDEF
getNumberAsHexString(0x00FFCC); // 0xFFCC
getNumberAsHexString(0x00FFCC, 6); // 0x00FFCC - Uses 6 as a minimum number of hexits

I’ve added the code to the AS3lib as  as a global function. I may move it to a utility class later on. You can view the source on Google Code.

Incidentally, in a moment of bug-fixing frustration, I found this article on O.C.A.S. which describes how the same functionality is already built into the toString() method! However, the work isn’t completely wasted since my version includes the option to show leading zeroes and adds “0x” at the front of the result.

Tagged , , , , , ,