A Flash Developer Resource Site














Internet Commerce

Partners & Affiliates














Developer Channel

internet.com


Featured Flash FLA
Gallery Downloads 11337 Flash Movies | 1 New Flash Movies Added
What's New | Top 100

Featured FLA

» Author: Bugra Ozden
» Title: Skatalog v9 - product catalog
» Description: Create your product catalog easly and publish on your website or Create your image gallery, documents list, portfolio. Fully XML Driven
» More by Bugra Ozden


Random FLAs | Add Flash Movie
Featured Flash Site
Gallery Downloads 5828 Flash Sites | 0 New Flash Links
What's New | Top 100 Flash Site

Featured Site

» Posted in the Flash Kit Links section
» Title: Creative DW Image Show PRO
» Description: Creative DW Image Show PRO is a Dreamweaver extension which enables the user to create multimedia presentations. It combines the features of the popular Creative DW Image Show with the ability to add professional text effects to slides (similar to After Effects). The product is very customizable: the user can choose the duration of the transition effects, the slide motion start and end position, zoom and panning type for both images and texts.


Random Links | Add your own Flash Related Links
Flash Tutorials 1280 Tutorials 7 New Tutorials Added!
What's New | Top100

» Make a Flash Slide Show Screen Saver
» Simple flash making tutorial for thanksgiving
» Create flash banner for website
» Create xml slideshow with free template
» How to Insert a Multilingual Subtitle Into Your Flash Video Studio
» How to Create Cool Halloween Slideshow
» Debugging flash using the Firebug console
» Create Flash Slideshow on Blogger
» FLASH TRICKS IN WEB ADVERTISING: FLASH BANNERS
» Unknown Tag: Title10
Random Tutorial | Add Site

Trading Customer Accounting (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume


Tutorials Home What's New Top Rated Submit myTutes Random!

Search Tutorials


Tutorials Tutorials » Games

Categories Building Games in Flash5 Part1: Player Movement and Fire
Author: David Doull | Website: http://www.artifactinteractive.com.au |

 
Page 5
«prev 1 2 3 4 5

Now lets add in some weapon fire.

What we will do is create a movie clip that looks like spaceship laser fire. Whenever the Ctrl key is pressed we will duplicate this movieclip, set its location to the same location as the spaceship and then, within a loop, increase the movieclips x-position until it goes off the screen.

First up we need to draw some laser fire. On a new layer draw some laser fire, a couple of blue lines should do the job, select the laser graphic, convert to a MovieClip (F8) and set the instance name to laser.

 

Now we need to add some new code to the spaceship clip event code.
Select the spaceship movie clip and choose Window>Actions to open up the Actionscript window. Add in the lines _root.laser._visible=false; and laserCounter++; so that the onClipEvent(load) code is now:

onClipEvent(load){
 moveSpeed=10;
 _root.laser._visible=false;
 laserCounter=1;
}

The first of these two new lines makes the laser invisible when the spaceship first loads. Why? because this will be our source laser clip. Whenever the user presses the Ctrl key we will duplicate this movie clip to create a new laser fire. Think of it as the mold from which we produce all the laser fire, this is the original movie clip and what gets fired across the screen every time Ctrl is pressed is a 'duplicate'.

The second line is just setting a counter variable equal to one. We'll use this variable next.

Right, we've mentioned that the laser will be fired when the Ctrl key is pressed, now let's add in this code into the spaceship clipEvent. The following code goes directly after the line
onClipEvent (enterFrame) { in the actions window for the spaceship.

if (Key.isDown(Key.CONTROL) {
laserCounter++; _root.laser.duplicateMovieClip( "laser"+laserCounter, laserCounter ); _root["laser"+laserCounter]._visible=true;
}

This code is run when the Ctrl key is pressed. The code does three things. It adds one to the laserCounter variable, it duplicates the laser movie clip and makes the new, duplicated clip, visible.

The laserCounter++; bit adds one to the variable laserCounter. NB: ++ means 'increase by one', so the line is the same as laserCounter=laserCounter+1.

The second line duplicates the movie clip and gives this new duplicated clip the name 'laser' plus the value of the laser counter variable and sets the clips depth to the value of the laser count variable. So if _root.laser.count is equal to 4 then the new movie clip will have the name laser4 and the depth of 4.

The third line of code within the curly brackets makes the new duplicated clip visible. You may not have seen this syntax before. It uses array style referencing for objects and is especially useful if you want to reference a dynamically created object name. It's kinda similar to how eval was used back in Flash 4. For example: _root.laser._visible can also be written _root["laser"]._visible
The normal dot syntax wont let you write _root."laser"+laserCounter._visible - its incorrect. But you can do it using array notation, using square brackets!

 

We are almost at the end now, we just need to add some clip event code to the laser movie clip.

Select the laser movie clip, and choose Window> Actions to open the Actionscript window.

Type the following code:

onClipEvent (load) {
 laserMoveSpeed=20;
 this._y=_root.spaceship._y;
 this._x=_root.spaceship._x+80;
}

Its some onClipEvent(load) code again. This time its associated with the laser movie clip so it will be run when the laser movieclip first appears on the stage. The code is also run whenever the laser movie clip is duplicated. When you duplicate a movieclip you duplicate the graphics, frames and all code associated with the movie clip. Each new duplicated movie clip has its own copy of the original movie clips code. And each duplicated movie clip runs its copy of the onClipEvent(load) code when its loaded or duplicated.

So what do those three lines do?
The first line just sets a new variable called laserMoveSpeed equal to 20. This will be the number of pixels that the laser moves per frame. The second line sets the y-position of the laser (or duplicated copy of the laser) to the same y-position as the spaceship.
The third line sets the x-position of the laser (or duplicated copy of the laser) to the same x-position as the spaceship plus 80. We add 80 because the x-position is for the center of the spaceship and it looks funny to have the laser coming from the center. Adding 80 makes it look like its coming from the front.

Now some enterFrame code:

onClipEvent (enterFrame) {
this._x+=laserMoveSpeed; if (this._x>600){ this.removeMovieClip(); } }

This code moves the laser movie clip (or duplicated copy of the laser) to the right of the screen. When the x-position gets greater than 600 it removes itself.

In a little more detail: the first line sets the x-position of the laser movie clip (or duplicated copy of the laser) to the current x-position plus laserMoveSpeed. As we set laserMoveSpeed to 20 this means that the x-position increases by 20 every frame.
The next lines are an if statement, which simple removes the laser movie clip if its x-position is greater than 600.

And that's it - if you followed carefully you should have a spaceship that you can move around the screen using the arrow keys and fire lasers with the Ctrl key. Hopefully you will also understand what the code is doing.

If you didn't get it working the final version can be downloaded

The next tutorial will build on this base to create a full game. We will add in scrolling backgrounds and some enemy spaceships to shoot at :)

Also thanks again to Olorin and MadSci for their assistance.

all the best, David.

«prev 1 2 3 4 5

» Level Intermediate

Added: : 2001-03-20
Rating: 8.21 Votes: 1005
Hits: 9483
» Author
No details available.
» Download
Download the files used in this tutorial.
Download (72 kb)
Get conversion and unzipping tools for PC and Mac here!

» Forums
More help? Search our boards for quick answers!

Please rate this tutorial, 10 is the top rating, you can also click the comments link to read/write a review.
10 9 8 7 6 5 4 3 2 1
Read or Post Comments
 
   
 

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs