# 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 🙂

``` 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; } } } ```