Flash basic Theory course fifth Chapter speed and Acceleration Ⅱ

Source: Internet
Author: User
Tags addchild definition numeric value

Back to "flash Basic Theory Class-Catalog"

Acceleration

Acceleration is generally considered to speed up and speed is slowed down. Yes, in this book, there is a more scientific definition of acceleration.

Velocity (vector) and acceleration have many similarities, they are vectors (or vectors). Velocity (vector) and Acceleration (vector) are defined by the amount of value (size) and direction. However, the velocity vector changes the position of the object, and the acceleration changes its velocity vector.

Imagine sitting in a car, then starting the car and stepping on the accelerator. What is the acceleration degree? After stepping on the accelerator (equivalent to acceleration), the speed vector begins to change (speed begins to accelerate and the direction changes to the steering wheel). After two seconds, the speed will rise to 4 to 5 miles per hour, and then it will become 10 miles, 20 miles, 30 miles, etc. The engine drives the car forward with its speed vector.

Therefore, the popular definition of acceleration is: to increase the force on the vector of the object's velocity. In ActionScript terms, the acceleration is a numeric value added to the speed vector.

For example, if a rocket were to fly from planet A to Planet B. It's in the direction of planet A and Planet B, and after adjusting the direction, start to light the rockets. When the rocket is lit, the speed will be faster and quicker, and when the commander thinks the rocket is fast enough, the rockets will slow down in order to keep the fuel. Assuming there is no resistance in space, the Rockets continue to fly at the same rate. When the rocket no longer fires, there is no more force to drive it. As a result, the acceleration is lost and the speed is no longer changed.

When the rocket approaches the target, it needs to slow down. What should the commander do? You can't use the brakes-you can't catch anything. At this point, the commander will allow the rockets to turn back, is moving in the opposite direction, and again ignition. This uses a negative acceleration, or an acceleration in the opposite direction. The force then continues to change speed, but at this time the speed is reduced, the speed will be smaller and eventually reached 0. Ideally, the rocket should be positioned several inches above the planet's surface.

Single axis acceleration

Let's put the knowledge we learned before into Flash to practice. As with the first velocity vector example, the first acceleration is only on one axis. Back in the Ball class, here is the code for the first example (acceleration1.as): package {
 import flash.display.Sprite;
 import flash.events.Event;
 public class Acceleration1 extends Sprite {
  private var ball:Ball;
  private var vx:Number=0;
  private var ax:Number=.2;
  public function Acceleration1() {
   init();
  }
  private function init():void {
   ball=new Ball  ;
   addChild(ball);
   ball.x=50;
   ball.y=100;
   addEventListener(Event.ENTER_FRAME,onEnterFrame);
  }
  private function onEnterFrame(event:Event):void {
   vx+= ax;
   ball.x+= vx;
  }
 }
}

The starting velocity vector (VX) is zero, the acceleration (AX) is 0.2, and the speed in each frame is added to the velocity vector, which is added to the position of the ball.

To test this example, we will see that the ball starts to move very slowly, and then it travels very fast to the right, moving out of the stage range.

The following is an example of a small ball that allows the ball to have both acceleration and reverse acceleration. Using the arrow keys here, we've learned how to listen for keyboard events, and we've also learnt to use the KeyCode property to find the event object that raised the event, and then call the event handler function. It is then compared to the constants in the Flash.ui.Keyboard class, which includes key-code attribute values that are appropriate for the user to read, such as Keyboard.left, Keyboard.space, Keyboard.shift, and so on. Currently, only the left and right arrow keys, Keyboard.left and Keyboard.right, are used to change the acceleration. Here is the code (ACCELERATION2.AS):

Package {
Import Flash.display.Sprite;
Import flash.events.Event;
Import flash.events.KeyboardEvent;
Import Flash.ui.Keyboard;
public class Acceleration2 extends Sprite {
private Var Ball:ball;
private var vx:number = 0;
private var ax:number = 0;
Public Function Acceleration2 () {
Init ();
}
Private Function init (): void {
Ball = new ball ();
AddChild (ball);
ball.x = STAGE.STAGEWIDTH/2;
Ball.y = STAGE.STAGEHEIGHT/2;
AddEventListener (Event.enter_frame, onenterframe);
Stage.addeventlistener (Keyboardevent.key_down, OnKeyDown);
Stage.addeventlistener (keyboardevent.key_up, onKeyUp);
}
Private Function OnKeyDown (event:keyboardevent): void {
if (Event.keycode = = Keyboard.left) {
AX =-0.2;
else if (Event.keycode = = keyboard.right) {
AX = 0.2;
}
}
Private Function OnKeyUp (event:keyboardevent): void {
Ax = 0;
}
Private Function Onenterframe (event:event): void {
VX + ax;
Ball.x + VX;
}
}
}

In this example, just check to see if the left and RIGHT arrow keys are pressed. If the left button is pressed, a negative value is set for ax. If you press the right key, it is a positive value and zero if it is not pressed. In the Onenterframe method, the velocity is assigned to the position of the object.

Testing the film will show that we can't completely control the speed of the object. That is, you cannot immediately stop the movie from moving. If the speed is too low, it will move in the opposite direction.

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.