ActionScript Learning 101120-3

11.20.2010

指定した数の重複しないランダムな数字を配列で返す関数。

クイズの出題や、画像を表示するときのシャッフルなどで使用する重複しない乱数を配列で返す関数。

SAMPLE VIEW CODE

package {
	
	import flash.events.*;
	import flash.display.*;
	import flash.text.*;
	import flash.net.*;

	public class NMain extends MovieClip {
		
		//-------------------------------------------------------------------------------- Properties
		
		private var _this:*;
		private var _stage:Stage;
		
		private var _main:MovieClip;
		
		
		
		//-------------------------------------------------------------------------------- Constractor
		
		public function NMain() {
			
			_this = this;
			
			_this.addEventListener(Event.ADDED_TO_STAGE, Init);
		}
		
		
		//-------------------------------------------------------------------------------- Function
		 
		/**
		 * initialize
		 */
		public function Init(eo:* = null):void {
			
			_this.removeEventListener(Event.ADDED_TO_STAGE, Init);
			
			_stage = stage;			
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			_stage.align = StageAlign.TOP_LEFT;		
			
			_main = new MovieClip();
			_this.addChild(_main);
			
			_stage.addEventListener(Event.RESIZE, stageresizeaction);
			stageresizeaction();
			
			_this.buttonMode = true;
			
			_this.addEventListener(MouseEvent.CLICK, maketext);
			
			maketext();
		}
		
		/**
		 * made text
		 */
		private var _texts:MovieClip;
		
		private function maketext(eo:* = null):void {
			
			if(_texts) _main.removeChild(_texts);
			
			_texts = new MovieClip();
			
			var _t = new t();
			var _tf = _t.tf;

			_tf.text = "";
			
			var randnumarr = getrandomnumbers(9);
			
			for(var i = 0; i < randnumarr.length; i ++) {
				
				_tf.text += randnumarr[i];
			}
			
			_t.width = _stage.stageWidth;
			_t.scaleY = _t.scaleX;
			
			_texts.addChild(_t);
			_main.addChild(_texts);
			
			var ref = new NM_Reflect({tgtmc:_texts, maskalpha:.2, ratio:255, dropoff:1});

			ref.y = _texts.y + 100;
			
			_texts.addChild(ref);	
			
			_texts.x = -Math.round(_texts.width / 2);
			_texts.y = -Math.round(_texts.height / 2);			
		}
		
		
		/**
		 * get randomnumbers
		 */
		private function getrandomnumbers(numleng:int):Array {
		
			var arr = [];
			
			for (var i:int = 0; i < numleng; i ++) {
				
				arr[i] = i + 1;
			}
			
			var randnum:int;
			var lastnum:int;
			
			while (numleng --) {
			
				randnum = Math.floor(Math.random() * numleng);
				lastnum = arr[numleng];
				
				arr[numleng] = arr[randnum];		
				arr[randnum] = lastnum;
			}
			
			return arr;
		}
		
		
		/**
		 * stage resize
		 */
		private function stageresizeaction(eo:* = null):void {
			
			_main.x = Math.round(_stage.stageWidth / 2);
			_main.y = Math.round(_stage.stageHeight / 2);
		}
	}
}


category : ActionScript / Flash

Demonstrations

Feature Samples

Author

虹村 マキオウ (nizimura makiou)

猫と太極拳を愛する横浜在住のフリーランスクリエイターです。

logo

Demo and Sample

Category