|
||||||||||||||||
. . . . . |
Flash MX - Anatomy of a GameIn this tutorial I will take you throught the complete process of design, Analysis, and coding
a game in Flash MX
For starters we will keep this really simple. We need two fighters. Call them B1 and B2 for now.
And we will need them to move left and right and kick and punch.
For now there will be no vertical movement.
But we will keep it in mind for later development.
These use cases can be translated into some sort of Requirements Doc.
A cursory attempt at which will follow.
Flash Tutorials in Video Format -
Watch them now at LearnFlash.com
Repeat the requirement for B2 , which is the same. What else will we need? We will definitely need an
So lets start up Flash MX and start ! First thing we need will be an intro page.
On frame 1 , make a background graphic on layer1 , do a title and some Text about gameplay on layer 2.
Do it in whatever style you like. Do a Start button and put it on a "Buttons" layer. You can do the code for the start button now. It is really simple. All you want to do is
go to frame 2. Select the start button, Hit the F9 key to open the actions window.
Put this code in the Actions window.
on(release) {
_root.gotoAndStop(2);
}
But wait, I forgot to put a stop() in frame 1. Make a new layer and call it "Control".
Click on frame 1 in that layer and put this code in the actions window. Stop(); Go to frame 2 in the "Background layer" and hit the F6 key to make a new keyframe. Test your movie. It should go to frame 2 and stay there and the background will be visible but no text or start button. Start animating your characters. For my characters I scanned in a real banana and traced around the main lines of the outlines and folds etc. Then just painted on a face and arms and legs. Next start animating the neutral B1 and B2. I just made their legs and arms wave around a little and a blink with the eyes. It's probably too much but it looks so silly. And that is what I am aiming for. Next start animating the walks left and right.
Animate the characters in place and just do a walk loop that goes for 25 frames.
Or better still do a martial arts sideways movement. Just like Bruce Lee-Banana.
We should have movies now for both characters as neutral and move right and left Animate a punch and a kick for each character. All up we should have 10 movies.
Add a new layer to B1main and B2main MovieClips and call it actions. Put a keyframe in each frame
and put the following code in each frame Stop();
Now we have all the graphical and movie elements to the game, so lets start coding it.
We need to activate the B1 and B2 movieclips to move left and right and punch and kick when we press
certain keys. (W A S D) and (I J K L).
onClipEvent(keyDown) {
// W key for punch
if (Key.isDown(87)) {
this.gotoAndStop(4);
// S key for kick
} if (Key.isDown(83)) {
this.gotoAndStop(5);
// D key for Move right
} if (Key.isDown(68)) {
this.gotoAndStop(2);
// A key for move left
} if (Key.isDown(65)) {
this.gotoAndStop(3);
}
}
This will need some explanation. The Ascii keycodes for WASD are the ones used above( 87, 83 etc).
When a key is pressed this checks if the WASD keys are down (Key.isDown) and tells the B1main movie to go to frame
4 for example which has the punch movieClip. And if Key D is hit the move right movie is played on frame 2.
onClipEvent(keyUp){
this.gotoAndStop(1);
}
We need some collision testing for the kicks and punches, also we need to adjust the scores for each player, and we need some sound effects both for kicks and collision. Kung Fu movies use bits of wood wacking for punching noises. Try something creative. Or better still funny and ridiculous. Well lets do what I should have done first and that is to write an algorithm. An algorithm is
computer code written in plain English. Let's do it properly for a kick : //check if key S is pressed // go to frame 5 // check for collision // if yes, play a sound // and add 2 to score // check if score is greater than winningscore // if yes, go to winners page // end Write that in the actions window for b1main where you did the last one. And enter the code after
each comment. like so,
// check if key S is pressed
if (Key.isDown(83))
{
// go to frame 5
this.gotoAndStop(5);
// check for collision
if(_root.b1main.b1kick, hitTest(_root.b2main))
{
// just do a trace to check its working
trace("kick hits b2");
// play a sound, (we'll do this later)
//add 2 to score
b1Score += 2;
// check if score is greater than winning score
if(b1Score >= winningScore)
{
_root.gotoAndStop(3);
}
}
}
You have a go at the punch for b1 doing it using algorithm and then coding after each
commented line. And then try it for B2. It should be exactly the same , just change b2 for b1.
We have to set some score variables . So in frame 1 ,
click the control layer and add this code to the actions window: b1Score = 0; b2Score = 0; winningScore = 100;
Add an empty frame to the main timeline and just leave it for now. That will be the winners page. We'll make some graphics later. We also need to display the scores on the second frame. So just keep that in mind. We'll do it later.
Next step is to import some sounds. Import your sounds to the library.
Make a folder and name it sounds and put them in that. To play the sound you first have to set up a Sound object and then attach the ID Name of the sound that we did
in the last part (kick1, kick2 etc). So, click on the first frame of the control layer and add the
following code :
kick1Sound = new Sound();
kick1Sound.attachSound("kick1");
Then all we have to do is start the sound in the line we left for later in the b1 movie actionscript.
So add this line after the line " // play sound " // play a sound kick1Sound.start(); Create 2 more frames. In frame 3 write " B1 is the winner" or whatever, and create a restart button (to go back to the first frame) with the code :
on(release) {
_root.gotoAndStop(1);
}
In frame 4 write "B2 is the winner " and use the same button with the same code to restart.
We are now ready to go back and finish off our code for the B1 and B2 movieClips. Click on the B1 character and put in this final code in the action window :
onClipEvent(load){
kick1Sound = new Sound();
punch1Sound = new Sound();
kick1Sound.attachSound("kick1");
punch1Sound.attachSound("punch1");
}
onClipEvent(keyDown) {
if (Key.isDown(87))
{
this.gotoAndStop(4);
// TEST for collision
if(_root.b1main.b1punch, hitTest(_root.b2main)){
kick1Sound.start(0,1);
_root.b1Score +=1;
updateAfterEvent();
}
}
// check if key S is pressed
if (Key.isDown(83))
{
this.gotoAndStop(5);
// TEST for collision
if(_root.b1main.b1punch, hitTest(_root.b2main)){
kick1Sound.start(0,1);
_root.b1Score +=2;
updateAfterEvent();
}
}
if (Key.isDown(68)) {
this.gotoAndStop(2);
this._x += 20;
} if (Key.isDown(65)) {
this.gotoAndStop(3);
this._x -=20;
}
if(_root.b1Score >= 100)
{
_root.gotoAndStop(3);
}
}
onClipEvent(keyUp){
this.gotoAndStop(1);
}
The code for B2 will be the same except that the b1 will be changed to b2 and kick1, punch1 changed to punch2, kick2 etc. AND you will have to find the keycodes for the letters I , J , K , L . You have a look for them yourself. Each letter on the keyboard has an ASCII code. A is 65, B is 66 , C is 67 - etc etc. Addendum - Characters Jump - 28th May 2003
I have made the characters jump thanks to some code by tonypa from flashkit forum.
onClipEvent(load){
kick1Sound = new Sound();
punch1Sound = new Sound();
kick1Sound.attachSound("kick1");
punch1Sound.attachSound("punch1");
// jumping variables
startspeed = 10;
gravity = 9.8;
jumping = 0;
}
The jumping variables startspeed, gravity and boolean flag "jumping" have been initialised. These will affect
the vertical movement component _y.
The enterframe code has been changed to what is shown below . For the left character, B1, the key "Q" is
used for jumping. You can play around with the startspeed and gravity values to get a different jump.
onClipEvent(enterFrame) {
if (Key.isDown(87))
{
this.gotoAndStop(4);
// TEST for collision
if(_root.b1main.b1punch, hitTest(_root.b2main)){
kick1Sound.start(0,1);
_root.b1Score +=1;
updateAfterEvent();
}
}
// check if key S is pressed
if (Key.isDown(83))
{
this.gotoAndStop(5);
// TEST for collision
if(_root.b1main.b1punch, hitTest(_root.b2main)){
kick1Sound.start(0,1);
_root.b1Score +=2;
updateAfterEvent();
}
}
if (Key.isDown(68)) {
this.gotoAndStop(2);
this._x += 20;
} if (Key.isDown(65)) {
this.gotoAndStop(3);
this._x -=20;
}
// jumping key is "Q"
if (Key.isDown(81) and jumping==0) {
jumping = 1;
starttime = getTimer()/1000;
startpos = _y;
}
if ( jumping==1) {
time = getTimer()/1000-starttime;
_y = _y-(startspeed-gravity*time);
if (_y>startpos) {
_y = startpos;
jumping = 0;
}
}
if(_root.b1Score >= 100)
{
_root.gotoAndStop(3);
}
}
onClipEvent(keyUp){
this.gotoAndStop(1);
}
For the other character(B2), just make the corresponding changes and use the "U" key(85) for the jump... That should be about it for this game. There are a few modifications that you could include.
|
|
||||||||||||||
|
| Home | Flash MX | Actionscript 2.0 | Flash 3D | Flash 8 | Flash Database | Flash Mobile | Flash CS3 | Java For Kids | Video Course | General Video | Photoshop | Web Design | Digital Photography | Forum | Games | free backgrounds | Resume | Flash Animations | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | store | advertisers | . . |
||||||||||||||||