ActionScript Learning 110307

3.7.2011

ラジオボタンのクラス。

NM_Buttonbaseを使用してラジオボタンのクラスを制作する。

過去に制作したNM_Buttonbaseだが、そのままでは使い勝手が悪いので改良した。
仕事で大量のサムネイルをラジオボタン化することがよくあるのでまとめておく。

SAMPLE VIEW CODE

package {
	
	import flash.display.*;
	import flash.events.*	
	import flash.filters.*; 
	import flash.geom.*;
	
	import frocessing.color.*;
	

	public class NMain extends MovieClip {
		
		//-------------------------------------------------------------------------------- Properties

		private var _this:*;
		private var _stage:Stage;
		
		
		//-------------------------------------------------------------------------------- Constractor
		
		public function NMain() {
			
			_this = this;
			_stage = stage;
			
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			_stage.align = StageAlign.TOP_LEFT;
			
			Init();
		}
		
		
		//-------------------------------------------------------------------------------- Function
		 
		/**
		 * initialize
		 */
		public function Init():void {

			initmain();
		}
		
		/**
		 * initialize main
		 */
		private var _main:MovieClip;
		private var radbtn:NM_Radiobutton;		
		
		private function initmain():void {
			
			_main = new MovieClip();
			
			var colorhsl = new ColorHSL();
			colorhsl.h = 200;
			colorhsl.s = 1;
			
			radbtn = new NM_Radiobutton();
			radbtn.Init();
			
			for(var i:int = 0; i < 7; i++) {
			
				var btn = new buttonbase();
				btn["n"].mouseEnabled = false;
				btn["n"].text = i + 1;
				btn.x = 100 * i;
				btn.y = 0;
				
				colorhsl.h += i * 8;
				
				btn.Color_di = 0xCCCCCC;
				
				colorhsl.l = .7;
				btn.Color_en = colorhsl.value;
				
				colorhsl.l = .5;
				btn.Color_ov = colorhsl.value;
				
				colorhsl.l = .98;
				btn.Color_ac = colorhsl.value;				

				
				var color_di:ColorTransform = btn.di.transform.colorTransform; 
				color_di.color = btn.Color_di;
				btn.di.transform.colorTransform = color_di; 
				
				var color_en:ColorTransform = btn.en.transform.colorTransform; 
				color_en.color = btn.Color_en;
				btn.en.transform.colorTransform = color_en; 
				
				var color_ov:ColorTransform = btn.ov.transform.colorTransform; 
				color_ov.color = btn.Color_ov;
				btn.ov.transform.colorTransform = color_ov;
				
				var color_ac:ColorTransform = btn.ac.transform.colorTransform; 
				color_ac.color = btn.Color_ac;
				btn.ac.transform.colorTransform = color_ac; 
				

				var initargs:Object = {};
				initargs.stage = _stage;
				initargs.radiobutton = radbtn;
				btn.Init(initargs);
				btn.SetEnable();
	
				_main.addChild(btn);
				
				radbtn.addNMButton(btn);
			}
			
			radbtn.Start(radbtn.buttonList[Math.floor(radbtn.buttonList.length * Math.random())]);
			
			_this.addChild(_main);
			_main.x = Math.round((_stage.stageWidth - _main.width) / 2) + 40;
			_main.y = Math.round((_stage.stageHeight - _main.height) / 2) + 40;
		}




	}
}



