1 Reply Latest reply on Mar 9, 2011 1:00 PM by prodigalmaster

    how to hitTest to bricks(break bricks with ball game)?

    JaxNa Level 1

      hi,

       

      Plz can u tell me plz how to hitTest to brick by ball.

      here is my code.

       

      stop();

       

      var ballXspeed:Number = 10;

      var ballYspeed:Number = 10;

       

      onEnterFrame = function ()

      {

      Paddle_mc._x = _xmouse - Paddle_mc._width * .5;

      if (_xmouse < Paddle_mc._width / 2)

      {

      Paddle_mc._x = 0;

      }

      if (_xmouse > Stage.width - Paddle_mc._width / 2)

      {

      Paddle_mc._x = Stage.width - Paddle_mc._width;

      }

      ball_mc._x += ballXspeed;

      ball_mc._y += ballYspeed;

      if (ball_mc._x >= Stage.width - ball_mc._width)

      {

      ballXspeed *= -1;

      }

      if (ball_mc._x <= 0)

      {

      ballXspeed *= -1;

      }

      if (ball_mc._y >= Stage.height - ball_mc._height)

      {

      ballYspeed *= -1;

      }

      if (ball_mc._y <= 0)

      {

      ballYspeed *= -1;

      }

      if (ball_mc.hitTest(Paddle_mc))

      {

      calcBallAngle();

      }

      for(var k:Number=0;k<=brickXArry.length;k++){

      if(ball_mc._x+width==brickXArry[k]){

      trace("hit")

      }

      }

      //if(ball_mc.hitTest(_root["brick3"+i])) 

      };

      function calcBallAngle():Void

      {

      var ballPose:Number = ball_mc._x - Paddle_mc._x;

      var ballForce:Number = (ballPose / (Paddle_mc._width - ball_mc._width)) - .5;

      ballXspeed = ballForce * 10;

      ballYspeed *= -1;

      }

       

      function level():Void

      {

      var arrayLength:Number = _root.levelArray[currentLevel - 1].length;

      var brickRow:Number = 0;

      for (var i:Number = 0; i < arrayLength; i++)

      {

      if (levelArray[currentLevel - 1][i] == 1)

      {

      _root.attachMovie("brick_mc","brick" + i,_root.getNextHighestDepth());

      _root["brick" + i]._x = 15 + (i - brickRow * 8) * 65;

      _root["brick" + i]._y = 10 + brickRow * 20;

       

      for (var j:Number = 1; j <= 10; j++)

      {

      if (i == j * 8 - 1)

      {

      brickRow++;

      }

      }

      }

      if (levelArray[currentLevel - 1][i] == 2)

      {

      _root.attachMovie("brick_mc2","brick2" + i,_root.getNextHighestDepth());

      _root["brick2" + i]._x = 15 + (i - brickRow * 8) * 65;

      _root["brick2" + i]._y = 10 + brickRow * 20;

       

      for (var j:Number = 1; j <= 10; j++)

      {

      if (i == j * 8 - 1)

      {

      brickRow++;

      }

      }

      }

      if (levelArray[currentLevel - 1][i] == 3)

      {

      _root.attachMovie("brick_mc3","brick3" + i,_root.getNextHighestDepth());

      _root["brick3" + i]._x = 15 + (i - brickRow * 8) * 65;

      _root["brick3" + i]._y = 10 + brickRow * 20;

       

      for (var j:Number = 1; j <= 10; j++)

      {

      if (i == j * 8 - 1)

      {

      brickRow++;

      }

      }

      }

      if (levelArray[currentLevel - 1][i] == 4)

      {

      _root.attachMovie("brick_mc4","brick4" + i,_root.getNextHighestDepth());

      _root["brick4" + i]._x = 15 + (i - brickRow * 8) * 65;

      _root["brick4" + i]._y = 10 + brickRow * 20;

       

      for (var j:Number = 1; j <= 10; j++)

      {

      if (i == j * 8 - 1)

      {

      brickRow++;

      }

      }

      }

       

      }

      }

       

      level();

       

       

      Thanks,

      JaxNa