A
A
Alexander Sharomet2015-02-18 15:46:05
Programming
Alexander Sharomet, 2015-02-18 15:46:05

Actionscript 3.0 how to properly use hitTestPoint?

Hello.
The essence of the game in a nutshell, the ball rolls, jumps, falls and. etc.
I wanted to make a simple game and ran into this problem:
When the ball falls on a flat surface, it stops in time, but if it hits some corner, it sometimes slips, everything can be seen in the screenshot.

private function run(e:Event):void{ //Функция запускается через ENTER_FRAME
      if(left){xspeed = 5;} //если переменная left=true (шар катится влево)
      else if(right){xspeed = -5;} //если переменная right=true (шар катится вправо)
      else {xspeed = 0;} //в другом случае скорость =0 и шар стоит на месте
      yspeed++; //Что то типа гравитации шар постоянно падает
      ball_mc.x -= xspeed;
      ball_mc.y += yspeed;
      
//Вот проблемный участок кода
      while (boks_mc.hitTestPoint(ball_mc.x,ball_mc.y+ball_mc.width/2,true)){
        ball_mc.y--;
        yspeed = 0;
        jamp = false;
      }
      while (boks_mc.hitTestPoint(ball_mc.x+ball_mc.width/2,ball_mc.y,true)){
        ball_mc.x--;
        xspeed = 0;
      }
      while (boks_mc.hitTestPoint(ball_mc.x-ball_mc.width/2,ball_mc.y,true)){
        ball_mc.x++;
        xspeed = 0;
      }

    }

Works
11fc851aebf240c48920e2cf0d6e9a3c.png
fine Here is the glitch
69e5abf3448a441091d54024cf0d43c8.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Mylchenko, 2015-02-18
@sharomet

This is not a glitch, you misunderstood the essence of this method.
hitTestPoint() - Calculates the display object to determine if it overlaps or intersects the x and y point . The x and y parameters specify a point in the coordinate plane of the client area, not the display object container containing the object (unless the container itself is the client area). hitTestObject() should suit you better

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question