package {
	
	import flash.display.*;
	

	public class buttonbase extends NM_Buttonbase {
		
		//-------------------------------------------------------------------------------- Properties
		
		private var _this:MovieClip;
		private var _stage:Stage;
		
		private var _ac:MovieClip;
		private var _ov:MovieClip;
		private var _en:MovieClip;
		private var _di:MovieClip;
		private var _n:TextField;
		
		public var Color_en:Number;
		public var Color_di:Number;
		public var Color_ov:Number;
		public var Color_ac:Number;
		
		private var radiobutton:NM_Radiobutton;
		
		
		//-------------------------------------------------------------------------------- Constractor
		
		public function buttonbase() {
			
			_this = this;
		}
		
		
		//-------------------------------------------------------------------------------- Function
		 
		/**
		 * Init
		 */
		override public function Init(initargs:Object):void {
			
			super.Init(initargs);
			
			_stage = initargs.stage;
			
			_ac = _this.ac;			
			_ov = _this.ov;
			_en = _this.en;
			_di = _this.di;
			_n = _this.n;
			
			radiobutton = initargs.radiobutton;
		}	
		/** */
		override public function Over():void {			
			
			super.Over();
			
			_ov.alpha = 1;
		}
		/** */
		override public function Out():void {
			
			super.Out();
			
			_ov.alpha = 0;
		}
		/** */
		override public function Down():void {
			
			super.Down();
		}
		/** */
		override public function Release():void {
			
			super.Release();
			
			SetActive();
			
			radiobutton.setCurrent(_this);
		}
		/** */
		override public function ReleaseOutside():void {
			
			super.ReleaseOutside();
		}
		
		/**
		 * Enable
		 */
		override public function SetEnable():void {
			
			super.SetEnable();
			
			_n.textColor = 0xFFFFFF;
			
			_ac.alpha = 0;
			_ov.alpha = 0;
			_en.alpha = 1;
		}
		
		/**
		 * Disable
		 */
		override public function SetDisable():void {
			
			super.SetDisable();
			
			_n.textColor = 0xFFFFFF;
			
			_ac.alpha = 0;
			_ov.alpha = 0;
			_en.alpha = 0;
		}
		
		/**
		 * Active
		 */
		override public function SetActive():void {
			
			super.SetActive();
			
			_n.textColor = Color_ov;
			
			_ac.alpha = 1;
			_ov.alpha = 0;
			_en.alpha = 0;
		}
		
		

	}
}



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

	public class NM_Buttonbase extends MovieClip {
		
		//-------------------------------------------------------------------------------- Properties
		
		private var _this:MovieClip;
		private var _stage:Stage;
		
		private var buttonstatus:String;
		
		
		//-------------------------------------------------------------------------------- Constractor
		
		public function NM_Buttonbase() {
			
			_this = this;
		}
		
		
		//-------------------------------------------------------------------------------- Function
		 
		/**
		 * Init
		 */
		public function Init(initargs:Object):void {
			
			_stage = initargs.stage;

			_this.addEventListener(MouseEvent.MOUSE_OVER, button_over);
			_this.addEventListener(MouseEvent.MOUSE_OUT, button_out);
			_this.addEventListener(MouseEvent.MOUSE_DOWN, button_down);
		}	
		/** */
		private function button_over(eo:* = null):void {
			
			if(buttonstatus != "enable") return;
			
			Over();
		}
		//
		public function Over():void {
			
			trace("Over");
		}
		/** */
		private function button_out(eo:* = null):void {
			
			if(buttonstatus != "enable") return;
			
			Out();
		}
		//
		public function Out():void {
			
			trace("Out");
		}
		/** */
		private function button_down(eo:* = null):void {
			
			if(buttonstatus != "enable") return;
			
			_stage.addEventListener(MouseEvent.MOUSE_UP, button_up);
			
			Down();
		}
		//
		public function Down():void {
			
			trace("Down");
		}
		/** */
		private function button_up(eo:* = null):void {
			
			if(buttonstatus != "enable") return;
			
			if(_this.hitTestPoint(_stage.mouseX, _stage.mouseY, true)) Release();
			else ReleaseOutside();
			
			_stage.removeEventListener(MouseEvent.MOUSE_UP, button_up);
		}
		//
		public function Release():void {
			
			trace("Release");
		}
		//
		public function ReleaseOutside():void {
			
			trace("ReleaseOutside");
		}
		
		/**
		 * Enable
		 */
		public function SetEnable():void {
			
			if(buttonstatus == "enable") return;
			
			buttonstatus = "enable";
			
			_this.buttonMode = true;
			_this.mouseEnabled = true;
			_this.mouseChildren = true;
		}
		
		/**
		 * Disable
		 */
		public function SetDisable():void {
			
			if(buttonstatus == "disable") return;
			
			buttonstatus = "disable";
			
			_this.buttonMode = false;
			_this.mouseEnabled = false;
			_this.mouseChildren = false;
		}
		
		/**
		 * Active
		 */
		public function SetActive():void {
			
			if(buttonstatus == "active") return;
			
			buttonstatus = "active";

			_this.buttonMode = true;
			_this.mouseEnabled = true;
			_this.mouseChildren = true;
		}
		
		

	}
}




package {
	
	import flash.display.*;
	

	public class NM_Radiobutton {
		
		//-------------------------------------------------------------------------------- Properties
		
		private var _this:*;
		
		public var currentButton:MovieClip;
		public var lastButton:MovieClip;
		public var buttonList:Array;

		
		
		//-------------------------------------------------------------------------------- Constractor
		
		public function NM_Radiobutton() {
			
			_this = this;
		}
		
		/** */
		public function get buttonLength():int {return buttonList.length;}
		
		
		//-------------------------------------------------------------------------------- Function
		 
		/**
		 * initialize
		 */
		public function Init():void {
			
			buttonList = [];
			
			currentButton = null;
			lastButton = null;
		}
		
		/**
		 * start
		 */
		public function Start(startbtn:MovieClip = null):void {
			
			currentButton = startbtn;
			
			startbtn.SetActive();
		}
		
		/**
		 * reset
		 */
		public function Reset():void {
			
			currentButton = null;
			lastButton = null;
			
			for(var i = 0; i < buttonList.length; i ++) {
				
				buttonList[i].SetEnable();
			}
		}
		
		/**
		 * add nmbutton
		 */
		public function addNMButton(tgtbtn:MovieClip):void {
			
			buttonList.push(tgtbtn);
		}
		
		/**
		 * set currentbutton
		 */
		public function setCurrent(tgtbtn:MovieClip):void {
			
			if(tgtbtn == currentButton) return; 
			
			lastButton = currentButton;
			currentButton = tgtbtn;
			
			currentButton.SetActive();
			if(lastButton) lastButton.SetEnable();
		}
		

	}
}




category : ActionScript / Flash

Demonstrations

Feature Samples

Author

虹村 マキオウ (nizimura makiou)

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

logo

Demo and Sample

Category