Chapter 15 Example 2
/**
* Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/Aphq
*/
package {
import flash.display.*;
import flash.events.MouseEvent;
import flash.geom.*;
public class ch15ex2 extends Sprite {
public function ch15ex2() {
stage.quality = StageQuality.LOW;
stage.addEventListener(MouseEvent.CLICK, onClick);
}
protected function onClick(event:MouseEvent):void {
var cube:Cube = new Cube(20);
cube.x = event.localX;
cube.y = event.localY;
addChild(cube);
var normalizedMousePosition2D:Point = new Point(
stage.mouseX / stage.stageWidth * 2 - 1,
stage.mouseY / stage.stageHeight * 2 - 1);
cube.angularVelocity.x = Math.pow(normalizedMousePosition2D.y, 3) * 10;
cube.angularVelocity.y = Math.pow(normalizedMousePosition2D.x, 3) * 10;
}
}
}
import flash.display.*;
import flash.events.Event;
import flash.geom.Vector3D;
class Cube extends Sprite {
public var angularVelocity:Vector3D;
public function Cube(size:Number = 50, ang:Vector3D = null) {
for (var side:int = 0; side < 6; side++) {
var face:Sprite = new Sprite();
face.blendMode = BlendMode.MULTIPLY;
var brightness:int = 0x80 + 0x80 * Math.random();
var color:int = brightness << 16 | brightness << 8 | brightness;
face.graphics.beginFill(color, 0.8);
face.graphics.drawRect(-size/2, -size/2, size, size);
face.graphics.endFill();
addChild(face);
}
getChildAt(0).rotationY = 90; getChildAt(0).x = -size/2;
getChildAt(1).rotationY = -90; getChildAt(1).x = size/2;
getChildAt(2).z = -size/2;
getChildAt(3).rotationY = 180; getChildAt(3).z = size/2;
getChildAt(4).rotationX = 90; getChildAt(4).y = size/2;
getChildAt(5).rotationX = -90; getChildAt(5).y = -size/2;
this.angularVelocity = (ang)? ang : new Vector3D(0, 0, 0);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
protected function onEnterFrame(event:Event):void {
this.rotationX += angularVelocity.x;
this.rotationY += angularVelocity.y;
this.rotationZ += angularVelocity.z;
}
}