Skip to main content
Participating Frequently
November 19, 2011
Question

HitTesting multiple array elements

  • November 19, 2011
  • 1 reply
  • 909 views

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.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
November 19, 2011

i'm not sure how it will help you to test all the blocks at one time instead of using a for-loop but you can do that by testing levelHolder.  you'll probably need a pixel-based hittet though. 

FoxelboxAuthor
Participating Frequently
November 19, 2011

I took another look at the problem and saw that hitBlock only contains 1 block at a time, if i'm correct.

If I lean on a corner and at the same time fall through the floor, it only contains the wall block and not the ground block, resulting in dropping through the floor.

kglad
Community Expert
Community Expert
November 19, 2011

then there's a problem in your logic.  you need to check if corner blocks are hit.