A Key Object Demo

[ LiB ]

I created demo GDA_PROG8.6.fla to show off the Key object in action, so you can get a feel for what it's like create a main player in your game. The airtank in the demo has the capability to move forward, backward, left, and right. I even added some scale modifications in there so it would seem as if the aircraft was really flying side-to-side.

See Figure 8.6 for a screen shot of the demo.

Figure 8.6. The airtank

graphic/08fig06.gif


As you will see from the listing, it is very easy to add features and other goodies to the programyou can even take the code and modify it for yourself.

 // Game Development with ActionScript // By Lewis Moronta (c) 2003 // This demo shows off the key object // that's built in to Flash. // This onEnterFrame handler // polls for the keys to see // which ones are being pressed. onEnterFrame = function() {   // Make sure the airtank is   // always resetting to its   // original position.   airtank._xscale = 100;   // Let's move the tank upward.   if (Key.isDown(Key.UP))   {     airtank._y += -10;     airtank._xscale = 100;   }  // Let's move the tank down. if (Key.isDown(Key.DOWN)) {   airtank._y += 5;   airtank._xscale = 100; } // Let's move to the right. if (Key.isDown(Key.RIGHT)) {   airtank._x += 8;   // This creates the illusion   // of the aircraft moving right   airtank._xscale = 75; } // Let's move to the left. if (Key.isDown(Key.LEFT)) {   airtank._x += -8;   airtank._xscale = 75; } // This section prevents the object // from moving out of the borders of // the screen. if (airtank._x < 0)   airtank._x = 0; if (airtank._x > 550-airtank._width)   airtank._x = 550-airtank._width; if (airtank._y < 0)   airtank._y = 0; if (airtank._y > 400-airtank._height)   airtank._y = 400-airtank._height; }; 

As you have been reading the book straight through, you should have no problem understanding the code.

The first line, airtank._xscale = 100; , could be confusing if you were not sure what the program was doingso why is it there? I originally set up the program to move the object in four directionsleft, right, up, and down. As the object is an aircraft, it didn't look natural for it just to move left and right without tilting. I resolved the problem in a very quick and effective wayperhaps not the best way, as it still looks a bit cheesy, but it works for the demo.

When the user presses either the Left or Right arrow on the keyboard, the graphic shrinks to 75 percent and creates the illusion of actually tilting to one side. As the airtank is symmetrical, the tilting works for both sides.

So back to this first line:

 // Make sure the airtank is // always resetting to its // original position. airtank._xscale = 100; 

If the object starts out at 100 percent, then why do I need this assignment? Well, it turns out that when the user presses either the Left or Right arrowand then stops pressing in that directionthe aircraft stays in the direction last pressed. One way to reset the aircraft to its original position is by making sure it always goes back to 100 percentand it will always look normal when left alone.

NOTE

NOTE

Keep in mind that you are in a block of code that is executed on every frame; if you have problems visualiz ing something, try to play out a few frames before moving on.

The if structure used in the demo is new, so let's examine it:

 // Let's move the tank upward. if (Key.isDown(Key.UP)) {   airtank._y += -10;   airtank._xscale = 100; } 

The Key.isDown method returns true if the Up key is being pressed, and if the Up key is being pressed, then the if block executes. So what's happening in the block? Well, when the user presses the Up key, the airtank moves up by 10 pixels because 10 is being subtracted from the _y property of airtank . And to make sure that the airtank has the proper orientation, make sure that the scale is at 100 percent.

As the user plays around with the controls, he will eventually press Down and the aircraft will move down. Amazing isn't it? What's the secret behind this magic?

 // Let's move the tank down. if (Key.isDown(Key.DOWN)) {   airtank._y += 5;   airtank._xscale = 100; } 

This is very similar to the last piece and not too difficult to understand. The if statement tests to see if Key.Down is being pressed. If the test results to true, then the airtank is moved down by adding 5 to its _y property.

Now let's see what happens when the user presses Right:

 // Let's move to the right. if (Key.isDown(Key.RIGHT)) {   airtank._x += 8;   // This creates the illusion   // of the aircraft moving right   airtank._xscale = 75; } 

Just as you guessed, 8 is added to its _x property, causing the tank to move 8 pixels to the right. The _xscale is brought down to 75 percent in order to create the tilting illusion I mentioned previously.

Moving left is not much different from moving right. All you have to do to activate left movement is press the Left arrow key on your keyboard. The scale is also adjusted to 75 percent for the same visual reasons.

 // Let's move to the left. if (Key.isDown(Key.LEFT)) {   airtank._x += -8;   airtank._xscale = 75; } 

One of the things I did to this program was the classic "prevent the object from going through the borders of the screen" routine. The following demonstrates how to prevent the object from going off the left side:

 // This section prevents the object // from moving out of the borders of // the screen. if (airtank._x < 0)   airtank._x = 0; 

This checks to see if the airtank._x property is less than 0, and if it is, it will keep it at 0.

The next thing I did was make sure that the tank is within the right side of the screen:

 if (airtank._x > 550-airtank._width)   airtank._x = 550-airtank._width; 

The width of my movie is 550 pixels, and subtracting my object's width ensures that the airtank will remain visible.

Now that I handled the horizontal motion, it is time to take care of the vertical motion:

 if (airtank._y < 0)   airtank._y = 0; 

With this if statement, I'm preventing the airtank from moving off the top of the screen. If the airtank is off the top, I adjust it back to 0.

And finally, I protect the aircraft from going off the bottom of the screen:

 if (airtank._y > 400-airtank._height)     airtank._y = 400-airtank._height; } 

If its _y value is passed 400 (minus its height) then it's too far off the edge, and I must adjust it (or keep it at 400 minus its height).

Have a happy flight!

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net