Article

Build a Shoot-em-up Game in Flash MX

Page: 1 2 3 4

ActionScript for the Targets

Still with me? Your movie isn't exactly going to inspire as it is now. What we need to do is to start adding some actions to the targets.

The ActionScript is pretty much the same for all targets, so you can simply cut and paste. Just be aware that, when using the code for 'target2' or 'target3', you will need to find and replace 'target1' with 'target2' or 'target3'. This is all highlighted in the code to help you. You will also need to update the 'activeTarget' variable.

Select the 'Target1' instance of TargetMC on the main stage by left-clicking on it, and open up the actions panel (Window Menu-Actions or (F9 [pc]). Copy the following code into it.

(AS for individual targets)    
   
onClipEvent(mouseDown) {    
hitsNeeded = 5; //hits needed to destroy target—this    
     //can be any number you want    
   
     
//CHANGE target1.hitTest, target1Hits and activeTarget to read target2 //or target3 for other targets and target1 to read 2 or 3 as in the //array we set up.    
 if(_root.target1.hitTest(_root._xmouse, _root._ymouse, true) && (_root.target1Hits<hitsNeeded) && (_root.activeTarget == \\\\\"target1\\\\\")) {    
 //OK From the top on this one:    
     //Using the AS hitTest() method    
     //If the specified MC is clicked on and    
     //if the hits needed on this MC have not    
     //reached the maximum and    
     //*if* it is the active target (activeTarget              
       
//CHANGE target1Hits to read target2Hits or target3Hits for other //targets    
   _root.target1Hits++; //increase MC hits variable by 1    
   _root.directHits++; //increase direct hits variable    
   _root.hits.text = _root.directHits+\\\\\" of \\\\\"+hitsNeeded+\\\\\" needed.\\\\\";    //display current hits for target in a text    
     //field on the main stage    
   
 highScore = _root.highScore;    
 highScore = highScore + 10; //increase high score by                 //however many points you                 //want for a hit.    
   
 _root.info.text = highScore;    
 _root.highScore = highScore; //update the variable on    
               //the main stage    
   
//CHANGE target1Hits to target2Hits or target3Hits for other //targets    
 if(_root.target1Hits == hitsNeeded) { //if hits needed total    
               //is reached    
_root.directHits = 0; //reset direct hits counter for future          
   //targets    
   
 //CHANGE to target2Image or target3Image for other targets    
 setProperty(_root.target1Image, _visible, false);    
//hide target    
   
 //CHANGE to target2d or target3d for other targets    
    setProperty(_root.target1d, _visible, true);    
     //Show the target destroyed image    
        //Check if array is now empty    
     //If so, endgame    
   
   
 n = _root.targets.length;    
 if(n == 0) {    
   _root.gotoAndPlay(2);    
 }                    
   
 //If it wasn't empty…    
 //generate a random target    
     
 n = _root.targets.length //get the current array    
//length    
 ran = random(n); //get random number location to    
         //select from array    
   
 target = _root.targets[ran]; //create target variable    
   
 _root.activeTarget = target; //set active target    
   
 setProperty(\\\\\"_root.\\\\\"+target+\\\\\"Image\\\\\", _visible, true);    
               //show target    
   
   
 _root.targets.splice(ran, 1); //remove the random    
             //element from array    
                //so that target cannot    
             //be picked again in    
             //this game    
   
 _root.hits.text = \\\\\"target destroyed!\\\\\"; //update the    
                   //text field    
   }    
}    
}    
   
//-------------------------------------------------------//

Only 2 more things to do before you're ready to test the game and add your own elements! As I mentioned at the start of the tutorial, you'll need 2 further images for each target, to show when each is active and when it is destroyed. For this tutorial, we're just going to use simple boxes as previously described.

Creating the Active Target Images

This one's nice and simple! Just create a new movie clip called 'Active', then create a red rectangle that's big enough to cover the target with a transparent fill.

Create above the Target layer a new layer called 'Active Targets', and place three instances of this movie clip over the top of your targets in frame 1. In the property inspector, name the instances, 'target1Image', 'target2Image' and 'target3Image', respectively.

Creating the Target Destroyed Images

This is much the same as the above, really. Create a new movie clip called 'Destroyed', then create a new layer above the last one, called 'Destroyed Targets'. Add 3 instances, called 'target1d', 'target2d' and 'target3d', respectively.

That's it! Except...

If you've got this far through the tutorial, you should have all the elements in place to start playing your game. You may want to add one or two other elements, such as a "play again" button, which can be as simple as using a standard button from the common libraries bundled with Flash (Window-Common Libraries-Buttons). Put the button on frame 2, then select it and insert the following into the actions panel:

on (press) {    
 prevFrame();    
}

Along the way, you've probably thought of things you can add to the game -- what you've learnt here will only be the start. Happy gaming!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: