rings
//////////////////////////////////////////////////////////////////////////////
ローディングで良く使うリングを回してみた
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright twistcube ( http://wonderfl.net/user/twistcube )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/5pit
*/
////////////////////////////////////////////////////////////////////////////////
//
// ローディングで良く使うリングを回してみた
//
////////////////////////////////////////////////////////////////////////////////
package
{
import flash.display.* ;
import flash.events.* ;
import flash.net.* ;
import flash.system.* ;
[SWF(width="500", height="500", backgroundColor="0x191919", frameRate="30")]
public class MyRing extends Sprite
{
private var _list:Array ;
private var _delayX:Number ;
private var _delayY:Number ;
public function MyRing( )
{
init( ) ;
}
private function init( ):void
{
stage.scaleMode = StageScaleMode.NO_SCALE ;
stage.align = StageAlign.TOP_LEFT ;
stage.quality = StageQuality.HIGH ;
_list = new Array( ) ;
_delayX = stage.stageWidth / 2 ;
_delayY = stage.stageHeight / 2 ;
addEventListener( Event.ENTER_FRAME, onRender ) ;
}
private function onRender( event:Event ):void
{
_delayX += ( stage.mouseX - _delayX ) * .2 ;
_delayY += ( stage.mouseY - _delayY ) * .2 ;
if ( Math.random( ) < .15 )
{
var nLength:uint = _list.length ;
if ( nLength < 40 )
{
var tmpColor:Number ;
var nRandom:Number = Math.random( ) ;
if ( nRandom < .33 )
{
tmpColor = 0x60702d ;
}
else if ( nRandom < .66 )
{
tmpColor = 0xe6fa87 ;
}
else
{
tmpColor = 0xaab232 ;
}
var obj:CircleObj = new CircleObj( 1, 1 + 1 * Math.random( ), 180 * Math.random( ) / 180 * Math.PI, 360 * Math.random( ), tmpColor ) ;
obj.addEventListener( CircleObj.ON_COMPLETE, onComplete ) ;
obj.x = _delayX ;
obj.y = _delayY ;
addChild( obj ) ;
_list.push( obj ) ;
}
}
}
private function onComplete( event:Event ):void
{
var nLength:uint = _list.length ;
var i:uint = 0 ;
while ( i < nLength )
{
var o:CircleObj = _list[ i ] ;
if ( o.isHide )
{
o.removeEventListener( CircleObj.ON_COMPLETE, onComplete ) ;
removeChild( o ) ;
o = null ;
_list.splice( i, 1 ) ;
nLength = _list.length ;
}
else
{
i++ ;
}
}
}
}
}
import flash.display.* ;
import flash.events.* ;
class CircleObj extends Sprite
{
public static var ON_COMPLETE:String = "on_complete" ;
private var _isHide:Boolean = false ;
private var _hankei:uint ;
private var _hutosa:uint ;
private var _nagasa:Number ;
private var _rotation:Number ;
private var _color:Number ;
private var _mask:Sprite ;
private var _main:Sprite ;
private var _nStep:Number ;
private var _nFutosaAccel:Number ;
private var _nLotationAccel:Number ;
private var _nNagasaAccel:Number ;
public function CircleObj( __hankei:uint = 50, __hutosa:uint = 1, __nagasa:Number = 2*Math.PI, __rotation:Number = 0, __color:Number = 0 ):void
{
_hankei = __hankei ;
_hutosa = __hutosa ;
_nagasa = __nagasa ;
_rotation = __rotation ;
_color = __color ;
_nStep = 1 ;
_nFutosaAccel = 0.1 ;
_nLotationAccel = 1 + 4 * Math.random( ) ;
_nNagasaAccel = .06 + .05 * Math.random( ) ;
addEventListener( Event.ADDED_TO_STAGE, init ) ;
addEventListener( Event.REMOVED_FROM_STAGE, unload ) ;
}
private function init( event:Event ):void
{
removeEventListener( Event.ADDED_TO_STAGE, init ) ;
// --
_mask = new Sprite( ) ;
_main = new Sprite( ) ;
addChild( _main ) ;
addChild( _mask ) ;
_main.mask = _mask ;
addEventListener( Event.ENTER_FRAME, onRender ) ;
}
private function unload( event:Event = null ):void
{
removeEventListener( Event.REMOVED_FROM_STAGE, unload ) ;
removeEventListener( Event.ENTER_FRAME, onRender ) ;
removeChild( _main ) ;
removeChild( _mask ) ;
_main = null ;
_mask = null ;
}
public function get isHide( ):Boolean
{
return _isHide ;
}
private function onRender( event:Event ):void
{
if ( _nagasa != 0 )
{
xDrawMask( ) ;
xDrawCircle( ) ;
xChange( ) ;
}
}
private function xDrawMask( ):void
{
var nHankei:Number = ( _hankei + _hutosa ) * 1.41422 ; // http://www.twist-cube.com/wonderfl/ring_sample.png
var nTheta:Number ;
var nX:Number ;
var nY:Number ;
_mask.graphics.clear( ) ;
_mask.graphics.beginFill( 0xFFFFFF ) ;
// right bottom
nTheta = Math.min( _nagasa, Math.PI / 2 ) ;
nX = nHankei * Math.cos( nTheta ) ;
nY = nHankei * Math.sin( nTheta ) ;
_mask.graphics.moveTo( 0, 0 ) ;
_mask.graphics.lineTo( nHankei, 0 ) ;
_mask.graphics.lineTo( nX, nY ) ;
_mask.graphics.lineTo( 0, 0 ) ;
_main.graphics.endFill( ) ;
// left bottom
if ( _nagasa > Math.PI / 2 )
{
nTheta = Math.min( (_nagasa - Math.PI / 2), Math.PI / 2 ) + Math.PI / 2 ;
nX = nHankei * Math.cos( nTheta ) ;
nY = nHankei * Math.sin( nTheta ) ;
_mask.graphics.moveTo( 0, 0 ) ;
_mask.graphics.lineTo( 0, nHankei ) ;
_mask.graphics.lineTo( nX, nY ) ;
_mask.graphics.lineTo( 0, 0 ) ;
_main.graphics.endFill( ) ;
}
// left top
if ( _nagasa > Math.PI )
{
nTheta = Math.min( (_nagasa - Math.PI), Math.PI / 2 ) + Math.PI ;
nX = nHankei * Math.cos( nTheta ) ;
nY = nHankei * Math.sin( nTheta ) ;
_mask.graphics.moveTo( 0, 0 ) ;
_mask.graphics.lineTo( -nHankei, 0 ) ;
_mask.graphics.lineTo( nX, nY ) ;
_mask.graphics.lineTo( 0, 0 ) ;
_main.graphics.endFill( ) ;
}
// right top
if ( _nagasa > Math.PI + Math.PI/2 )
{
nTheta = Math.min( (_nagasa - (Math.PI + Math.PI/2)), Math.PI / 2 ) + Math.PI + Math.PI/2 ;
nX = nHankei * Math.cos( nTheta ) ;
nY = nHankei * Math.sin( nTheta ) ;
_mask.graphics.moveTo( 0, 0 ) ;
_mask.graphics.lineTo( 0, -nHankei ) ;
_mask.graphics.lineTo( nX, nY ) ;
_mask.graphics.lineTo( 0, 0 ) ;
_main.graphics.endFill( ) ;
}
}
private function xDrawCircle( ):void
{
_main.graphics.clear( ) ;
_main.graphics.beginFill( _color );
_main.graphics.drawCircle( 0, 0, _hankei + _hutosa ) ;
_main.graphics.drawCircle( 0, 0, _hankei ) ;
_main.graphics.endFill( ) ;
}
private function xChange( ):void
{
_hankei += _nStep ;
_nStep += 0.14 ;
_hutosa += _nFutosaAccel ;
_rotation += _nLotationAccel ;
_nagasa += _nNagasaAccel ;
_nagasa = Math.min( 2 * Math.PI, _nagasa ) ;
_nFutosaAccel *= 1.08 ;
_nLotationAccel *= 1.01 ;
_nNagasaAccel *= 1.04 ;
if ( _nagasa == 2 * Math.PI )
{
alpha -= .03 ;
if ( alpha < .05 )
{
_isHide = true ;
dispatchEvent( new Event( ON_COMPLETE ) ) ;
}
}
rotation = _rotation ;
}
}