The Nineth chapter of Flash Theory course collision Detection Ⅰ

Source: Internet
Author: User
Tags requires

Back to "flash Basic Theory Class-Catalog"

So far, we have learned to interact with objects in their space. Next, look at the interaction between objects. This requires determining when collisions occur between objects, which is what we call Collision detection (collision detection or hit testing).

In this chapter I will try to tell you all the relevant knowledge that needs to be mastered. These include the collision of two films, the collision between the films and the points, the detection of collisions by distance and the method of collision detection of multiple objects. First, take a look at the existing collision detection methods.

Collision Detection Method

The idea of collision detection is very, very simple. We just need to know whether two objects are in the same position at the same time. Of course, there may be more than two objects, which requires knowing if one of them is colliding with other objects.

As in the topic of this section, there are many ways to detect collisions: can you detect the actual pixel of an object (sprite or MovieClip), that is, to determine whether the graphics in the two movies overlap? For this kind of detection method, want to judge by the actual visible pixel of the picture inside the movie, still be judged by the rectangle boundary of the movie? This involves two options built into the hittest approach to meet different requirements.

Collisions can also be judged by distance. Get the distance between the two objects and ask, "Is it nearly enough to collide?" "You need to calculate and judge distances when applying this method."

Each of these methods has its own purpose. Specific implementations are explained in this chapter. As for what to do after a collision, this chapter does not cover. Because, in the 11th chapter, we will explain the conservation of momentum in detail.

Hittestobject and Hittestpoint

Movie clips in previous Flash have built-in hittest methods, which can be used in many ways. Now it is more reasonable to divide it into two different methods. The Hittestobject method is used to determine whether a collision occurred between two display objects, and the Hittestpoint method is used to determine whether a collision occurred between a point and a display object.

Collision Detection Two films

Using Hittestobject to determine whether two films collide is probably the simplest method of collision detection. Call this function as a method of the movie and pass the reference of another movie as a parameter. Note that while I'm talking about movies, both of these methods are members of the Displayobject class and can be used for all subclasses that inherit from the display object class, such as Movieclip,bitmap,video,textfield. The format is as follows:

sprite1.hitTestObject(sprite2)

Typically used in an if statement:

if(sprite1.hitTestObject(sprite2)) {
    // 碰撞后的动作
}

Returns true if two movies collide, and executes the contents of the IF statement block. All things are a double-edged sword. The simpler the collision detection method is, the lower the precision is, and the higher the accuracy of the detection, the more complex the implementation becomes. Therefore, the simplest method, the accuracy is also the worst.

So what does accuracy mean in collision detection? is not to judge two objects there is no conflict? I also hope that the problem is only so simple.

Looking back at the problem: know the location of two films, how to determine whether they collide? The simplest way to do this is to take an object and draw a rectangle around it. Duplicate one of the same movie, two collision detection between the two. Finally, determine if there is an intersection between the two rectangles. If so, a collision occurs. To surround an object with a rectangle is our well-known rectangular boundary (bounding box). When we click on a stage component in the Flash IDE, we see a circle of blue contour lines, as shown in Figure 9-1.

Figure 9-1 Rectangular Boundary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.