PDA

View Full Version : [AS3] Collision related questions


JonnyG
04-16-2009, 04:10 AM
You guys seem to know what your talking about and helped me out with my previous ones so heres another go now at some more.

1.I have some code here on the driving game that I am working o for an assignment, I am however stuck with getting some collision code to work. My teacher gave me an example which i pretty much copy/pasted into my game and it worked before but I changed the movie clip from one object to the one I want to use now and it doesn't seem to want to work (same when I try to change it back).

2. I want to put some random objects along the track of my road which has turns and is one movie clip entirely. What would be the best method for this to make sure that it stays on the movie clip?

here is the code that was refered to in question 1:


car_mc.gotoAndStop(1);

stage.addEventListener( KeyboardEvent.KEY_DOWN, goCar );
function goCar( e:KeyboardEvent )
{
if( e.keyCode == Keyboard.UP )
{
car_mc.gotoAndStop(1)
car_mc.y = car_mc.y - 10;
backgroundRoad_mc.y = backgroundRoad_mc.y + 25
back1_mc.y = back1_mc.y + 25
barrier_mc.y = barrier_mc.y + 25
finishline_mc.y = finishline_mc.y + 25
}
if( e.keyCode == Keyboard.DOWN )
{
car_mc.gotoAndStop(6)
car_mc.y = car_mc.y + 10;
backgroundRoad_mc.y = backgroundRoad_mc.y - 25
back1_mc.y = back1_mc.y - 1
barrier_mc.y = barrier_mc.y - 25
finishline_mc.y = finishline_mc.y - 25
}

if( e.keyCode == Keyboard.LEFT )
{
car_mc.gotoAndStop(5)
car_mc.x = car_mc.x - 10;
}

if( e.keyCode == Keyboard.RIGHT )
{
car_mc.gotoAndStop(4)
car_mc.x = car_mc.x + 10;
}
}

stage.addEventListener(Event.ENTER_FRAME, animate);
function animate(event:Event):void
{
if(car_mc.y < stage.stageHeight/2 )
{
car_mc.y = stage.stageHeight/2;
}
else if(car_mc.x + car_mc.width/2 > stage.stageWidth )
{
car_mc.x = stage.stageWidth - car_mc.width/2;

}
else
{
if( back1_mc.y + back1_mc.height > 0 )
{
back1_mc.y -= 1;
}
}
}

//Collision With Barrier

var carLeft:Boolean = false;
var carRight:Boolean = false;
var carUp:Boolean = false;
var carDown:Boolean = false;

collision_txt.visible = false;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyCheckDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyCheckUp);
stage.addEventListener(Event.ENTER_FRAME, runAnimation);

function keyCheckDown(event:KeyboardEvent):void
{
if(event.keyCode==Keyboard.RIGHT)
{
carRight = true;
}
if(event.keyCode==Keyboard.LEFT)
{
carLeft = true;
}
if(event.keyCode==Keyboard.UP)
{
carUp = true;
}
if(event.keyCode==Keyboard.DOWN)
{
carDown = true;
}
}

function keyCheckUp(event:KeyboardEvent):void
{
if(event.keyCode==Keyboard.RIGHT)
{
carRight = false;
}
if(event.keyCode==Keyboard.LEFT)
{
carLeft = false;
}
if(event.keyCode==Keyboard.UP)
{
carUp = false;
}
if(event.keyCode==Keyboard.DOWN)
{
carDown = false;
}
}

function runAnimation(event:Event):void
{
if(car_mc.hitTestObject(backgroundRoad_mc))
{
collision_txt.visible = true;
}
else
{
collision_txt.visible = false;
}
if(carRight)
{
car_mc.x += 5;
}
if(carLeft)
{
car_mc.x -= 5;
}
if(carUp)
{
car_mc.y -= 5;
}
if(carDown)
{
car_mc.y += 5;
}
}




Thanks all those that put forward some insight into this. :)

rrh
04-16-2009, 05:06 PM
What does it do instead of work?

JonnyG
04-16-2009, 05:11 PM
It doesnt do anything. No message pops up at all.

JonnyG
04-16-2009, 05:20 PM
Heres something I think I should mention. I'm layering movie clips ontop of each other would that effect this code? My order is:

Barrier
Road
Grass

Which again all are movie clips.

rrh
04-16-2009, 05:29 PM
One of my little zen of programming things is, "It's always doing something. You just don't know what it's doing."

What this means in practical terms is, throw in a bunch of trace statements, to make the problem more visible. Like so:

trace('line1',car_mc,backgroundRoad_mc,car_mc.hitT estObject(backgroundRoad_mc));
if(car_mc.hitTestObject(backgroundRoad_mc))
{
trace('true path');
collision_txt.visible = true;
}
else
{
trace('false path');
collision_txt.visible = false;
}

JonnyG
04-16-2009, 05:35 PM
This was what popped up if this is what you wanted to see:

line1 [object MovieClip] [object MovieClip] true
true path

rrh
04-16-2009, 05:45 PM
Does it always trace that, even when it's not supposed to be colliding?

You might be bumping against the "bounding box" issues of hitTestObject.
http://www.actionscript.org/forums/showthread.php3?t=182851

JonnyG
04-16-2009, 06:00 PM
No I dont believe so, I have the game uploaded if you dont mind taking a look at it?

http://www.filesavr.com/gamecs3_1

EightySeven
04-16-2009, 11:23 PM
I donno what was wrong with your text box but i replaced it and it worked fine.

However your new challenge is to fix the collision. because you are ALWAYS colliding with the background movieclip As long as the card's blue boarder is inside of the targets blue boarder your colliding
you need to break each section down into smaller chunks if you want your collision to work

Check the attachment

Ohh yea..i may or may not have changed the name and phrase in your text

JonnyG
04-17-2009, 12:10 AM
Thank you so much EightySeven I really do appreciate the effort you put into this. I do have one small question to ask, I was thinking about when you said split it up I was going to make them two seperate movie clips, would I be able to use the same code for both but just change the movie clip name thats in the code or am I gonna have to modify the original to fit both in?

JonnyG
04-17-2009, 05:30 AM
Ok heres an update, I changed my barriers to smaller movie clip pieces that line the edge of my road and i found out that it be best for me to make an array for my barrier. Could I get some help on how to make a proper array that would work with this?

Again much appreciation to those that already helped me and to those that decide to help me with this task. :)

http://www.filesavr.com/gamecs32 (New version)