Filed under Snippets

Grid generator for <canvas>

by

Here’s a snippet of code that creates a grid in the <canvas> tag using JavaScript. It’s just a quick little experiment but I thought I’d share it with you.

I added the draw command to a loop that moves around the grid’s starting point just for fun.

See it in action.

SnippetSaturday: Number to String

by

This is some code I wrote a long time ago back when AS3 was just starting to show up on the scene (I updated it some to work on its own and to use crazy nested Vectors). I’m sure it’s not terribly useful, it probably could be optimized quite a lot, but hey, it is what it is. I think it could be informative as one crazy possible solution to a kind of difficult problem. Comments away!

numberToString – wonderfl build flash online

Tagged , , ,

Snippet Saturday: quick random choice

by

Today’s Snippet Saturday (actually, Sunday) is a quick shortcut for choosing one of several strings, objects, etc. randomly.

Now, I wouldn’t really recommend using this code in a project. There are ways to do the same thing that are much more readable and less error prone. Instead, I thought it was an interesting experiment to show off some of how AS3′s syntax works for those of you who may not have seen something like this.

What’s happening here? Let’s break it down.

  • ["Alpha", "Bravo", "Charlie"] – Here we are instantiating a new array and populating it with three strings. This is essentially the same as:
    var a:Array = new Array();
    a[0] = "Alpha";
    a[1] = "Bravo";
    a[2] = "Charlie";
    
  • [...] – next is another pair of square brackets. This is an array access operator. In other words, everything between the two brackets will be evaluated as the index of the array to retrieve.
  • int(...) – This is an explicit type-cast to an int. That means that everything inside those parentheses is evaluated and flash attempts to convert from whatever data type it is to an integer. In the case of decimal numbers, they are rounded down so this is similar to using Math.floor().
  • Math.random() * 3 – random() of course produces a random floating point (decimal) number between 0.0 and (almost but not quite) 1.0. Multiplying that number by 3 (the length of the array) produces a number between 0.0 and 3.0 (technically, between 0 and 2.99999999etc).

The result, an array is created with three strings, a random number between 0.0 and (almost) 3.0 is generated, it is rounded down to an int between 0 and 2, that number is used as the index of the array to look up. The result will be randomly one of the three strings.

I hope you found this interesting!

Tagged ,

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 , , ,