Monday, December 27, 2010

Position and Speed Variables

Position and Speed Variables:

The impression of spatial continuity – that an object exists at some location, and stays there until it moves to an adjacent position – is not assumed by a computer. This seemingly natural behavior has to be described in code by the programmer.

Fortunately, there is a pattern for this.

It can seem obtuse the first time or two it’s seen – it did for me – though once used a few times its workings become clear.

Side Note Regarding Flash Programming

ActionScript 3 supports a variation of this, by drawing to BitmapData as the Everyone’s Platformer engine does. Otherwise, the Sprite and addChild system commonly used in AS3 (example) bypasses redraw details described in this article.

Overview

What follows assumes very little prior experience, so there’s plenty of text in an effort to be thorough. There are only a few points, though:

  1. Create number variables to store the position of the thing to be drawn.

  2. Use those variables as coordinates when drawing the image for that thing.

  3. Change those variables to move it.

  4. Create additional number variables – to treat as speed for horizontal and vertical movement – and add those to the position variables every frame [...]

No comments:

Post a Comment