Asteroids 2.03: New Asteroid Shapes

So I didn’t expand on the interaction between the ship and the asteroids. Instead, I tooled around all weekend trying to create randomly shaped polygons to use for the asteroids. This is something I wanted to do in the original build and never took the time to concentrate on the problem.

I read several articles online. Looked at academic research on a [Random Polygon Generator](http://www.cosy.sbg.ac.at/~held/projects/rpg/rpg.html). The algorithms were mind boggling. I e-mailed some very smart ActionScript programmers for some help. None came (at least by the time of this writing).

Then it occurred to me… can’t I just rotate similar-sized squares and triangles appearing randomly to produce a randomly shaped polygon? Bet your ass I could 🙂

[Download the source files](http://flashforlearning.com/files/Asteroids_03.zip).

So this release marks two changes that are related. One is the obvious — randomly shaped asteroids for a more unique experience.

The other is that I’ve gone back to the Drawing API for the asteroids, so the Asteroids are now (almost) entirely ActionScript. I’ll move the ship back under its controls in a future release. In fact, getting this to work the way I did reveals that I’ll have to set up a “Game” class to generate the movieclips that will bind to the Asteroid and the Ship classes — and in that class, not only should I be able to move out of having any movieclips in the library — I should be able to incorporate the collision detection between the asteroids and the ship.

I think I’m getting classes now. Here’s the updated code for the Asteroid class…

class Asteroid extends MovieClip
{
	// velocity value for the Asteroid's x- and y- movement
	private var xv:Number;
	private var yv:Number;

	// rotation of the Asteroid, so it looks like it drifts
	// in outer space
	private var r:Number;

	private var AsteroidColor:Number = 0x00CC00;

	// this is the constructor function, which initializes
	// each Asteroid instance.  It must be a public function
	// or it won't be able to be accessed by the Flash movie.
	private function Asteroid ()
	{
		DrawAsteroid( this );

		// shorten up some Math functions
		var mr = Math.random;
		var mf = Math.floor;

		// set the velocity for the Asteroid
		xv = mf( mr() * 6 ) - 3;
		yv = mf( mr() * 6 ) - 3;

		// set the Asteroid's original rotation.
		r = mf( mr() * 360 ) - 180;
		_rotation = r;

	}

	// Each Asteroid will now encapsulates, or
	// determintes its own movement.  This will
	// improve performance over the original
	// game, where one onEnterFrame on the _root
	// controlled everything.
	private function onEnterFrame():Void
	{
		// This series of if statements simply guides the placement
		// of an asteroid as it floats near the borders of the movie.
		if ( _x < = 10 )
		{
			_x += 390;
		}
		if ( _x >= 390 )
		{
			_x -= 390;
		}
		if ( _y < = 10 )
		{
			_y += 140;
		}
		if ( _y >= 140 )
		{
			_y -= 140;
		}

		// Keep the Asteroid moving per its velocity
		_x += xv;
		_y += yv;

		// Keep the Asteroid rotating on its axis, as if it were floating
		// in space.
		_rotation += r / 60;
	}

	private function DrawRectangle ( shape )
	{
		shape.beginFill ( AsteroidColor, 100 );
		shape.lineTo ( 30, 0 );
		shape.lineTo ( 30, -30 );
		shape.lineTo ( 0, -30 );
		shape.lineTo ( 0, 0 );
	}

	// DrawRectangle();

	private function DrawTriangle ( shape )
	{
		shape.beginFill ( AsteroidColor, 100 );
		shape.lineTo ( 30, 0 );
		shape.lineTo ( 0, -30 );
		shape.lineTo ( 0, 0 );
	}


	private function DrawAsteroid( mc )
	{
		var degrees:Number = 360;

		while ( degrees -= 45 )
		{
			var i:Number = Math.floor( Math.random() * 2);
			var shape:MovieClip = this.createEmptyMovieClip( "shape" + degrees, this.getNextHighestDepth() );
			(i) ?  DrawRectangle( shape ) : DrawTriangle( shape );
			shape._rotation = degrees;
		}
	}
}

4 replies on “Asteroids 2.03: New Asteroid Shapes”