HitTesting multiple array elements
I have a tile-based platformer game under work, and I'm stuck on a problem with hitTesting:
//check all character collisions with levelHolder
for(var i:int = 0; i < levelHolder.numChildren; i++)
{
//get current block being touched
var hitBlock:DisplayObject = levelHolder.getChildAt(i);
//put char on surface of ground
if(hitBlock.hitTestPoint(char.x,char.y))
{
levelHolder.y = hitBlock.y * -1 + char.y + hitBlock.height;
ySpeed = 0;
jumping = false;
break;
}
//put char on surface of wall
if(hitBlock.hitTestPoint(char.x + char.width / 2, char.y - char.height / 2) && xSpeed > 0)
{
levelHolder.x = hitBlock.x * -1 + char.x + 15;
xSpeed = 0;
//if moving towards wall, initiate charOnWall
if(rightArrow)
{
charOnWall = true;
break;
}
}
if(hitBlock.hitTestPoint(char.x - char.width / 2, char.y - char.height / 2) && xSpeed < 0)
{
levelHolder.x = hitBlock.x * -1 + char.x - hitBlock.width - 15;
xSpeed = 0;
//if moving towards wall, initiate charOnWall
if(leftArrow)
{
charOnWall = true;
break;
}
}
//make charOnWall false
charOnWall = false;
}
levelHolder = the sprite holding all blocks
char = character MC, origin is in the bottom middle
jumping = boolean that checks if char is in the air
charOnWall = boolean that checks if char is leaning towards a wall
- then makes ySpeed a bit slower and makes walljumping possible
The problem here is that hitBlock only hitTests one block at a time, and when I test this:
If you lean on a wall (press right arrow key) and go downwards you can go through the floor.
I need to make it so that I can test on multiple blocks at a time.
And I did try to replace hitBlock with levelHolder, but it gave an error saying:
ReferenceError: Error #1069: Property 0 not found on flash.display.Sprite and there is no default value.
at Main/enterFrame()
I'm guessing that comes from the level array where 0 = blank block / empty space.