topical media & game development
#graphic-player-10-cube-be-nascom-flash10-tests-Rotation3dBehaviour.ax
#graphic-player-10-cube-be-nascom-flash10-tests-Rotation3dBehaviour.ax
[swf]
[flash]
flex
package be.nascom.flash10_tests{
import be.nascom.flash.component.SimpleGridGraphic;
import be.nascom.flex.component.FlexSimpleTraceBox;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;
import flash.geom.Point;
import mx.containers.HBox;
import mx.controls.Button;
import mx.controls.HSlider;
import mx.controls.Label;
import mx.events.SliderEvent;
public class @ax-graphic-player-10-cube-be-nascom-flash10-tests-Rotation3dBehaviour extends AbstractFP10Test{
protected var _grid_size:uint=300;
protected var _grid:SimpleGridGraphic;
protected var _test_sprite:Sprite;
protected var _nested_asset:Bitmap;
protected var _test_center_x:uint=400;
protected var _test_center_y:uint=300;
protected var _buttons_bar:HBox;
protected var _rotate_x_button:Button;
protected var _rotate_y_button:Button;
protected var _rotate_z_button:Button;
protected var _change_z_slider:HSlider;
protected var _change_z_label:Label;
protected var _center:Sprite;
protected var _center_nested_asset_button:Button;
protected var _add_drop_shadow_button:Button;
protected var _reset_button:Button;
public function @ax-graphic-player-10-cube-be-nascom-flash10-tests-Rotation3dBehaviour(){
super();
}
override protected function createChildren():void{
super.createChildren();
_buttons_bar=new HBox();
_buttons_bar.x=10;
_buttons_bar.y=10;
_rotate_x_button=new Button();
_rotate_x_button.label="rotate X";
_rotate_x_button.addEventListener(MouseEvent.MOUSE_DOWN,startRotateX);
_rotate_x_button.addEventListener(MouseEvent.MOUSE_UP,stopRotateX);
_buttons_bar.addChild(_rotate_x_button);
_rotate_y_button=new Button();
_rotate_y_button.label="rotate Y";
_rotate_y_button.addEventListener(MouseEvent.MOUSE_DOWN,startRotateY);
_rotate_y_button.addEventListener(MouseEvent.MOUSE_UP,stopRotateY);
_buttons_bar.addChild(_rotate_y_button);
_rotate_z_button=new Button();
_rotate_z_button.label="rotate Z";
_rotate_z_button.addEventListener(MouseEvent.MOUSE_DOWN,startRotateZ);
_rotate_z_button.addEventListener(MouseEvent.MOUSE_UP,stopRotateZ);
_buttons_bar.addChild(_rotate_z_button);
_change_z_label=new Label();
_change_z_label.text="change Z:";
_buttons_bar.addChild(_change_z_label);
_change_z_slider=new HSlider();
_change_z_slider.minimum=-400;
_change_z_slider.width=80;
_change_z_slider.maximum=400;
_change_z_slider.value=0;
_change_z_slider.addEventListener(SliderEvent.THUMB_RELEASE,updateItemZ);
_buttons_bar.addChild(_change_z_slider);
_center_nested_asset_button=new Button();
_center_nested_asset_button.label="Center Nested Asset";
_center_nested_asset_button.addEventListener(MouseEvent.CLICK,centerNestedAsset);
_buttons_bar.addChild(_center_nested_asset_button);
_add_drop_shadow_button=new Button();
_add_drop_shadow_button.label="Drop Shadow";
_add_drop_shadow_button.addEventListener(MouseEvent.CLICK,addDropShadow);
_buttons_bar.addChild(_add_drop_shadow_button);
_reset_button=new Button();
_reset_button.label="reset";
_reset_button.addEventListener(MouseEvent.CLICK,resetRotations);
_buttons_bar.addChild(_reset_button);
addChild(_buttons_bar);
}
override public function run():void{
FlexSimpleTraceBox.trace("Nested3dTest.run()");
createGrid();
create3dSprite();
createCenter();
}
protected function createGrid():void{
_grid=new SimpleGridGraphic(_grid_size,_grid_size,20,0xFFFFFF);
_grid.x=_test_center_x;
_grid.y=_test_center_y;
test_holder.addChild(_grid);
}
//ripped off from adobe sample code
protected function createCenter():void {
var centerRadius:int = 20;
_center = new Sprite();
_center.buttonMode=true;
_center.useHandCursor=true;
// circle
_center.graphics.lineStyle(1, 0x000000);
_center.graphics.beginFill(0xCCCCCC, 0.5);
_center.graphics.drawCircle(0, 0, centerRadius);
_center.graphics.endFill();
// cross hairs
_center.graphics.moveTo(0, centerRadius);
_center.graphics.lineTo(0, -centerRadius);
_center.graphics.moveTo(centerRadius, 0);
_center.graphics.lineTo(-centerRadius, 0);
_center.x = _test_center_x;
_center.y = _test_center_y;
_center.z = 0;
test_holder.addChild(_center);
_center.addEventListener(MouseEvent.MOUSE_DOWN, startDragProjectionCenter);
_center.addEventListener(MouseEvent.MOUSE_UP, stopDragProjectionCenter);
_center.addEventListener( MouseEvent.MOUSE_MOVE, doDragProjectionCenter);
this.updatePerspectiveProjectionCenter();
}
protected function create3dSprite():void{
_test_sprite=new Sprite();
_nested_asset=new TestAssets.BatmanImage() as Bitmap;
_test_sprite.addChild(_nested_asset);
_test_sprite.x=_test_center_x;
_test_sprite.y=_test_center_y;
test_holder.addChild(_test_sprite);
//_test_sprite.graphics.beginFill();
}
protected function resetRotations(e:Event=null):void{
if(_test_sprite==null)return;
_test_sprite.rotationX=0;
_test_sprite.rotationY=0;
_test_sprite.rotationZ=0;
_nested_asset.x=0;
_nested_asset.y=0;
_test_sprite.filters=[];
}
protected function updateItemZ(e:Event=null):void{
_test_sprite.z=int(this._change_z_slider.value);
}
protected function centerNestedAsset(e:Event=null):void{
_nested_asset.x-=_nested_asset.width/2;
_nested_asset.y-=_nested_asset.height/2;
}
protected function addDropShadow(e:Event=null):void{
_test_sprite.filters=[new DropShadowFilter(10,45,0x000000,.5,15,15,3)];
}
protected function startRotateX(e:Event=null):void{
addEventListener(Event.ENTER_FRAME,rotateX);
}
protected function rotateX(e:Event=null):void{
_test_sprite.rotationX++;
}
protected function stopRotateX(e:Event=null):void{
removeEventListener(Event.ENTER_FRAME,rotateX);
}
protected function startRotateY(e:Event=null):void{
addEventListener(Event.ENTER_FRAME,rotateY);
}
protected function rotateY(e:Event=null):void{
_test_sprite.rotationY++;
}
protected function stopRotateY(e:Event=null):void{
removeEventListener(Event.ENTER_FRAME,rotateY);
}
protected function startRotateZ(e:Event=null):void{
addEventListener(Event.ENTER_FRAME,rotateZ);
}
protected function rotateZ(e:Event=null):void{
_test_sprite.rotationZ++;
}
protected function stopRotateZ(e:Event=null):void{
removeEventListener(Event.ENTER_FRAME,rotateZ);
}
//taken from adobe sample code
protected var _in_drag:Boolean=false;//huh huh, huh huh, he said "in drag", huh huh
protected function startDragProjectionCenter(e:Event) :void{
_center.startDrag();
_in_drag = true;
}
protected function doDragProjectionCenter(e:Event) :void{
if (_in_drag){
updatePerspectiveProjectionCenter();
}
}
protected function stopDragProjectionCenter(e:Event) :void{
_center.stopDrag();
updatePerspectiveProjectionCenter();
_in_drag = false;
}
protected function updatePerspectiveProjectionCenter():void{
//root.transform.perspectiveProjection.projectionCenter = new Point(center.x, center.y);
root.transform.perspectiveProjection.projectionCenter = this.localToGlobal(new Point(_center.x, _center.y));
}
override public function destroy():void{
FlexSimpleTraceBox.trace("Nested3dTest.destroy()");
test_holder.removeChild(_grid);
_buttons_bar.removeChild(_rotate_x_button);
_rotate_x_button.removeEventListener(MouseEvent.MOUSE_DOWN,startRotateX);
_rotate_x_button.removeEventListener(MouseEvent.MOUSE_UP,stopRotateX);
_buttons_bar.removeChild(_rotate_y_button);
_rotate_y_button.removeEventListener(MouseEvent.MOUSE_DOWN,startRotateY);
_rotate_y_button.removeEventListener(MouseEvent.MOUSE_UP,stopRotateY);
_buttons_bar.removeChild(_rotate_z_button);
_rotate_z_button.removeEventListener(MouseEvent.MOUSE_DOWN,startRotateZ);
_rotate_z_button.removeEventListener(MouseEvent.MOUSE_UP,stopRotateZ);
_buttons_bar.removeChild(_change_z_label);
_change_z_slider.removeEventListener(SliderEvent.THUMB_RELEASE,updateItemZ);
_buttons_bar.removeChild(_change_z_slider);
_center_nested_asset_button.removeEventListener(MouseEvent.CLICK,centerNestedAsset);
_buttons_bar.removeChild(_center_nested_asset_button);
_add_drop_shadow_button.removeEventListener(MouseEvent.CLICK,addDropShadow);
_buttons_bar.removeChild(_add_drop_shadow_button);
_buttons_bar.addChild(_add_drop_shadow_button);
_reset_button.removeEventListener(MouseEvent.CLICK,resetRotations);
_buttons_bar.removeChild(_reset_button);
removeChild(_buttons_bar);
_center.removeEventListener(MouseEvent.MOUSE_DOWN, startDragProjectionCenter);
_center.removeEventListener(MouseEvent.MOUSE_UP, stopDragProjectionCenter);
_center.removeEventListener( MouseEvent.MOUSE_MOVE, doDragProjectionCenter);
test_holder.removeChild(_center);
}
//possibility for developer to document findings, important items to note etc. Use HTML.
override public function getNotes():String{
var note:String="How does flash place and rotate assets in '3d'?";
note+="<ul>";
note+="<li>A sprite containing a bitmap, the sprites x,y,z,rotationX,Y andZ are manipulated</li>";
note+="<li>Unlike papervision, the default 'axis' is the top left corner (not center)</li>";
note+="<li>Adjust the 'nested asset' for papervision style axis (x-=width/2 , y-=height/2)</li>";
note+="<li>DRAG the 'center' clip to adjust 'perspective center' (vanishing point), rotate pic to appreciate impact</li>";
note+="<li>Note when adjusting Z, negative means 'closer', positive is 'further'</li>";
note+="</ul>";
return note;
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.