Timeline AS3 collision detection

Commenta 4th July , 2009
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

Original post by German Cutz

Lascia un Commento