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: バネる (質量値を追加してみたらプルプルになった。)

質量値 massを追加してみました。
ついでに計算ミスってた部分を修正。
// forked from KonKiKi's バネる
// 質量値 massを追加してみました。
//ついでに計算ミスってた部分を修正。
package{
    import flash.display.*;
    
    public class BaneBane extends Sprite{
        
        private var n:uint = 1000;
        private var springConst:Number = 0.3;
        private var damp:Number = 0.8;
        
        function BaneBane(){
            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(springConst,damp,myX,myY,mass)
                stage.addChild(ball);
            }
        }
    }     
}

import flash.display.*;
import flash.events.*;
class Ball extends Sprite{
    
    private var _sp:Number;
    private var _damp:Number;
    private var _mass:Number;
    private var _myX:Number;
    private var _myY:Number;
    private var _tagX:Number;
    private var _tagY:Number;
    private var _xVal:Number;
    private var _yVal:Number;
    private var _limit:Number = 125;
    private var _expansion:Number = 50;
    
    function Ball(s:Number,d:Number,tx:Number,ty:Number,mass:Number){
        _sp = s;
        _damp = d;
        this.x = _myX = _tagX = tx;
        this.y = _myY = _tagY = ty;
        _xVal = _yVal = 0;
        
        _mass = mass;
        
        this.graphics.beginFill(0x00CCFF,1);
        //_massの値によってballの大きさをかえる。
        this.graphics.drawCircle(0,0,_mass);
        this.graphics.endFill();
        
        this.addEventListener(Event.ENTER_FRAME,onLoop,false,0,true);
    }
    
    private function onLoop(eventObj:Event):void {
        var xx:Number = _tagX - stage.mouseX;
        var yy:Number = _tagY - stage.mouseY;
        var DIST:Number = Math.sqrt( xx * xx + yy * yy );
        
        if(DIST < _limit){
            var ANGLE:Number = Math.atan2( yy, xx );
            var LENG:Number = (_expansion / (_limit * _limit)) * (DIST - _limit) * (DIST - _limit);
            
            _myX = _tagX + LENG * Math.cos(ANGLE);
            _myY = _tagY + LENG * Math.sin(ANGLE);
            
            this.scaleX = this.scaleY = LENG / (_expansion  / _mass) * 30 + 1;
        }else {
            this.scaleX = this.scaleY = 1;
            _myX = _tagX;
            _myY = _tagY;
        }
        _xVal = valElastic(this.x,_myX,_xVal);
        _yVal = valElastic(this.y,_myY,_yVal);
        this.x += _xVal;
        this.y += _yVal;
    }
    
    private function valElastic(orig:Number,dist:Number,elas:Number):Number {
        var force:Number = -_sp * (orig - dist);
        var acceleration:Number = force / _mass;
        return _damp * (elas + acceleration);
    }
    
}