This site is now just an archive over the rise and fall of Flash. The domain flashmagazine.com is available for sale
Login | Register

Flash Game Development - Overcoming Time / Code Execution Problems

Programming in Flash requires not only programming logic, but also a great deal of creativity. The ability to mold, shape, and twist Actionscript to fit your needs is an essential part to game development.

19. July 2001, By Venkatesh Prabhu Flash game developers will often find that even though their code appears to be logically flawless, there are still a few inconsistencies. One of the reasons is because unlike other standard languages, Flash Actionscript incorporates another important element to the programming framework: Time.

The Role of Time in Flash
Some tend to dismiss the Frame Rate (fps: Frames per second) settings in Flash as an option that simply affects the playback speed of the movie. This assumption may be adequate in simple Flash movies where there isn't too much interactivity, but as soon as intense interactivity becomes involved, Frame Rate becomes much more important. It is now a measure of the speed at which your code is executed.

The importance of this distinction may not become apparent immediately, but consider this oversimplified example.

Now assume that your Frame Rate is 12fps, which means that the code contained in Frame 2 is executed once every 1/12th of a second. In this scenario, if the player pressed the “Left” then “Up” keys within 1/12th of a second, Frame 2 would only register the “Up” key. Flash is unable to keep up with the player's keypresses. Therein lies the problem.

Overcoming the TimeCode Problem
A possible solution would be to increase the Frame Rate so that the Critical Frame (the frame containing the movement code) would be executed more frequently. Doing so would make is harder for someone to press two direction keys faster than the frame loop. At first glance, this seems to solve the problem well enough. Raising the Frame Rate to fps=50 would make it easy for Flash to keep up with the keypresses. Consider however that most computers cannot run complex Flash code at more than 15fps, at most 18fps. So what's the solution then?

The key to solving the Time-Code problem is simply by integrating a “keypress memory” variable that stores keypresses and executes them independently of the user's kepresses. This segment of code illustrates the idea.

The code basically creates an array called key_memory that will store the user's keypresses as letters (“U” for Up, “D” for down, “R” for right, and “L” for left). Every time the user presses one of the directional arrows on the keyboard, a letter is added to the end of the array. Then, when the Critical Frame is executed, the first letter of the array is removed from the array and stored in the variable next. Depending on the value of next, the proper action is executed. When the variable key_memory_length is zero, it means that the key_memory array is empty. The object will then continue to move in the direction specified by the variables move_x and move_y, which basically means that the object will continue moving in the same direction as the last keypress.

Creative Programming
Although this tutorial was focused on a particular problem, it is a prime example of recognizing the limitations of the Flash authoring platform, and creatively working around it. One of the most important characteristics of a well coded Flash game is the fact that the code is woven through the platform, rather than being independent. Having intimate knowledge of the capabilities and limitations of Flash is more important than programming skill.

Creative programming is an art form all on it's own, a concept that many conventional programmers have difficulty mastering. Artists and designers however, seem to pick it up quite easily. Go figure.

 

Get new stories first

Click to follow us on Twitter!

 

Comments

No comments for this page.

Submit a comment

Only registered members can comment. Click here to login or here to register