Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 07-04-2009, 02:33 AM   #1
German Cutz
Member
 
Join Date: Dec 2004
Posts: 69
Default Timeline AS3 collision detection

I'm learning AS3. On stage, I have a movie clip mc_1 that responds to keyboarding. There are four movie clips rectangle_mc .. rectangle_mc3 on each side of stage.

PROBLEM:

mc_1 is moved up, down, right, and left with arrow keys. It detects rectangles when hit, but it does not stop when hitting rectangles. How can I make mc_1 stop when hitting a rectangle?


HERE IS MY SCRIPT:

stage.addEventListener(KeyboardEvent.KEY_DOWN, moveDrawing);


function moveDrawing(event:KeyboardEvent): void
{
var speed:uint=5;

switch (event.keyCode){
case Keyboard.RIGHT:
mc_1.x+=speed;
break;

case Keyboard.LEFT:
mc_1.x-=speed;
break;

case Keyboard.UP:
mc_1.y-=speed;
break;


case Keyboard.DOWN:
mc_1.y+=speed;
break;
}
}


// collision detection

mc_1.addEventListener(Event.ENTER_FRAME, rectangleHit);
function rectangleHit(event:Event): void
{
if (mc_1.hitTestObject(rectangle_mc|| rectangle_mc2)){
mc_1.stop(); //HERE IS WHERE I WANT IT STOP.
trace ("rectangle & 2 hit");
}

if (mc_1.hitTestObject(rectangle_mc1|| rectangle_mc3)){
mc_1.stop();//HERE IS WHERE I WANT IT STOP.
trace("rectangle1 &3 hit");
}
};

thanks for your help.


German
German Cutz is offline   Reply With Quote
Old 07-04-2009, 11:06 AM   #2
Tilpo
Watermelons are gansters
 
Tilpo's Avatar
 
Join Date: Aug 2008
Location: Hilversum, The Netherlands
Posts: 215
Default

Hey there German,

I took a look at your code and saw that without changing the idea of your original code it would be a little bit complicated to solve the problem. But i did it anyway:
ActionScript Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveDrawing); var keyPressed:String; var keyEnabled:Array = [true, true, true, true]; function moveDrawing(event:KeyboardEvent):void {     var SPEED:uint = 5;     switch (event.keyCode) {         case Keyboard.RIGHT :             if (keyEnabled[0]) {                 mc_1.x += SPEED;                 keyPressed="right";                 enableAll();             }             break;                     case Keyboard.LEFT :             if (keyEnabled[1]) {                 mc_1.x-=SPEED;                 keyPressed="left";                 enableAll();             }             break;                     case Keyboard.UP :             if (keyEnabled[2]) {                 mc_1.y-=SPEED;                 keyPressed="up";                 enableAll();             }             break;                     case Keyboard.DOWN :             if (keyEnabled[3]) {                 mc_1.y+=SPEED;                 keyPressed="down";                 enableAll();             }             break;     }     function enableAll():void {         keyEnabled = [true, true, true, true];     } } mc_1.addEventListener(Event.ENTER_FRAME, rectangleHit); function rectangleHit(event:Event):void {     if (mc_1.hitTestObject(rectangle_mc)||mc_1.hitTestObject(rectangle_mc2)) {         disableKey();         trace("rectangle & 2 hit");     }     if (mc_1.hitTestObject(rectangle_mc1)||mc_1.hitTestObject(rectangle_mc3)) {         disableKey();         trace("rectangle1 &3 hit");     }         function disableKey(key:String = keyPressed):void {         if (key=="right") {             keyEnabled[0]=false;         }         if (key=="left") {             keyEnabled[1]=false;         }         if (key=="up") {             keyEnabled[2]=false;         }         if (key=="down") {             keyEnabled[3]=false;         }     } }

What it basically does is that when there is collision with a rectangle, he disables the mc_1 to go any further in the current direction.
When mc_1 collides with a rectangle, the code checks what key was pressed in order to create that collision. Then whenever a key is pressed it checks whether it is not the key that caused the collision, when it that key, he simply ignores the key being pressed. When a different key is pressed the code re enables the disabled key.
__________________

hoc postum continuit nudilo appendo
This post was touched by his noodly appendage.
Tilpo is offline   Reply With Quote
Old 07-04-2009, 12:11 PM   #3
German Cutz
Member
 
Join Date: Dec 2004
Posts: 69
Default

Dear Tilpo:

Thanks for taking the time to help me. I used your suggestion. However, I haven't been able to make it work yet.

1. It seems there is more code I couldn't get. I got up to case keyBoard.UP
2. Flash reports 1180: Call to a possibly undefined method enabled. when enableAll(); is used.

Can you please resend your code in a way I can see it all?
Why is this error message when using enableAll?

Thanks again.

German
German Cutz is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:53 AM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2010 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.