Lake
09-26-2009, 08:58 AM
I would like collisions in my game (obviously :P) but i cant get a system running correctly. My level is in an array so the walls are added dynamically and when they are being added an onEnterFrame function is given to each square that makes up the walls like so :
var row:Number = 0;
_root.createEmptyMovieClip('blockHolder',_root.get NextHighestDepth());
function createLvl():Void{
//finding the array of the current level we're on
//this is just a way to dynamically select an Array within the document
var lvlArray:Array = _root['lvlArray'+lvlCurrent];
//we have to find how far this level will span
//this will be used so we know when to move to the next row
//there will always be 16 rows, so this is how we find it out
//of course, this will make the level formatting very strict
var lvlColumns:Number = Math.ceil(lvlArray.length/16);
//now we can create the level
for(var i:Number=0;i<lvlArray.length;i++){
if(i/lvlColumns == int(i/lvlColumns)){
row ++; //moving onto the next row once we're done with a column
}
if(lvlArray[i] == 'D'){
//making a new block and adding it to stage
blockHolder.attachMovie('block_mc', 'Block'+i,blockHolder.getNextHighestDepth());
//modifying this guy's coordinates
blockHolder['Block'+i]._x = (i-(row-1)*lvlColumns)*25;
blockHolder['Block'+i]._y = (row-1)*25;
blockHolder['Block'+i].onEnterFrame = function()
{
}
} else if (lvlArray[i] == 'MAIN'){
//changing main's coordinates if we spot him
player_mc._x = (i-(row-1)*lvlColumns)*25;
player_mc._y = (row-1)*25;
}
}
//reset the row for later use
row = 0;
}
//finally, we run the function for the first time
createLvl();
and this is the array.
var lvl1Code:Array = new Array(1,1,1,1,1);
var lvlCurrent:Number = 1;
var X:String = 'MAIN';
var D:String = 'D';
var lvlArray1:Array = new Array(
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,D,D,D,D,D,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
);
inside the onEnterFrame function is where the collision detection i would like to go.
ive tried this so far:
if ( this.hitTest( _root.player_mc ) && dir == "left" ) { player_mc._x = player_mc._x + 5; moveLeft = false; }
else if ( this.hitTest( _root.player_mc ) && dir == "down" ) { player_mc._y = player_mc._y - 5; moveDown = false; }
else if ( this.hitTest( _root.player_mc ) && dir == "up" ) { player_mc._y = player_mc._y + 5; moveUp = false; }
else if ( this.hitTest( _root.player_mc ) && dir == "right" ) { player_mc._x = player_mc._x - 5; moveRight = false; }
( yes the variables are declared )
but if i press the up arrow and lets say the right arrow it will go through the wall.
any help ?
and how can i upload my game so you can view it ?
var row:Number = 0;
_root.createEmptyMovieClip('blockHolder',_root.get NextHighestDepth());
function createLvl():Void{
//finding the array of the current level we're on
//this is just a way to dynamically select an Array within the document
var lvlArray:Array = _root['lvlArray'+lvlCurrent];
//we have to find how far this level will span
//this will be used so we know when to move to the next row
//there will always be 16 rows, so this is how we find it out
//of course, this will make the level formatting very strict
var lvlColumns:Number = Math.ceil(lvlArray.length/16);
//now we can create the level
for(var i:Number=0;i<lvlArray.length;i++){
if(i/lvlColumns == int(i/lvlColumns)){
row ++; //moving onto the next row once we're done with a column
}
if(lvlArray[i] == 'D'){
//making a new block and adding it to stage
blockHolder.attachMovie('block_mc', 'Block'+i,blockHolder.getNextHighestDepth());
//modifying this guy's coordinates
blockHolder['Block'+i]._x = (i-(row-1)*lvlColumns)*25;
blockHolder['Block'+i]._y = (row-1)*25;
blockHolder['Block'+i].onEnterFrame = function()
{
}
} else if (lvlArray[i] == 'MAIN'){
//changing main's coordinates if we spot him
player_mc._x = (i-(row-1)*lvlColumns)*25;
player_mc._y = (row-1)*25;
}
}
//reset the row for later use
row = 0;
}
//finally, we run the function for the first time
createLvl();
and this is the array.
var lvl1Code:Array = new Array(1,1,1,1,1);
var lvlCurrent:Number = 1;
var X:String = 'MAIN';
var D:String = 'D';
var lvlArray1:Array = new Array(
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,D,D,D,D,D,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
);
inside the onEnterFrame function is where the collision detection i would like to go.
ive tried this so far:
if ( this.hitTest( _root.player_mc ) && dir == "left" ) { player_mc._x = player_mc._x + 5; moveLeft = false; }
else if ( this.hitTest( _root.player_mc ) && dir == "down" ) { player_mc._y = player_mc._y - 5; moveDown = false; }
else if ( this.hitTest( _root.player_mc ) && dir == "up" ) { player_mc._y = player_mc._y + 5; moveUp = false; }
else if ( this.hitTest( _root.player_mc ) && dir == "right" ) { player_mc._x = player_mc._x - 5; moveRight = false; }
( yes the variables are declared )
but if i press the up arrow and lets say the right arrow it will go through the wall.
any help ?
and how can i upload my game so you can view it ?