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

5つの丸がランダムな位置に移動する7

ステージ上をクリックすると、丸がランダムな位置に移動します。
Get Adobe Flash player
by umhr 15 May 2010
    Embed
/**
 * Copyright umhr ( http://wonderfl.net/user/umhr )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/7zL9
 */

/*
ステージ上をクリックすると、丸がランダムな位置に移動します。
*/
package{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	public class Main extends Sprite{
		private var _marus:Array = new Array();
		private var _onPositionXs:Array = new Array();
		private var _onPositionYs:Array = new Array();
		private var _n:int = 8;
		public function Main(){
			
			for (var i:int = 0; i < _n; i++) {
				_onPositionXs[i] = 0;
				_onPositionYs[i] = 0;
				
				var maru:Sprite = new Sprite();
				maru.graphics.beginFill(0xFF0000);
				maru.graphics.drawCircle(0,0,50);
				maru.graphics.endFill();
				this.addChild(maru);
				_marus[i] = maru;
			}

			this.addEventListener(Event.ENTER_FRAME,onEnter);
			stage.addEventListener(MouseEvent.CLICK,onClick);
		}
		private function onClick(event:MouseEvent):void{
			for (var i:int = 0; i < _n; i++) {
				_onPositionXs[i] = Math.random()*stage.stageWidth;
				_onPositionYs[i] = Math.random()*stage.stageHeight;
			}
		}

		private function onEnter(event:Event):void{
			for (var i:int = 0; i < _n; i++) {
				var dx:Number = _onPositionXs[i] - _marus[i].x;
				var dy:Number = _onPositionYs[i] - _marus[i].y;
				_marus[i].x += dx*0.1;
				_marus[i].y += dy*0.1;
			}
		}
	}
}