Code Samples
Tank Shoot
Click on the example, use the keyboard arrows to rotate the tank, mouse click to shoot.
Download the Source Files for Adobe Flash CS4
Download the Source Files for Adobe Flash CS3
About the Example
This example uses the FreeSpin3D Action Script API to “shoot” a 2D cannonball (sprite) object from a 3D model (the tank). To do that I:
1. Utilized the FreeSpin3D z-sorting mechanism to sort between the 2D object and the 3D model
2. Simulated a 2D object flying in a 3D space by:
a. Giving the 2D object the x, y and z coordinates as a start point
b. Give the 2D object a speed and direction
The 2D cannon ball start 3D coordinates
I will start by saying that the starting 3D point coordinates of the cannon ball are (-3, 0, -100). How did I know that?
Before explaining, we need to know that every 3D model imported has a bounding box with dimensions of 100 from the rotation center of the 3D model, on each of the axes, therefor the starting 3D point will be somewhere in the range from (-100, -100, -100) to (100, 100, 100). To find the 3D starting point coordinates we can do one of two things, the first one is by trial and error, just play with some numbers until you get a correct position. The second and more “scientific” way is to go by the rotation center of the 3D model, in our case it is located at the center of the tank (you set that when importing the 3D model).
![]() |
![]() |
X coodinate – the cannonball should start a bit above the rotation center so we’ll give it a value of -3
Y coordinate – the cannonball starts exactly from the height of the rotation center. We’ll give it a value of 0.
Z coordinate – the cannonball starting point is at the end of the cannon, since the cannon is the longest element of the tank, we can safely give the z coordinate a value of 100. Note that positive values are closer to us where negative values are further from us.
Once we have the starting position of the 2D object, we can use the RviModelToParent method to translate the 3D position of the tip of the cannon from the model coordinates system to the stage one.
The FLA
In the FLA I imported a 3D model of a tank using the FreeSpin3D import Engine and created an instance of it on the stage. I created a Movieclip for the cannon ball.
The Adobe Flash CS4 Actions Layer (If using Adobe Flash CS3, see code in box below)
import FreeSpin3D.CRvFreeSpin3D;
import FreeSpin3D.IRvFreeSpin3D;
// the speed of the cannonball
const speed:Number = 20;
// note that the 3D model depth is set to true (at depth 0) in the FreeSpin3D Control Panel
// This could also be done by code using the following:
// my3DModel_fs.RviSetDepthSorting(true, 0);
// add the cannonball to the depth sorting space
my3DModel_fs.RviAddMovieClipToDepthSorting(Bullet, 0);
// add a mouse click listener to the stage
stage.addEventListener(MouseEvent.CLICK, OnMouseClick);
// add a behavior function to the tank 3D model instance
my3DModel_fs.RviBehaviorFunction = OnEnterFrame;
function OnEnterFrame(model_fs:IRvFreeSpin3D):void
{
if (my3DModel_fs.RviAvpY > 8)
{
my3DModel_fs.visible = false;
Plane.visible = false;
Bullet.visible = false;
return;
}
The Adobe Flash CS3 Actions Layer
mport FreeSpin3D.CRvFreeSpin3D;
// Set the speed of the cannon ball
const speed:Number = 20;
// Note: The 3D model depth is set to true (at depth 0) in the FreeSpin3D Control Panel
// This can also be set by using the FreeSpin3D Action Script API:
// my3DModel_fs.RviSetDepthSorting(true, 0);
// Add the cannon ball to the tank instance depth sorting space.
my3DModel_fs.RviAddMovieClipToDepthSorting(Bullet,0);
// Add listeners to the stage.
stage.addEventListener(MouseEvent.CLICK, OnMouseClick);
stage.addEventListener(Event.ENTER_FRAME, OnEnterFrame);
function OnEnterFrame(e:Event):void {
if (my3DModel_fs.RviGetFrameY()>7)
{
my3DModel_fs.visible = false;
plane.visible = false;
return;
}
my3DModel_fs.visible = true;
plane.visible = true;
if(Bullet.Direction == null)
return;
// Move the cannon ball based on the direction of the tank when the fire was triggered (mouse click)
Bullet.x += Bullet.Direction[0] * speed;
Bullet.y += Bullet.Direction[1] * speed;
Bullet.z += Bullet.Direction[2] * speed;
}
function OnMouseClick(e:MouseEvent):void {
// Translate the coordinates of the tip of the cannon from model coordinates to stage coordinates
var parentCoordinates = my3DModel_fs.RviModelToParent(-3, 0, -100);
// Set these coordinates to the cannon ball as the start point
Bullet.x = parentCoordinates[0]; // x coordinate
Bullet.y = parentCoordinates[1]; // y coordinate
Bullet.z = parentCoordinates[2]; // z coordinate
// Add tank direction vector to the bullet
Bullet.Direction = my3DModel_fs.RviVectorForward;
}



