Thank you for visiting Blazing Games

Crashing Into Walls

The technique used for handling the level and collisions was a different technique then what I would do now if I was creating the game. Still, for smaller levels, the technique used in this game is very simple and quick to implement so may be perfect for many other games, especially if they are built out of smaller rooms.

The actual walls that make up the game world are a separate layer. In fact, the world consists of three layers. A floor layer, the wall layer, and the player. The floor and wall are linked together so they move at the same time. In fact, the space ship is not what is moving when you are playing the game, but instead the world is moved around the player.

The direction of movement is based on combining accelerations to form a vector of motion. This may sound complicated, but it is simply adding the existing horizontal and vertical speeds with the acceleration speed of the direction the player is currently facing.

       public function accelerate(delta:Number)
       {
           var rad:Number = _spaceship_movie.rotation * Math.PI/180;

           _xspeed = Math.max(-MAX_SPEED,
                   Math.min(MAX_SPEED,
                   _xspeed + delta * ACCELERATION * Math.sin(rad)));
           _yspeed = Math.max(-MAX_SPEED,
                   Math.min(MAX_SPEED,
                   _yspeed + delta * ACCELERATION * -Math.cos(rad)));
           // trace ("speed: " + _xspeed + "," + _yspeed);
       }

The motion information is used to move the world around the player. To determine a collision, a very simply center point of the player is used and a point collision test with the level map (called the _hitmap_movie) is performed. Had I not been in such a rush, multiple points from the ship would have been tested so that more accurate collisions would be detected. Finally, determining if the player has landed is simply a matter of seeing if the player is within the landing zone and the speed of the player is low enough.

       public function updatePosition(delta)
       {
           _hitmap_movie.x -= _xspeed * delta;
           _hitmap_movie.y -= _yspeed * delta;
           _landingbay_movie.x = _hitmap_movie.x;
           _landingbay_movie.y = _hitmap_movie.y;
           if (_hitmap_movie.hitTestPoint(320,240,true))
               collision();
           if (_landingbay_movie._target_movie.hitTestPoint(320,240,true)) {
               // trace("Inside landing target");
               if ( (Math.abs(_xspeed) < 5) && (Math.abs(_yspeed) < 5) )
               {
                   _xspeed = 0;
                   _yspeed = 0;
                   if (!_landed) {
                       _landed = true;
                       trace ("Win event being sent");
                       var evnt:Event = new Event(WIN_GAME_EVENT);
                       this.dispatchEvent(evnt);
                   }
               }
           }
       }

For space games, the motion routines are quite handy and could be used in a lot of other games. The big thing that should be done is to revamp the collision detection code. Simply having multiple points being sampled would have made the collision detection significantly better and in hind-site would not have been that time consuming or difficult to implement. More precise per-pixel techniques could also be used but for most games this would probably be overkill.

Previous page
Chapter 38 Page 5

About - Privacy Policy - Contact - Links - FAQ
Copyright © 2011 Blazing Games Inc. All Rights Reserved