Skip to main content
gargcreative
Participant
September 18, 2016
Question

if a ball is hitting a box from every side, how to know which side hittest is true

  • September 18, 2016
  • 1 reply
  • 863 views

i am creating a brick breaker game in which ball is hitting a square object. i want to change the direction of ball when he hit square object.

square is of 15 px. and

ball is of 10 px.

for example

   

if hits on the right side, Speed in x direction will reverse

if hits on the left side, Speed in x direction will reverse

if hits on the up side, Speed in y direction will reverse

if hits on the down side , Speed in y direction will reverse.

   

I tried it hard but still can not understand how to describe each side of the box. any help will be appreciated. here is the code:

   

          import flash.events.*;

          import flash.display.*;

           stop();

// speed of ball in x and y

           var speedx : Number = 10;

           var speedy : Number = 10;

// start running

           function begin_code (event:MouseEvent):void{

           addEventListener(Event.ENTER_FRAME,move_ball);

           stage.addEventListener(MouseEvent.MOUSE_DOWN,by_key);

           addEventListener(Event.ENTER_FRAME,ht_mc);

           start_game.alpha=0;

           }

           start_game.addEventListener(MouseEvent.CLICK, begin_code) ;

//start ball moving

   

      function move_ball(e:Event):void{

  Ball.x += speedx;

  Ball.y += speedy;

  Ball.rotation +=speedx;

  var ballposition : Number = Ball.x -Hitbar.x;

    var ballhitpercent : Number = (ballposition /(Hitbar.width-Ball.width));

  if(Ball.x <= x1.x+Ball.width/2){

  speedx = speedx *-1;

  }

  if(Ball.x >= x2.x-Ball.width/2){

  speedx = speedx *-1;

  }

  if(Ball.y <=(55)){

  speedy = speedy *-1;

  }

  else if (Ball.y >= stage.stageHeight-Ball.height){

  speedy = speedy *-1;

  trace("hit y");

  }

  //start ball angle

  else if(Ball.hitTestObject(Hitbar)){

  speedx = ballhitpercent*10;

  speedy = speedy *-1;

  }

}

//start hitbar moving

// code for keys!!!!!!!!!

        var distance : Number = 0;

        function by_key(e:MouseEvent):void{

        if (mouseX>Hitbar.x){

     distance = (mouseX-Hitbar.x);

     Hitbar.x += distance;

  }

  else if (mouseX<Hitbar.x){

  distance = (Hitbar.x-mouseX);

  Hitbar.x -= distance;

  }

  }

//restart

      function by_key_up(e:MouseEvent):void{

   }

      stage.addEventListener(MouseEvent.MOUSE_UP, by_key_up);

      function pLimiter(e:Event):void{

      Hitbar.y = 406.2;

      if (Hitbar.hitTestObject(x1)){

  Hitbar.x = 32.6;

  }

       if (Hitbar.hitTestObject(x2)){

  Hitbar.x = 287.55;

  }

    }

      addEventListener(Event.ENTER_FRAME, pLimiter);

// Bricks set hit

        var pickup:Array =new Array();

        for (var i = 0;i<numChildren;i++){

  if(getChildAt(i) is abc){

        pickup.push(getChildAt(i));

  }

    }

       function hittest_box(e:Event):void{

  for (var f = 0;f<pickup.length;f++){

  if (Ball.hitTestObject(pickup)){

    if(pickup.parent) {

  pickup.parent.removeChild(pickup);

  }

     }

       }

       }

function ht_mc(e:Event):void{

  for (var j = 0;j<pickup.length;j++){

         if(Ball.x > pickup.x -pickup.width/2 -Ball.width/2 && Ball.x < pickup.x +pickup.width/2 +Ball.width/2){

      

  if (Ball.hitTestObject(pickup)){

          speedy = speedy *-1;

  if(pickup.parent) {

     pickup.parent.removeChild(pickup);

  trace("box y");

  }

  }

  }

  else if(Ball.y > pickup.y -pickup.width/2 -Ball.width/2 && Ball.y < pickup.y +pickup.width/2 +Ball.width/2){

         if (Ball.hitTestObject(pickup)){

         if(pickup.parent) {

     pickup.parent.removeChild(pickup);

  speedx = speedx *-1;

  trace("box x");

  }

  }

  }

  }

}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
October 22, 2016

you can compare the ball's x,y properties with the box'es x,y properties and determine which side is being hit.