Animating the Tank Rotation


At this point, it became obvious that the turrets would have to turn independently of the tank body movie clips so that the tanks could move to the new location without causing the turrets to point the wrong way. Inside the tank symbols, I embedded the turret into a new movie clip and gave it an instance name of turret so that it could be rotated whenever the rotation control was changed.

When the user clicks on the player1target movie clip, the tank needs to rotate toward where the mouse is clicked and then move to that point. First I initialized a few variables by placing the following code on the player1tank movie clip instance:

 onClipEvent (load) {     // How long the tween should take      frames = 6;      // Initialize frame counter that paces movement of the tank      currentframe = 0;      // Initialize vars that activate rotation and movement      rotation = false;      motion = false;  } 

The frames variable was used to determine the number of frames to use to animate the tank movement. The currentframe variable was used in a loop to actually move the tank. Two new variables, rotation and motion, were initialized and were used to trigger the animations.

To calculate how far and which direction to rotate the tank, on the player1tank movie symbol, I replaced the if statement that evaluates the hitTest() function in the onClipEvent(mouseDown) event with this code:

 if (_root.player1target.hitTest(_root._xmouse, _root._ymouse, true))) {     // Calculate X and Y distances      deltaX = _root._xmouse-this._x;      deltaY = _root._ymouse-this._y;      // Calculate the degrees to rotate the tank      radians = Math.atan2(deltaY, deltaX);      degrees = Math.round(((180*radians)/Math.PI));      degrees = _root.fixAngle(degrees);      // Determine whether to rotate tank left or right      direction = 1;      if (_root.fixAngle(degrees-_root.fixAngle(this.body._rotation))>180)  {         direction = -1;      }  } 

The hitTest() function checks to see whether the mouse pointer's coordinates intersect with Player 1's target. If they do, the x and y distances between the mouse click and the tank are calculated. Flash 5's built-in arctangent function, Math.atan2(), then uses those values to calculate the angle between the two points, in radians. That number is converted to degrees and is rounded to the nearest integer with the built-in Math.round() function. That number is shifted to the range of 0 “360 with the new fixAngle() function on frame 1 of the main timeline. An if statement calculates how far to rotate the tank by comparing the angle of the tank body's current rotation to the angle between the tank and the mouse click.

The fixAngle() function on frame 1 of the main timeline is used to determine which way to rotate the tank. The two angles are compared. If the result is greater than 180, a new variable, rotval, is set to “5 so that the tank will be rotated to the left; otherwise , it is set to 5 so that it will rotate to the right. At this point, the degrees and rotval variables store how many degrees and which direction the tank needs to rotate.



Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net