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

Ball (挙動がおかしい)

Get Adobe Flash player
by 178ep3 23 May 2009
/**
 * Copyright 178ep3 ( http://wonderfl.net/user/178ep3 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cacs
 */

package 
{
	import flash.display.Sprite;
	import flash.events.Event;

	public class Hit extends Sprite
	{
		private var _numBall:uint = 20;
		private var _list:Array = new Array(_numBall);
		
		public function Hit()
		{
                        this.graphics.beginFill(0x000000);
                        this.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
                        this.graphics.endFill();
			init();
			addEventListener(Event.ENTER_FRAME,loop);
		}
		
		private function init():void
		{
			var i:uint;
			for(i=0; i<_numBall; i++)
			{
				_list[i] = new Ball(i,stage,_numBall);
				addChild(_list[i]);
			}
			for(i=0; i<_numBall; i++)
			{
				_list[i].setList(_list);
			}
		}
		
		private function loop(e:Event):void
		{
			for(var i:uint=0; i<_numBall; i++)
			{
				_list[i].collide();
				_list[i].Move();
			}
		}
	}
}

import flash.display.Sprite;
import flash.display.Stage;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.events.Event;

class Ball extends Sprite
{
	private var _spring:Number = 0.5;
	private var _gravity:uint = 2;
	private var _friction:Number = -0.5;
	private var _numBall:uint;
		
	public var _size:uint;
	public var _vx:int = Math.random()*6-3;
	public var _vy:int = Math.random()*6-3;
	private var _num:uint;
	private var _list:Array;
	
	private var _stage:Stage;
	private var _flg:Boolean;
	private var _mp:Point = new Point(0,0);
	
	public function Ball(idin:uint,stg:Stage,ballNum:uint)
	{
		_stage = stg;
		_numBall = ballNum;
		
		_size = Math.random()*50+10;
		this.x = Math.random()*(_stage.stageWidth-_size*2)-_size;
		this.y = Math.random()*(_stage.stageHeight-_size*2)-_size;
		_num = idin;
		
		init(0xffffff);
		
		this.addEventListener(MouseEvent.MOUSE_DOWN,down);
	}
	
	private function init(color:uint):void
	{
		with(this.graphics)
		{
			clear();
			lineStyle(0.8,0xffffff);
			beginFill(color,0.6);
			drawCircle(0,0,_size);
			endFill();
		}
	}
	
	private function down(e:MouseEvent):void
	{
		init(0xff0000);
		this.startDrag();
		_flg = true;
		_vx = _vy = 0;
		
		addEventListener(Event.ENTER_FRAME,move);
		this.addEventListener(MouseEvent.MOUSE_UP,up);
		this.addEventListener(MouseEvent.MOUSE_OUT,out);
	}
	
	private function move(e:Event):void
	{
			_mp.x = this.x;
			_mp.y = this.y;
			_vx = (this.x - _mp.x);
			_vy = (this.y - _mp.y);
	}
	
	private function out(e:MouseEvent):void
	{
		up();
	}
	
	private function up(e:MouseEvent = null):void
	{
		init(0xffffff);
		_vx = (this.x - _mp.x);
		_vy = (this.y - _mp.y);
		this.removeEventListener(MouseEvent.MOUSE_OUT,out);
		this.removeEventListener(MouseEvent.MOUSE_UP,up);
		removeEventListener(Event.ENTER_FRAME,move);
		_mp.x = _mp.y = 0;
		this.stopDrag();
		_flg  = false;
	}
	
	public function setList(list:Array):void
	{
		_list = list;
	}
	
	public function collide():void
	{
		for(var i:uint = _num+1; i<_numBall; i++)
		{
			var dx:Number = _list[i].x - this.x;
			var dy:Number = _list[i].y - this.y;
			var dist:Number = Math.sqrt(dx*dx+dy*dy);
			var minDist:Number = _list[i]._size + _size;
			
			if(dist < minDist)
			{
				var angle:Number = Math.atan2(dy,dx);
				var targetX:Number = this.x + Math.cos(angle) * minDist;
				var targetY:Number = this.y + Math.sin(angle) * minDist;
				var ax:Number = (targetX - _list[i].x) * _spring*1.05;
				var ay:Number = (targetY - _list[i].y) * _spring;
				_vx -= ax;
				_vy -= ay;
				_list[i]._vx += ax;
				_list[i]._vy += ay;
			}
		}
	}
	
	public function Move():void
	{
		if(!_flg)
		{
			
		_vy += _gravity;
		
		this.x += _vx;
		this.y += _vy;
		
		if(this.x> _stage.stageWidth - _size)
		{
			this.x = _stage.stageWidth - _size;
			_vx *= _friction;
		}
		else if(this.x < _size)
		{
			this.x = _size;
			_vx *= _friction;
		}
		if(this.y >_stage.stageHeight - _size)
		{
			this.y = _stage.stageHeight - _size;
			_vy *= _friction;
		}
		else if(this.y < _size)
		{
			this.y = _size;
			_vy *= _friction;
		}
		
		}
	}
	
}