Archive for category JavaScript

Faster isNaN()

Tags: ,

You cannot directly check if a value is NaN by comparing with it. AS3, AS2, and JavaScript therefore provide a useful isNaN() function to do this very check. However, it is very slow. Today I’ll show you a workaround that results in a faster isNaN(): (UPDATE: see the definitive article on isNaN for much more!)

Read the rest of this article »

22 Comments

Flexible If Syntax

Tags: , , ,

This article is sort of a follow-up to my article on Flexible Loop Syntax. This was reported to my by a coworker who spotted the anomaly. I guess he had done with if the same sort of thing that I had done with for. Read on for a little insight into how the comma operator interacts with the if statement.

Read the rest of this article »

5 Comments

Repeatable Random

Tags:

Everybody knows about Math.random(), and for good reason. It’s pretty much the way to get random numbers in AS3, AS2, and JavaScript other than bizarre alternatives like AS3’s BitmapData.noise(). However, it has one critical problem that arises when you want to repeat a certain test or prevent game cheaters from exploiting the randomizer until they get an “easy” setup or desirable outcome. This problem is the lack of repeatability.

Read the rest of this article »

10 Comments

Inline Math.ceil()

Tags: , ,

Math.ceil() is a common, mundane function that you likely call all the time. I know I do. If performance gets to be important and you have a Math.ceil() in some inner loop or frequently called function, consider inlining it. Below I’ll show you how and provide a test app showing you just how much CPU time you’ll save.

Read the rest of this article »

11 Comments

With Blocks

Tags: , ,

While plainly documented by Adobe in the Flash 10 AS3 Docs, it seems as though few programmers know about the with statement. I don’t use them much personally, but when a coworker came across one in my code recently and was puzzled, I figured I would write a quick article to cover their usage.

Read the rest of this article »

4 Comments

Function Variables

Tags: ,

AS3, AS2, and JavaScript have some strange rules regarding the initialization of variables. These are shocking and perhaps ridiculous to users of C and Java. This article covers one particularly insane quirk.

Read the rest of this article »

6 Comments

AS3 vs. JavaScript Performance Test

Tags:

With the announcement of WebGL last Friday I started to wonder about the relative performance of AS3 and JavaScript. This is what I’ve found.

Read the rest of this article »

9 Comments

Fun With Dynamic Functions

Tags:

Dynamic functions are a very useful feature of AS3 and JavaScript. Closures constantly come in handy for cleaner, more powerful code. Here’s a feature you might not know about them though.

Read the rest of this article »

No Comments

Details of NaN

Tags:

The constant NaN (not a number) can come up in a lot of situations. In AS3 it’s the default value of a Number field, it’s the result of division by zero in AS2, AS3, and JavaScript, and you can get it in a number of other ways. This article is about the reality of dealing with NaN.

Read the rest of this article »

2 Comments

Uses for arguments

Tags:

The arguments magic variable has been around since the AS2 days. It used to be more useful than it is in AS3, but don’t overlook it completely!

Read the rest of this article »

2 Comments