Daak
12-28-2008, 08:52 PM
Levels/Collision
Hey all I’m new here and to actionscript. I’ve been doing collision experiments with multiple blocks and can’t get it to work right. I created an array at the bottom for my two “block” instances and when I trace that array it says the two blocks are on level 0. However, once I plug the array into the “if” statement, It recognizes the blocks on level 0 and 1. How do I get the “player” movieclip to collide with more than one block? Heres the script, the important areas are in bold. Thanks.
stop();
init();
function init(){
player.dx = 0;
player.dy = 0;
player.speed = 5;
} // end init
//manage player control through keyboard
player.onEnterFrame = function(){
player.checkKeys();
player.move();
player.checkCollisions();
} // end enterFrame
player.checkKeys = function(){
player.dx = 0;
player.dy = 0;
if (Key.isDown(Key.UP)){
player.dy = - player.speed;
} // end if
if (Key.isDown(Key.DOWN)){
player.dy = + player.speed;
} // end if
if (Key.isDown(Key.LEFT)){
player.dx = - player.speed;
} // end if
if (Key.isDown(Key.RIGHT)){
player.dx = + player.speed;
} // end if
} // end checkKeys
player.move = function(){
//calculate what position will happen next
newX = player._x + player.dx;
newY = player._y + player.dy;
//check to see if this position will hit a building
if (testme_array[0].hitTest(newX, newY, true)){
//do nothing, because this motion will make you crash into a building
//trace ("hitting building");
} else {
//it won't be a building hit, so go for it.
player._x = newX;
player._y = newY;
} // end if
}// end move
var testme_array:Array = new Array(block1_mc, block2_mc);
trace(testme_array);
Hey all I’m new here and to actionscript. I’ve been doing collision experiments with multiple blocks and can’t get it to work right. I created an array at the bottom for my two “block” instances and when I trace that array it says the two blocks are on level 0. However, once I plug the array into the “if” statement, It recognizes the blocks on level 0 and 1. How do I get the “player” movieclip to collide with more than one block? Heres the script, the important areas are in bold. Thanks.
stop();
init();
function init(){
player.dx = 0;
player.dy = 0;
player.speed = 5;
} // end init
//manage player control through keyboard
player.onEnterFrame = function(){
player.checkKeys();
player.move();
player.checkCollisions();
} // end enterFrame
player.checkKeys = function(){
player.dx = 0;
player.dy = 0;
if (Key.isDown(Key.UP)){
player.dy = - player.speed;
} // end if
if (Key.isDown(Key.DOWN)){
player.dy = + player.speed;
} // end if
if (Key.isDown(Key.LEFT)){
player.dx = - player.speed;
} // end if
if (Key.isDown(Key.RIGHT)){
player.dx = + player.speed;
} // end if
} // end checkKeys
player.move = function(){
//calculate what position will happen next
newX = player._x + player.dx;
newY = player._y + player.dy;
//check to see if this position will hit a building
if (testme_array[0].hitTest(newX, newY, true)){
//do nothing, because this motion will make you crash into a building
//trace ("hitting building");
} else {
//it won't be a building hit, so go for it.
player._x = newX;
player._y = newY;
} // end if
}// end move
var testme_array:Array = new Array(block1_mc, block2_mc);
trace(testme_array);