ActionScript 3.0 Lesson 17: Detecting Stage Collisions Between Objects in Different Layers

Wed, Aug 19, 2009

ActionScript


Click on Stage and using the keyboard arrows bring the disc to the base, and both will disappear !

This movie requires Flash Player 9


CLICK HERE TO SEE A LIVE DEMONSTRATION OF THIS TUTORIAL COMPLETED

On this Actionscript 3.0 Flash HD High Definition Tutorial we create a base for our Flying Saucer and testing collision with an IF statement a visible property change is fired. X Files Theme is the background music.

DOWNLOAD THE SOURCE FILE

DOWNLOAD THIS TUTORIAL TO YOUR HARD DRIVE

CLICK HERE FOR A COMPLETE ACTIONSCRIPT 3.0 CARTOON SMART TUTORIAL

Here is the source code

// Collision Between Two Objects on Stage Example
// by Avraham Saltoun
// solutions at say-web dot com
// FREE TO USE COPY AND DISTRIBUTE

stage.addEventListener(KeyboardEvent.KEY_DOWN,MoveSaucer);

//This will set the compiler
//waiting for a key stroke, and then
// execute the MoveSaucer function

function MoveSaucer (event:KeyboardEvent):void
// the void states that the function returns no value
// but receives the value of the key stroke
{
switch (event.keyCode){

case Keyboard.RIGHT:
FlyingSaucer_mc.x = FlyingSaucer_mc.x+5;
break;
// x is the coordinate for the horizontal position
// break will exit the switch and wait for
// next key stroke.

case Keyboard.LEFT:
FlyingSaucer_mc.x -=5;
// “FlyingSaucer_mc.x = FlyingSaucer_mc.x-5″
// is the same as
// “FlyingSaucer_mc.x -=5;”
break;

case Keyboard.UP:
FlyingSaucer_mc.y -=5;
break;

case Keyboard.DOWN:
FlyingSaucer_mc.y +=5;
break;

}

// This will check the collision
// between the Flying Saucer and
// the base.

if (FlyingSaucer_mc.hitTestObject(Base_mc))
{trace (“BOOOM !!!”);}

// Note both objects can be in separate layers
// instance names have to be identical to the objects
// This will happen every keystroke (Key Down) event

// Thanks for watching.

}

Related Articles:

, , , , , , , , , , , , , , , , , , , ,

One Response to “ActionScript 3.0 Lesson 17: Detecting Stage Collisions Between Objects in Different Layers”


Trackbacks/Pingbacks

Leave a Reply

You must be logged in to post a comment.

My name is Avraham Saltoun, and I live in Jerusalem, Middle East. I am currently on my mid  40's and I am a computer programming technician. Here you will find over one hundred quality video tutorials on Actionscript, and yes, I give online support for aspiring programmers, and pros as well, sharing screen on skype and delivering source code by email, click the button for live flash actionscript support now!

 
oDesk Certified Action Script Programmer
oDesk Certified Adobe Flash CS3 Designer
oDesk Certified Adobe Photoshop 6.0 Designer
Skype Me™!

$7.49 .Com! Score Savings Cartoon Smart Tutorials


1,260
Unique Visitors
Previous 30 Days
Powered By Google Analytics