ActionScript Learning 110306

3.6.2011

ドラッグドロップでMCの位置を入れ替える。

配列のメソッドspliceを利用してでMC位置を入れ替えるアクションを制作する。

最初に表示するMCを配列に登録しておく。 ドラッグ中に随時座標をチェックしてマウスアップ後の新しい位置が何番目かを取得し、 位置が決定したら配列を「splice(新しい位置の番号, 0, ドラッグ中MC)」して順番を調整する。

SAMPLE VIEW CODE

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

	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 function initmain():void {
			
			_main = new main();
			
			var initargs:Object = {};
			initargs.stage = _stage;
			
			_main.Init(initargs);
			_main.Start();
			
			_this.addChild(_main);
		}
	}
}








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

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

		private var _this:MovieClip;
		private var _stage:Stage;

		
		
		//-------------------------------------------------------------------------------- Constractor
		
		public function main() {
			
			_this = this;
		}
		
		
		//-------------------------------------------------------------------------------- Function
		 
		/**
		 * initialize
		 */
		public function Init(initargs:Object):void {
			
			_stage = initargs.stage;
			
			initcards();
		}
		
		/**
		 * start
		 */
		public function Start():void {
			
			_this.x = Math.round((_stage.stageWidth - _this.width) / 2) + (stepx / 2);
			_this.y = Math.round((_stage.stageHeight - _this.height) / 2) + (stepy / 2);
		}

		/**
		 * initialize cards
		 */
		private var stepx:Number = 140;
		private var stepy:Number = 140;
		private var cardsarr:Array;
		
		private function initcards():void {
			
			stepx = 140;
			stepy = 140;
			cardsarr = [];
			
			for(var i:int = 0; i < 9; i ++) {
				
				var cardclass:Class = getDefinitionByName("no_" + (i + 1)) as Class;
				var card = new cardclass();
				
				card.name = "card_" + (i + 1);
				card.posid = i;
				card.moving = false
				card.x = (i % 3) * stepx;
				card.y = Math.floor(i / 3) * stepy;
				card.buttonMode = true;
				card.addEventListener(MouseEvent.MOUSE_DOWN, card_mousedown);
				
				cardsarr.push(card);
				
				_this.addChild(card);
			}
		}
		/** */
		private function card_mousedown(eo:* = null):void {
			
			var tgtcard:MovieClip = eo.currentTarget;
			
			_stage.addEventListener(MouseEvent.MOUSE_UP, card_mouseup);
			
			cardsaction_start(tgtcard);
		}
		/** */
		private function card_mouseup(eo:* = null):void {
			
			_stage.removeEventListener(MouseEvent.MOUSE_UP, card_mouseup);
			
			cardsaction_stop();
		}
		
		/**
		 * card action
		 */
		private var mdownx:Number;
		private var mdowny:Number;
		private var currentcard:MovieClip;
		//
		private function cardsaction_start(tgtcard:MovieClip):void {
			
			currentcard = tgtcard;
			
			for(var i:int = 0; i < cardsarr.length; i ++) {
				
				_this.setChildIndex(cardsarr[i], i);
			}
			
			_this.setChildIndex(currentcard, _this.numChildren - 1);
			
			mdownx = currentcard.mouseX;
			mdowny = currentcard.mouseY;
			
			trace("1:" + cardsarr);
			
			_this.addEventListener(Event.ENTER_FRAME, cardsaction_movieng);			
		}
		//
		private function cardsaction_stop():void {			
			
			_this.removeEventListener(Event.ENTER_FRAME, cardsaction_movieng);
			
			_this.addEventListener(Event.ENTER_FRAME, cardsaction_staying);
		}

		/** */
		private function cardsaction_movieng(eo:* = null):void {
			
			currentcard.x = _this.mouseX - mdownx;
			currentcard.y = _this.mouseY - mdowny;
			
			var countx:int = Math.floor((currentcard.x + stepx / 2) / stepx);
			if(countx > 2) countx = 2;
			else if(countx < 0) countx = 0;
			
			var county:int = Math.floor((currentcard.y + stepy / 2) / stepy);
			if(county > 2) county = 2;
			else if(county < 0) county = 0;
			
			var tgtposid:int = countx + (3 * county);
			
			cardsarr.splice(currentcard.posid, 1);
			
			currentcard.posid = tgtposid;
			
			cardsarr.splice(tgtposid, 0, currentcard);
			
			for(var i:int = 0; i < cardsarr.length; i ++) {
				
				if(i == tgtposid) continue;
				
				cardmove(i);
			}
		}
		/** */
		private function cardsaction_staying(eo:* = null):void {
			
			for(var i:int = 0; i < cardsarr.length; i ++) {
				
				cardmove(i);
			}
			
			var stopcount:int = 0;
			
			for(var j:int = 0; j < cardsarr.length; j ++) {
				
				if(!cardsarr[j].moving) stopcount ++;
				
				if(stopcount >= cardsarr.length) {
					
					_this.removeEventListener(Event.ENTER_FRAME, cardsaction_staying);
				
					trace("2:" + cardsarr);
				}
			}
		}
		//
		private function cardmove(i:int):void {
			
			var tgtcard:MovieClip = cardsarr[i];
			tgtcard.posid = i; 
			
			var posx:Number = tgtcard.posid % 3 * stepx;
			var posy:Number = Math.floor(tgtcard.posid / 3) * stepy;
			
			tgtcard.x += (posx - tgtcard.x) * .1;
			tgtcard.y += (posy - tgtcard.y) * .1;
			
			if(Math.abs(posx - tgtcard.x) < 1) tgtcard.x = posx;
			if(Math.abs(posy - tgtcard.y) < 1) tgtcard.y = posy;
			
			if(tgtcard.x == posx && tgtcard.y == posy) tgtcard.moving = false;
			else tgtcard.moving = true;
		}
	}
}




category : ActionScript / Flash

Demonstrations

Feature Samples

Author

虹村 マキオウ (nizimura makiou)

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

logo

Demo and Sample

Category