Links:
view my trainer profile on TrainerExchange.com Listed on: Dmegs Web Directory

PHP video tutorials

phpmath.com
phpclasses.org
Canadian Mathematical Society
camel.math.ca

Algebra.com
Blogs:
rationalcode.com
Silverlight Playground

Technology:
silverlight.net
XNA Creators Club Online
php
asp.net


Flash Gaming Tutorial - Collision Detection

by Administrator 30. July 2009 02:14

Collision detection is the most important concept in game development. It does not matter which programming language you are using for gaming. XNA, Flash or ... They all offer a method to detect collisions. You should be able to detect collisions and perform some logic to respond to it.

In this small tutorial I'm going to show you how to detect collisions in ActionScript. There are other ways to detect collisions in Flash / ActionScript but this one is the simplest one I believe.

Say you have a bullet movie clip and an enemy movie clip and need to know if the bullet hits the enemy. To do so follow these steps:

  • Open a new Flash file (anything but ActionScript 3.0 file).
  • Draw your bullet. For simplicity I used the Oval tool from the toolbox to create it.
  • Select the bullet object you just made and press F8 to convert it to a movie clip.
  • Select the movie clip and press Ctrl + F3 to open the Properties panel. Type bulletInstance in the instanceName box.
  • Draw the enemy. I used the Rectangle tool to create a simple rectangle as my enemy.
  • Select the rectangle and press F8 to convert it to a movie clip.
  • Select the enemy movie clip and press Ctrl + F3 to open the Properties panel. Then Type enemyInstance in the instanceName box.
  • Now we have all the graphics to start ActionScripting. Select the bulletInstance and press F9 to open the Actions panel. Type the following script:
    • onClipEvent (enterFrame) {
       // Check if the bullet hit the enemy object.
       if (this.hitTest(_root.enemyInstance)) {
        // The bullet hit the enemy. Stop moving.
       } else {
        // The bullet has not hit the target yet. Keep moving the bullet.
        this._x += 10;
       }
      }
  • We are done. Run our simple game by pressing Ctrl + Enter and confirm that the bullet stops after reaching (hitting) the enemy.

hitTest function:
I used hitTest method to detect collisions. I called this fucntion on the bulletInstance movie clip (I used this because I'm adding scripts to the bulletInstance's Action panel). It takes the target movie clip as a parameter (here enemyInstance) and examins for collisions between those two.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , ,

Flash/ActionScript | Gaming | Training

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

Reza Salehi
Microsoft Certified Professional

Microsoft Certified Trainer

Microsoft Certified Professional Developer

Microsoft certiied Technology Specialist (ASP.NET 2.0)

About Me

I started this blog to share my ideas, programs and interesting programming notes. Visitors who are interested in mathematics, physics and astronomy may find my blog interesting.

I live in Toronto, Canada and work as a Senior Software Developer and Microsoft Certified Trainer.

My online Microsoft web page is available  here.  Online business card.

RecentComments

Comment RSS