Showing posts in series: ActionScript 3 Tutorials
Posted June 17, 2011 by Nick Vogt in Programming
Here's a quick description of a unit vector for those that don't know. A unit vector is a vector with a magnitude (distance) of 1. The vector can be pointing in any direction, but the magnitude will always be 1. To get a unit vector from any old vector, you "normalize" that vector, which gives you a new vector with the same direction but a magnitude of 1. Unit vector and normalized vector are often used interchangeably.

Take a look at this graph. Notice that while (1,0) is a unit vector, (1,1) is not. Think about the actual length of the line (it's magnitude) and you'll understand why (1,1) is not a unit vector.


Continue reading...
Posted May 31, 2011 by Nick Vogt in Programming
When working with BitmapData and copyPixels, you'll quickly run into the limitation of not having an alpha attribute with which to change the transparency level of objects. If you're like me, you started out using Sprite and MovieClip in your games and could easily fade-in or fade-out objects using the alpha attribute. Unfortunately, BitmapData does not have the alpha attribute, so how do you adjust transparency dynamically while using copyPixels?

You could create several versions of every image at different transparency levels. This is very tedious and will result in more file size and memory usage. Another way is to use a separate transparent BitmapData mask and apply it to any image that you want transparent.

Continue reading...
Posted May 31, 2011 by Nick Vogt in Programming
Creating semi-transparent BitmapData in ActionScript 3 is done using an ARGB hexadecimal value (32-bit). ARGB stands for Alpha-Red-Green-Blue and is just like an RGB hex value but with an alpha level at the beginning. Due to the alpha level, ARGB hex values are 8 hex digits and not 6. If you aren't familiar with hexadecimal, I suggest reading up on it before continuing.

In the alpha level, 00 represents fully transparent and FF represents fully opaque (visible). For example, to create a fully opaque black, you would use FF000000 (0xFF000000). To create a semi-transparent black, you could use AA000000 (0xAA000000).

Continue reading...
Posted March 10, 2011 by Nick Vogt in Programming
Most Flash Actionscript 3 developers start out by writing code directly into the Actions window (F9), or into a separate .as file and using include to include that code into the Actions window. This works fine for quick, simple programs, but to get the most out of Flash and Actionscript 3, you should learn how to use Packages and Classes.

Programming using classes can be a little odd at first. Don't think of your program as one single block of code that performs a series of actions based on user input. Instead, think of your program as comprised of many separate, interacting objects, that each have their own tasks and can interact with each other.


Continue reading...
Posted January 17, 2011 by Nick Vogt in Programming
Passing variables to a Flash movie that is embedded in a web page can be done using either the query string or FlashVars. The difference between these methods is that using query string will cause browsers to re-download the Flash movie each visit, while FlashVars allows browsers to use the cached version. Depending on the functionality you want, one method may suit you better than the other.

Here is an example of loading variables via FlashVars (use an & to separate each name/value pair):

<object>
<param name="movie" value="movie.swf">
<param name="FlashVars" value="name1=value1&name2=value2">
<embed src="movie.swf" FlashVars="name1=value1&name2=value2">
</object>

And now via the query string:


Continue reading...
Posted November 19, 2010 by Nick Vogt in Programming
In order to use int typecasting in place of Math.floor or Math.ceil, you must first understand exactly what these functions do: Math.floor takes a number and returns the next lowest whole integer. Passing 0.9 will return 0 and -0.9 will return -1. Math.ceil returns the next highest integer, so will return 1 from 0.9 and 0 from -0.9. This is different than truncating, which would return 0 in all cases, and is why typecasting using int (usually used to truncate) wouldn't seem to work at first.

In order to make int typecasting work, you need to add a condition check to see if the number is negative or positive (negative for floor, positive for ceil), and adjust it by 1 if it is. Why do this instead of just using Math.floor or Math.ceil? Because it is faster and you can avoid a function call if desired. Look at these examples:

Using Math.floor like normal requires 691 milliseconds for this run:

var i:int = 0;
var testVar:Number = -82.20035;
var floored:Number;

var time1 = new Date();
for(i = 0; i < 5000000; i ++)
{
  floored = Math.floor(testVar);
}
var time2 = new Date();
trace(time2 - time1);

Now using a custom floor function:


Continue reading...
Posted November 17, 2010 by Nick Vogt in Programming
See this post for a much-improved version

----------------------------

I am working on a simple platformer in Actionscript 3. The player aims with the mouse cursor and when he shoots it calculates the vector using trigonometry and a speed multiplier. This works well as it is very accurate, but I started to wonder if I might not be able to make a more efficient formula, since it doesn't really need to be absolutely accurate.

Here is the trigonometry I used originally (the syntax is Actionscript 3 but it should be pretty self-explanatory for C++, Python, etc programmers):

radians = Math.atan2(eX - sX, eY - sY);
vX = Math.sin(radians) * speed;
vY = Math.cos(radians) * speed;

I am looking to use something that doesn't use any trigonometry functions, just multiplication and division. I came up with this:


Continue reading...
Posted November 17, 2010 by Nick Vogt in Programming
Before doing more complex collision detection in a game, it is useful to first check and make sure a larger bounding area is colliding. This is so that only one calculation needs to be done for each collision detection most of the time, and then further collision detections are only done if objects are close and potentially colliding.

The higher-level bounding collision detection is often done with either a bounding box or a bounding circle (for 2D). The question is, which is faster? With bounding boxes you are checking all four sides of the boxes to see if they overlap. With bounding circles you are checking the distance between two circles and comparing it to their combined radiuses (radii).

Update:

Following a comment from "C coder" below, I found that my previous method for checking bounding box collision was not optimized. Previously I had checked all 4 sides in one "if statement". After further testing, it turns out that checking each side individually is faster. This is because if the first bounding box's right side is less than the other bounding box's left side, there can be no collision and there's no use in checking the other sides after that.

So my original unoptimized performance check was this (Actionscript 3):


Continue reading...
Posted November 17, 2010 by Nick Vogt in Programming
Most game programmers are familiar with the distance (magnitude) equation and how inefficient it is. Using a square root is a costly function that usually can't be afforded many times per frame. Here is a much more efficient distance equation for situations in which you want to check the distance against a threshold (such as determining if two bounding spheres collide).

Here is the original distance formula:

Distance = Sqrt( (x2 - x1)² + (y2 - y1)² )

And here it is expanded into simple multiplication :

Distance * Distance = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)

(For 3D be sure to add the z part of the formula)

Using simple multiplication results in extremely fast calculations. Here are some performance tests I ran in Actionscript 3. Other programming languages should be similar. I started with the traditional distance equation:

dist = Math.sqrt(Math.pow(x2 - x1,2) + Math.pow(y2 - y1,2));
if(dist < minDist)
{
  // Perform action
}

Looping the above code 10,000,000 iterations resulted in a total process time of 4387 milliseconds. Now here is the simple multiplication version:


Continue reading...
Posted November 16, 2010 by Nick Vogt in Programming
In Actionscript 3 there are usually different coding methods to achieve the same output (this can be said of any programming language). Variables can be declared in different ways, math equations can be performed in different ways, you can move code into functions, perform things on single lines or multiple lines, and so on and so forth. While different methods may achieve the same output, certain methods are often faster than others. This post is dedicated to finding as many performance tweaks and enhancements as I can, and I will update it as I find more.

Methodology: For each test, I use the following loop to determine the relative duration of processing time. I use 500,000 iterations (or more in some cases) so that small differences in performance between code are exacerbated. Usually, any variables that I use in the test are initialized before the timed loop, to eliminate their initialization time from the test. That is unless I'm testing the performance including initialization. I also run the test multiple times to get the average time.

var i:int;
var time1 = getTimer();
for(i = 0; i < 500000; i ++)
{
   // Code to test
}
var time2 = getTimer();
trace(time2 - time1);


Continue reading...
Features
Free Web MP3 PlayerComputer Build GuidePHP Beginner Tutorials
Post Series
ActionScript 3 TutorialsHard Drive Cost Charts
Popular Tags
actionscriptajaxcall of dutycrysisebayfacebookgooglejavascriptminecraftneweggphprageskyrimtutorialyoutube


H3XED © 2012 Nick Vogt | Web Design
Wednesday, February 22, 2012 | Privacy Policy | Disclosure Policy | Contact