A
A
Alexander Sharomet2016-08-28 22:36:04
Programming
Alexander Sharomet, 2016-08-28 22:36:04

How to use hitTestObject to predict collision?

Hello.
Is there any way to predict that an object will collide with another object?
I just wrote a script in which the ball falls to the surface, its speed increases, such as acceleration.
And it turns out that initially his speed (step) is equal to 5, but at the time of the collision it is equal to 25, that is, the last step to the obstacle he climbs onto it, since during the collision his step was 25. How can you deal with this?

import flash.events.Event;

var ball:Ball;
var ball_speed_y:Number = 5; // Начальная скорость
var ball_acceleration_y = 1.25; //Ускорение

var ball_run_y:Boolean = true; // Движение да нет.

ball = new Ball(); 

ball.x = 50;
ball.y = 50;

addChild(ball);

ball.addEventListener(Event.ENTER_FRAME, run);

function run(e:Event):void{
  if(pol.hitTestObject(ball)){ //ball_speed_y=25
    ball_run_y = false;
  }
  
  if(ball_run_y){
    ball.y += ball_speed_y;
    ball_speed_y += ball_acceleration_y;
  }
}

235f8f54675a4fd2ab75a76d3075c2b9.png
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Neonoviiwolf, 2016-08-28
@sharomet

You can, for this you must calculate the collision 1 frame in advance.
But there is a physical the Nape library, everything has already been calculated for you, well, or Box2D (it really has a memory leak in the AS3 version)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question