Flipping Squares
I wrote this piece of code in order to make myself familiar with five3d libs.
Nothing awesome, though.
/**
* Copyright JorgeBraccini ( http://wonderfl.net/user/JorgeBraccini )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/hpt2
*/
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.geom.Vector3D;
import flash.utils.Timer;
import gs.TweenLite;
import gs.easing.*;
import net.badimon.five3D.display.Scene3D;
import net.badimon.five3D.display.Sprite3D;
[SWF(backgroundColor="#000000", width="640", height="480")]
public class Five3DA extends Sprite {
private var _scene:Scene3D;
private var _container:Sprite3D;
private var _timer :Timer;
private var _ang_x:Number = 0;
private var _ang_y:Number = 0;
private var _move_x:Number = 0;
private var _move_y:Number = 0;
private var _angMax:Number = 20;
private var _moveMax:Number = 100;
private var _radius:Number = 200;
public function Five3DA(){
init();
}
private function init():void {
var w:Number = 150;
var h:Number = 150;
TweenLite.defaultEase = Bounce.easeOut;
_scene = new Scene3D();
_scene.x = stage.stageWidth / 2;
_scene.y = stage.stageHeight /2;
_container = new Sprite3D();
_container.childrenSorted = true;
_scene.addChild(_container);
addChild(_scene);
initializeStage(20, 20);
_timer = new Timer(33);
_timer.addEventListener(TimerEvent.TIMER, handleTick);
_timer.start();
stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
}
private function initializeStage($cols:Number, $rows:Number):void {
var w:Number = 40;
var h:Number = 40;
var finalWidth:Number = (w*2) * $cols;
var finalHeight:Number = (h*2) * $rows;
var pos:Point = new Point(-finalWidth/2, -finalHeight/2);
var square:SquareSprite3D;
for(var i:Number = 0; i < $rows; i++){
for(var j:Number = 0; j < $cols; j++){
square = new SquareSprite3D(w, h);
square.x = pos.x;
square.y = pos.y;
square.addEventListener(MouseEvent.ROLL_OVER, handleRollOver);
square.addEventListener(MouseEvent.ROLL_OUT, handleRollOut);
_container.addChild(square);
pos.x += w * 2;
}
pos.y += h * 2;
pos.x = -finalWidth/2;
}
}
private function handleMouseMove(e:MouseEvent):void {
var porc_x:Number = Math.round(stage.mouseX * 100 / stage.stageWidth);
var porc_y:Number = Math.round(stage.mouseY * 100 / stage.stageHeight);
porc_x = -((50 - porc_x) * 2);
porc_y = -((50 - porc_y) * 2);
_move_x = porc_x * _moveMax / 100;
_move_y = porc_y * _moveMax / 100;
_ang_x = porc_x * _angMax / 100;
_ang_y = porc_y * _angMax / 100;
}
private function handleTick(e:TimerEvent):void {
_container.rotationY += (_ang_x - _container.rotationY) / 5;
_container.rotationX += (_ang_y - _container.rotationX) / 5;
_scene.x += (((stage.stageWidth / 2) + (-_move_x)) - _scene.x) / 10;
_scene.y += (((stage.stageHeight / 2) + (-_move_y)) - _scene.y) / 10;
_container.rotationZ += (_ang_y - _container.rotationZ) / 20;
}
private function handleRollOver(e:MouseEvent):void {
TweenLite.to(e.target, 1, {rotationY:e.target.rotationY + 200});
}
private function handleRollOut(e:MouseEvent):void {
//TweenLite.to(e.target, .7, {z:0, scaleX:1, scaleY:1, rotationY:0});
}
}
}
import net.badimon.five3D.display.Sprite3D;
internal class SquareSprite3D extends Sprite3D {
public function SquareSprite3D($width:Number, $height:Number){
var r:Number = Math.random() * 255;
var g:Number = Math.random() * 255;
var b:Number = Math.random() * 255;
var color:uint = r << 16 | g << 8 | b;
graphics3D.beginFill(color);
graphics3D.drawRect(-$width, -$height, $width*2, $height*2);
graphics3D.endFill();
}
}