Rotate MEGANE
//////////////////////////////////////
*
* ぐるぐるめがね
* スクリーンショット?の撮り方わからん。
*
/
///////////////////////////////////
/**
* Copyright glasses_factory ( http://wonderfl.net/user/glasses_factory )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/9CXk
*/
/*//////////////////////////////////////
*
* ぐるぐるめがね
* スクリーンショット?の撮り方わからん。
*
/*//////////////////////////////////////
package
{
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.display.Sprite;
import flash.events.Event;
import frocessing.shape.FShapeSVG;
[SWF(backgroundColor=0)]
public class Study1 extends Sprite
{
/*//////////////////////////////////////
/* public variables
/*//////////////////////////////////////
/*//////////////////////////////////////
/* getter
/*//////////////////////////////////////
/*//////////////////////////////////////
/* setter
/*//////////////////////////////////////
/*//////////////////////////////////////
/* public methods
/*//////////////////////////////////////
//Constractor
public function Study1()
{
super();
init();
}
/*//////////////////////////////////////
/* private methods
/*//////////////////////////////////////
public function init():void
{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener( Event.COMPLETE, loadedHandler );
loader.load(new URLRequest( "http://glasses-factory.net/test/glasses.svg"));
}
private function loadedHandler( e:Event ):void
{
var svgXML:XML = new XML(e.target.data);
var fshape:FShapeSVG = new FShapeSVG( svgXML );
var sp:Sprite = fshape.toSprite();
_megane = new RotateObj( sp );
addChild( _megane );
e.target.removeEventListener( Event.COMPLETE, loadedHandler );
}
/*//////////////////////////////////////
/* private variables
/*//////////////////////////////////////
private var _megane:RotateObj;
}
}
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
class RotateObj extends Sprite
{
public function RotateObj( sp:Sprite )
{
_sp = sp;
_sp.scaleX = _sp.scaleY = 0.35;
addChild( _sp );
this.buttonMode = true;
this.mouseEnabled = true;
addEventListener( MouseEvent.MOUSE_DOWN, dragStartHandler );
addEventListener( MouseEvent.MOUSE_UP, dragEndHandler );
}
private function dragStartHandler( e:MouseEvent ):void
{
removeEventListener(Event.ENTER_FRAME,enterFrameHandler );
addEventListener(Event.ENTER_FRAME,dragingHandler );
root.stage.addEventListener(MouseEvent.MOUSE_UP,dragEndHandler );
_mx = root.stage.mouseX;
_my = root.stage.mouseY;
var localPt:Point = this.globalToLocal( new Point( _mx, _my ));
_xpos = _mx;
_ypos = _my;
_sp.x = localPt.x * -1;
_sp.y = localPt.y * -1;
_vx = 0;
_vy = 0;
_vr = 0;
_first_action = true;
setPos();
e.updateAfterEvent();
}
private function dragEndHandler( e:MouseEvent ):void
{
root.stage.removeEventListener(MouseEvent.MOUSE_UP,dragEndHandler);
var toGlobalPt:Point = this.localToGlobal(new Point( _sp.x, _sp.y));
_xpos = toGlobalPt.x;
_ypos = toGlobalPt.y;
_sp.x = 0;
_sp.y = 0;
_dmx = 0;
_dmy = 0;
setPos();
removeEventListener(Event.ENTER_FRAME,dragingHandler);
addEventListener(Event.ENTER_FRAME,enterFrameHandler);
e.updateAfterEvent();
}
private function enterFrameHandler( e:Event ):void
{
_xpos += _vx;
_ypos += _vy;
_r -= _vr;
_vx *= 0.95;
_vy *= 0.95;
_vr *= 0.8;
colliCheck();
setPos();
checkStop();
}
private function dragingHandler( e:Event ):void
{
_mx = root.stage.mouseX;
_my = root.stage.mouseY;
if(_first_action)
{
_first_action = false;
_dmx = 0;
_dmy = 0;
}
else
{
_dmx = _mx - _mx0;
_dmy = _my - _my0;
}
_mx0 = _mx;
_my0 = _my;
_fx = ( _dmx - _vx) * _mass;
_fy = ( _dmy - _vy) * _mass;
var rx:Number = _mx - _xpos;
var ry:Number = _my - _ypos;
var mcLength:Number;
var mcf:Number;
var torque:Number;
if( _fx == 0)
{
mcLength = rx;
mcf = _fy;
torque = mcf * mcLength;
}
else
{
if( _fy == 0)
{
mcLength = ry;
mcf = _fx;
torque = (0 - mcf) * mcLength;
}
else
{
var k:Number= _fy / _fx;
var kx:Number = ((0 - k) * k * rx + k * ry) / ((0 - k) * k - 1);
var ky:Number = k * (kx - rx) + ry;
mcLength = Math.sqrt(kx * kx + ky * ky);
mcf = Math.sqrt( _fx * _fx + _fy * _fy);
if ( _fx * ky > 0)
{
torque = (0 - mcf) * mcLength;
}
else
{
torque = mcf * mcLength;
}
}
}
_vx = _dmx;
_vy = _dmy;
_xpos += _vx;
_ypos += _vy;
_vr -= torque / _inertia;
var cos:Number = Math.cos( _vr / 180 * Math.PI);
var sin:Number = Math.sin( _vr / 180 * Math.PI);
_addX = _dx * cos + _dy * sin - _dx;
_addY = ( 0 - _dx) * sin + _dy * cos - _dy;
_xpos -= _addX;
_ypos -= _addY;
_r -= _vr * 0.5;
_r %= 360;
_vx /= _b;
_vy /= _b;
_vr /= _b;
setPos();
}
private function colliCheck():void
{
var loc:Point = _sp.localToGlobal(new Point( _sp.x, _sp.y));
var len:Object = { t:0, r:0, b:0, l:0};
var deg:Number = _r % 360;
if( deg < 0 ) deg += 360;
switch(true)
{
/*bottom_right*/
case (deg <= 90):
len.t = 0;
len.r = _sp.width * Math.cos( _r * Math.PI / 180);
len.b = this.height;
len.l = _sp.height * Math.cos(( _r + 90) * Math.PI / 180);
break;
/*top_right*/
case (deg <= 180):
len.t = _sp.y + _sp.height * Math.sin(( _r + 90 ) * Math.PI/180 );
len.r = 0;
len.b = _sp.width * Math.sin(_r * Math.PI / 180 );
len.l = this.width * -1;
break;
/*top_left*/
case (deg <= 270):
len.t = this.height * -1;
len.r = _sp.height * Math.cos(( _r + 90) * Math.PI / 180 );
len.b = 0;
len.l = _sp.width * Math.cos( _r * Math.PI / 180 );
break;
/*bottom_left*/
default:
len.t = _sp.width * Math.sin( _r * Math.PI / 180 );
len.r = this.width;
len.b = _sp.y + _sp.height * Math.sin(( _r + 90) * Math.PI / 180 );
len.l = 0;
break;
}
/*collision stage top*/
if( loc.y + len.t < 0)
{
_ypos = 0 + (len.t * -1) + 1;
_vy *= -0.7;
_vr *= -0.8;
}
/*collision stage right*/
if(loc.x + len.r > root.stage.stageWidth)
{
_xpos = root.stage.stageWidth - len.r;
_vx *= -0.7;
_vr *= -0.8;
}
/* collision stage bottom*/
if(loc.y + len.b > root.stage.stageHeight)
{
_ypos = root.stage.stageHeight - len.b;
_vy *= - 0.7;
_vr *= -0.8;
}
/*collision stage left*/
if(loc.x + len.l < 0)
{
_xpos = 0 +(len.l * -1) +1;
_vx *= -0.7;
_vr *= -0.8;
}
}
private function setPos():void
{
this.x = _xpos;
this.y = _ypos;
this.rotation = _r;
}
private function checkStop():void
{
if(Math.abs( _vx ) < 0.2 && Math.abs( _vy) < 0.2 && Math.abs( _vr) < 0.001)
{
_vx = 0;
_vy = 0;
_vr = 0;
removeEventListener( Event.ENTER_FRAME, enterFrameHandler);
}
}
private var _sp:Sprite;
private var _xpos:Number = 0, _ypos:Number = 0, _r:Number = 0;
private var _mx:Number = 0, _my:Number = 0, _mx0:Number = 0, _my0:Number = 0;
private var _dx:Number = 0, _dy:Number = 0, _dmx:Number = 0, _dmy:Number = 0;
private var _addX:Number = 0, _addY:Number = 0;
private var _vx:Number = 0, _vy:Number = 0, _vr:Number = 0;
private var _fx:Number = 0, _fy:Number = 0, _fr:Number = 0;
private var _mass:Number = 1;
private var _b:Number = 1.05;
private var _inertia:Number = 140;
private var _first_action:Boolean;
}