Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Colliding Clips - Page 2

November 27, 2001




Next we are going to create two different movie clips. We will see how to detect when one of the clips actually collides with the other movie clip. This can be great for Flash interactive games and some cool navigation. For this example we are going to have one of the clips remain stationary while the other one is going to be moving around using the startDrag function. For this example we are going have an instance name of "good_guy" on the stationary clip and "bad_guy" on the movie clip that will ultimately collide with our good one.

On the very first frame of the movie we are going to add some ActionScript to allow the bad_guy movie clip to be dragged by the user around the screen. To do this we simply add startDrag ("_root.bad_guy", true); in the very first frame of the movie. The true variable tells Flash to lock the mouse to the center of the movie clip. In the normal mode you will be able to see the option at the bottom of the ActionScript panel. If you want to test the movie at this point go ahead and you will see that you can how move the object around with the mouse pointer. In the bad guy clip we want to put in a "stop" action in the first frame of the clip so that once it starts it won't advance past the beginning. Then in the second frame of the movie we will create another view of the image that looks as though it exploded so that when it does collide with the good guy clip it will look different.

Next we want to determine when the actual collision occurs. When you create something using the collision detect you will need to decide which clip is going to detect the other clip. For this example we are going to have the bad guy doing the detection. So, edit the bad_guy movie clip and add an extra frame. This is done so that it will loop and constantly check for collision. In the first frame of the clip enter the following code:

flag = this.hitTest("/good_guy");
 if (flag == true) { tellTarget ("/") { gotoAndStop
 ("hit"); } }

[The lines above are one continuous line. It has been split for formatting purposes.]

This is done by selecting setVariable from the Actions menu. Then in the Variable section, enter: flag Next, in the Value section, enter: this.hitTest("/good_guy") Then enter the following code:

if (flag == true) { tellTarget ("/good_guy") { gotoAndStop (2); } }

This is the code that will tell your movie to move to the second frame and explode upon collision.

Using the following code, the clip will go back to the first frame of the movie once you move away from the movie clip.

if (flag == false) { tellTarget ("/good_guy") { gotoAndStop (1); } }

This can be done, but is certainly not required.

Beginning to Use ActionScript
Flash 5 Tutorial: Part 1
Random Quotes - Page 3


Up to => Home / Authoring / Flash / Basic




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers