forked from: forked from: forked from: forked from: snow

by s8t1h12akj
AS100本ノック
9回目のお題は「雪」
あなたなりの「雪」を表現してください。

時間がないのでパラメータとかテキトーになた。
♥0 | Line 176 | Modified 2015-01-15 15:10:17 | MIT License | (replaced)
play

ActionScript3 source code

/**
 * Copyright s8t1h12akj ( http://wonderfl.net/user/s8t1h12akj )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/v2a3
 */

// forked from s8t1h12akj's forked from: forked from: forked from: snow
// forked from s8t1h12akj's forked from: forked from: snow
// forked from s8t1h12akj's forked from: snow
// forked from NewKrok's snow
// forked from j2013's forked from: forked from: 【AS100本ノック】9回目:雪
// forked from mex_ny's forked from: 【AS100本ノック】9回目:雪
// forked from mex's 【AS100本ノック】9回目:雪
/* 
 * AS100本ノック
 * 9回目のお題は「雪」
 * あなたなりの「雪」を表現してください。
 * 
 * 時間がないのでパラメータとかテキトーになた。
 */
package {
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.StageQuality;
    import flash.events.Event;
    import flash.display.*;
    import flash.net.URLRequest;
    import flash.system.LoaderContext;

    public class Snow extends Sprite {
        private const SNOW_PARTICLE_NUMBER:uint = 300;
        private var _stageWidht:Number , _stageHeight:Number;

        public function Snow() {
            if ( stage ) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            loadImage ( "http://assets.wonderfl.net/images/related_images/7/78/781e/781e2b3788c27784d74bd4b876b3fd13b0c69c6am", onLoadComplete )
            removeEventListener(Event.ADDED_TO_STAGE, init);
            
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.quality = StageQuality.BEST;
            _stageWidht = stage.stageWidth;
            _stageHeight = stage.stageHeight;
            Wonderfl.capture_delay( 3 );
            
            addBackground();
            addSnow();
            changeDepth()
        }
        
        private function loadImage ( $url:String, $onComplete:Function ) :void {
            var imageLoader:Loader = new Loader ();
            imageLoader.contentLoaderInfo.addEventListener ( Event.COMPLETE, $onComplete );
            var image:URLRequest = new URLRequest ( $url );
            imageLoader.load ( image, new LoaderContext ( true ) );
        }
        
        private function onLoadComplete(event:Event):void {
          var back:DisplayObject = addChildAt ( LoaderInfo(event.target).content,0);
          back.width = stage.stageWidth
          back.scaleY = back.scaleX;
          if (  back.height < stage.stageHeight) {
              back.height = stage.stageHeight
              back.scaleX = back.scaleY;
              back.x = stage.stageWidth / 2 - back.width / 2
          }

        }
        
        private function addBackground():void
        {
        }
        
        private function addSnow():void
        {
            var i:uint , snowParticle:SnowParticle;
            
            for ( i = 1; i <= SNOW_PARTICLE_NUMBER; i++ )
            {
                addChild( snowParticle = new SnowParticle( _stageWidht , _stageHeight ) );
                snowParticle.name = i.toString();
            }
        }
        
        private function changeDepth():void
        {
            var i:uint , snowParticle:SnowParticle;

            for ( i = 1; i <= SNOW_PARTICLE_NUMBER; i++ )
            {
                snowParticle = getChildByName( i.toString() ) as SnowParticle;
                if ( snowParticle.isBig )
                    swapChildrenAt( getChildIndex(snowParticle ) , numChildren - 1 );
            }
        }
    }
}

import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Graphics;
import flash.events.Event;
import flash.filters.BlurFilter;
    
class SnowParticle extends Sprite
{
    private const PARTICLE_MAX_SIZE:uint = 60;
    private const MAX_SPPED:uint = 11;

    private var _particle:Shape;
    private var _size:Number;
    private var _stageWidth:uint;
    private var _stageHeight:uint;
    private var _sx:Number;
    private var _sy:Number;
    private var _isBig:Boolean = false;
    private var _basePosition:Number;

    public function SnowParticle( stageWidth:uint , stageHeight:uint ):void
    {
        _stageWidth = stageWidth;
        _stageHeight = stageHeight;
        init();
    }
    
    private function init():void
    {
        checkBig();
        addSnow();
        setPosition();
        setSpeed();
        setFilter();
        moveSnow();
    }
    
    private function checkBig():void
    {
        if ( Math.round( Math.random() * 10 ) == 10 )
            _isBig = true;
    }
    
    private function addSnow():void
    {
        var g:Graphics , i:uint , pi:Number , angle:Number;
        
        addChild( _particle = new Shape() );
        
        if ( !_isBig )
        {
            _size = Math.random() * PARTICLE_MAX_SIZE / 10;
            _particle.alpha = _size / ( PARTICLE_MAX_SIZE / 10 ) * Math.random() + 0.3;
        }
        else
        {
            _size = Math.random() * PARTICLE_MAX_SIZE + PARTICLE_MAX_SIZE / 4;
            _particle.alpha = Math.random() * 0.7 + 0.2;
        }

        g = _particle.graphics;
        g.beginFill( 0xffffff );
        g.moveTo( _size , 0 );
        
        pi = Math.PI / 180;
        for( i = 0; i < 6;i++)
        {
            angle = 60 * i;
            g.lineTo( _size * Math.cos( pi * angle ) , _size * Math.sin( pi * angle ) );
        }
        g.lineTo( _size , 0);
    }
    
    private function setPosition():void
    {
        _particle.x = Math.random() * _stageWidth;
        _particle.y = Math.random() * _stageHeight * -1;
        
        _basePosition = _size >> 1;
    }
    
    private function setSpeed():void
    {
        var speedY:Number;
        
        if( !_isBig )
            speedY = Math.random() * MAX_SPPED +  + ( MAX_SPPED >> 1 );
        else
            speedY = Math.random() * MAX_SPPED * 6 + ( MAX_SPPED >> 1 );
        _sy = speedY;
        _sx = ( Math.random() * 1 < 0.6 ) ? Math.random() * ( MAX_SPPED >> 1 ) : Math.random() * ( MAX_SPPED >> 1 ) * -1;
    }
    
    private function setFilter():void
    {
        if( !_isBig )
            _particle.filters = [ new BlurFilter( _size << 1 , _size << 1 ) ];
        else
            _particle.filters = [ new BlurFilter( 32 , 32 ) ];
    }

    private function moveSnow():void
    {
        _particle.addEventListener( Event.ENTER_FRAME , enterFrameHandler );
    }
    
    private function enterFrameHandler( $evt:Event ):void
    {
        _particle.scaleX = _particle.scaleY = Math.random() * 1 + 0.8;
        if ( _particle.scaleX > 1 ) _particle.scaleX = _particle.scaleY = 1;

        _particle.x += _sx;
        _particle.y += _sy;
        
        if ( _particle.y - _basePosition > _stageHeight || _particle.x + _basePosition < 0 || _particle.x - _basePosition > _stageWidth )
        {
            setPosition();
            setSpeed();
            setFilter();
        }
    }
    
    public function get isBig():Boolean { return _isBig; }
}