samedi 17 septembre 2016

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

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 side Speed in y direction will reverse.

I tried it hard but found nothing. any help will be appreciated. here is the code:

var pickup:Array =new Array();

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

if(getChildAt(i) is abc){

pickup.push(getChildAt(i)); } }

function ht_mc(e:Event):void{

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

if (Ball.hitTestObject(pickup[j])){

        if (Ball.y >= pickup[j].y + pickup[j].width/2 + Ball.width/2-0.5 ){
        speedy = speedy *-1;
        trace ("true y down")
        }

        if (Ball.x >= pickup[j].x + pickup[j].width/2 + Ball.width/2-0.5 ){
        speedx = speedx *-1;
        trace ("true x right")
        }

        if (Ball.x <= pickup[j].x - pickup[j].width/2 - Ball.width/2+0.5 ){
        speedx = speedx *-1;
        trace ("true x left")
        }
        if (Ball.y <= pickup[j].y - pickup[j].width/2 - Ball.width/2+0.5 ){
        speedy = speedy *-1;
        trace ("true y up")
        }
        if(pickup[j].parent) {
            pickup[j].parent.removeChild(pickup[j]);
} 

}

}

}

Aucun commentaire:

Enregistrer un commentaire