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. :)
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. :)