Back to "flash Basic Theory Class-Catalog"
I'm very glad to be promoted to this chapter. The previous chapters are all interactive movements in general. Let the object move, then let the object interact with the environment, then interact with the user, and finally the interaction between the objects. This chapter will describe in detail the interaction between objects, with a certain distance. To be specific, we will learn about particles, gravity (a little different from the front), elastic motion (or it!). ) and the famous node Garden. Let's get started!
Particle (particles)
Explain what we mean by the particle. For the purposes of this chapter, the particle is only a separate unit, usually accompanied by several (or more) of the same units. A particle can be said to be a speck of dust, a beach ball, or a planet.
Particles have a common behavior, or they can have independent behavior of their own children. OOP programmers often look at them like objects. All objects of a class have the same behavior, they are defined by this class, and the constructed instance becomes somewhat different because it is endowed with various attribute values. We have seen examples of many Ball classes in the example. Each object has its own attributes: speed, Mass, size, color, and so on, but all the balls are moving in the same rules.
In the ball class, we have all the features we need. Similarly, instances of particles are only used to hold properties. Document classes are only responsible for moving particles and handling interactions between them. Another approach is to put the behavior code in the particle class, so that each particle object will have their own enterframe function or timed animation, they will be responsible for moving or processing interactive motion. They all have their own addition and subtraction operations when they perform the processing functions. The example program in this book places motion and interaction in the document class to look simple.
The program as a whole is the same as the previous example, and the change is in the interaction and gravitation between the two particles, which are written in the Onenterframe method. In the program we're going to create multiple particles and randomly place them on the screen with the following code:
package {
import flash.display.Sprite;
import flash.events.Event;
public class ClassName extends Sprite {
private var particles:Array;
private var numParticles:uint = 30;
public function ClassName() {
init();
}
private function init():void {
particles = new Array();
for (var i:uint = 0; i < numParticles; i++) {
var particle:Ball = new Ball(5);
particle.x = Math.random() * stage.stageWidth;
particle.y = Math.random() * stage.stageHeight;
particle.mass = 1;
addChild(particle);
particles.push(particle);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
}
}
}
Here, the initial radius of each particle is 5 set, and the mass is 1. Later, by changing these values we can see different effects, and of course we can give the particles random speed and size.
The procedures in this chapter assume that these basic settings are complete, as we mainly introduce the Onenterframe function and the methods it needs. Now, let's start with some basic theories, the file that you just created is not going to be closed, because it needs to be used right away.
Gravity
The first kind of particle gravity is gravity. You might say, "Isn't this the second part of the return?" "Yes, but that's the most common form of gravity."
Standing on the Earth, the definition of gravity is simple: pull the object downward. In fact, the downward traction has a special rate. The acceleration it exerts on an object is equal to 32 feet per second. From an acceleration point of view is the amount of speed accumulated in a moment. Gravity makes the object move 32 feet and pulls down every second. But we can go to the highest mountain, or the lowest canyon, and these subtle changes to the 32 figure are not enough to make people aware of (unless using sophisticated instruments).