Controlling the 3D rotation

This example demonstrates how to use ActionScript together with the 3D Box parameters, in order to create additional interaction (such as motion effects, physic effects and so on).

1. Continue to work with the 'box.fla' movie that you created in the Controlling the dimensions tutorial.

2. Add two simple buttons for "show me front" and "show me back" to the stage:


3. Add the following code to first button ("show me front"):

on (release) {
box.k = 1;
box.rx+=10;
}

 

4. Add the following code to the second button ("show me back"):

on (release) {
box.k = -1;
box.rx-=10;
}


5. Give your 3D Box the instance name of "box":



6. Add the following code to the 3D Box instance:

onClipEvent (enterFrame) {
if (k) {
// rotate the 3D box to achive horizontal position:
this.rx += k*(1-this.v3.px)/30;
this.ry += -k*(1-this.v3.py)/30;
this.rz += k*(1-this.v2.py)/80;
// stop the calculation when the 3D Box get the horizontal pozition:
if (this.rx*this.rx+this.ry*this.ry+this.rz*this.rz<.01) {
this.rx = this.ry=this.rz=0;
}
}
}
// pause the rotation on mouse press
onClipEvent (mouseDown) {
k=0;
}


This code demonstrates some interesting functionality: You can still roll the box with your mouse, but when you press "show me front" or "show me back" the box return to the horizontal position (viewed either from the front or from the back). You can tune it somewhat by changing the coefficients to '30' or '80' as well as by changing the 3D Box friction parameter.