PDA

View Full Version : Levels/Collision


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);

Bunney lord
12-28-2008, 10:04 PM
You are only doing a hitTest on the first item in your array. So you will need to loop through the array like this in order to do a hitTest on all of the movieClips.

Replace:

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


with:

for (var i = 0; i<testme_array.length; i++) {
if (testme_array[i].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;
}
}

Daak
12-30-2008, 08:29 PM
Hi, thanks for getting me on the right track. I see what you are saying but it didn’t work after cutting and pasting your revision. The player movie clip passed through both blocks. I tried moving the “testme_array” into the “player.move” code block and I think it did start to recognize the loop. If i=1 (in the loop), the player hits block 2 only and if I subtract 1 from the .length, the player hits block 1 only(what you see below). Any other ideas? Does the block symbol need a linkage identifier? I’m using flash 8 professional and if you need the file I’m using, I can upload it somewhere. 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;
var testme_array:Array = new Array (block1_mc, block2_mc);
//check to see if this position will hit a building
for (i=0; i<testme_array.length-1; i++) {
if (testme_array[i].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;
}
}
}

Bunney lord
12-31-2008, 02:41 AM
It would seem that the code checks to see if the player is hitting the first block, and if it is, then the player dosn't move, but then the code checks to see if the player is hitting the second box which it isn't, so the player moves. All you need to do is add a varible and set it true before you start checking, and if you hit a box, you can set the varible to false, then if the varible is true you can move. I put the varible back at where it was, that way you arn't making an array every time you move, all you need to do is put _root before you address it. This code will work:


stop();
init();
var testme_array:Array = new Array(block1_mc, block2_mc);


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;
moving = true;
for (i=0; i<testme_array.length; i++) {
trace(i);
if (_root.testme_array[i].hitTest(newX, newY, true)) {//do nothing, because this motion will make you crash into a building
trace("hitting building");
moving = false;
break;
}
}
if (moving == true) {
player._x = newX;
player._y = newY;
}
};

Daak
01-01-2009, 09:29 PM
Thanks, that gives me something to think about for a while.