In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: forked from: バネる (修正版)

エンジン部分を修正。
* 各パーティクルにaddEventListenerしていたのを、
* balls:Arrayに各パーティクルを入れて処理する方法に変更。
* この方が負荷がすくない? この辺良くわかってません (>x<)
// forked from KonKiKi's forked from: バネる (質量値を追加してみたらプルプルになった。)
// forked from KonKiKi's バネる
/* エンジン部分を修正。
* 各パーティクルにaddEventListenerしていたのを、
* balls:Arrayに各パーティクルを入れて処理する方法に変更。
* この方が負荷がすくない? この辺良くわかってません (>x<)
*/
package{
    import flash.display.*;
    import flash.events.*;
    
    public class BaneBane extends Sprite{
        
        private var balls:Array;
        private var n:uint = 1000;
        //バネ定数
        private var springConst:Number = 0.3;
        //抵抗値
        private var damp:Number = 0.8;
        //マウスに反応する半径
        private var limitLeng:Number = 125;
        //バネで伸びる半径の最大値
        private var expansion:Number = 50;
        
        function BaneBane(){
            balls = new Array();
            init();
        }
        
        private function init():void {
            for(var i:uint=0; i<n; i++){
                var myX:Number = Math.random() * stage.stageWidth;
                var myY:Number = Math.random() * stage.stageHeight;
                var mass:Number = Math.random() + 0.1;
            
                var ball:Ball = new Ball(myX,myY,mass)
                balls.push(ball);
                stage.addChild(ball);
            }
            stage.addEventListener(Event.ENTER_FRAME,onLoop,false,0,true);
        }
        
        private function onLoop(eventObj:Event):void {
            for(var i:uint = 0; i < balls.length; i++){
                
                var xx:Number = balls[i]._tagX - stage.mouseX;
                var yy:Number = balls[i]._tagY - stage.mouseY;
                var DIST:Number = Math.sqrt( xx * xx + yy * yy );
                
                if(DIST < limitLeng){
                    var ANGLE:Number = Math.atan2( yy, xx );
                    var LENG:Number = (expansion / (limitLeng * limitLeng)) * (DIST - limitLeng) * (DIST - limitLeng);
                    
                    balls[i]._myX = balls[i]._tagX + LENG * Math.cos(ANGLE);
                    balls[i]._myY = balls[i]._tagY + LENG * Math.sin(ANGLE);
                    
                    balls[i].scaleX = balls[i].scaleY = LENG / (expansion  / balls[i]._mass) * 30 + 1;
                    
                }else {
                        balls[i].scaleX = balls[i].scaleY = 1;
                        balls[i]._myX = balls[i]._tagX;
                        balls[i]._myY = balls[i]._tagY;
                }
                    
                balls[i]._xVal = valElastic(balls[i].x,balls[i]._myX,balls[i]._xVal,balls[i]._mass);
                balls[i]._yVal = valElastic(balls[i].y,balls[i]._myY,balls[i]._yVal,balls[i]._mass);
                balls[i].x += balls[i]._xVal;
                balls[i].y += balls[i]._yVal;
                
            }
        }
        
        private function valElastic(orig:Number,dist:Number,elas:Number,mass:Number):Number {
            var force:Number = -springConst * (orig - dist);
            var acceleration:Number = force / mass;
            return damp * (elas + acceleration);
        }
    }     
}

import flash.display.Sprite;

class Ball extends Sprite{
    //質量値
    public var _mass:Number;
    
    public var _myX:Number;
    public var _myY:Number;
    public var _tagX:Number;
    public var _tagY:Number;
    public var _xVal:Number;
    public var _yVal:Number;
    
    function Ball(tx:Number,ty:Number,mass:Number){
      
        this.x = _myX = _tagX = tx;
        this.y = _myY = _tagY = ty;
        _xVal = _yVal = 0;
        
        _mass = mass;
        
        this.graphics.beginFill(0x00CCFF,1);
        //質量値によってballの大きさをかえる。
        this.graphics.drawCircle(0,0,_mass);
        this.graphics.endFill();
    }
}