• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• if a ball is hitting a box from every side, how to...

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

New Here ,
Sep 18, 2016 Sep 18, 2016

Copied

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{

start_game.alpha=0;

}

//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{

}

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;

}

}

// 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");

}

}

}

}

}

Views

808

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Oct 22, 2016 Oct 22, 2016

Copied

LATEST

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