イナヅマTVログ

ActionScript 3, Event処理を簡単に – Signals, カスタムイベント その1

| 2件のコメント

Signalsの4回目。
Custom Event(カスタムイベント)はどう使う、1回目。

Custom Event(カスタムイベント)の時は org.osflash.signals.Signal を使うみたい。
どうも使い方がまだ良くわからない。

Radio ボタンで選ばれた方向へステージのボールが3px動く。

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
 
import com.bit101.components.RadioButton;
import com.bit101.components.PushButton;
 
import org.osflash.signals.Signal;
 
// minimalcomp radio buttons
function setupRadioButton ():void {
	var radioContainer:Sprite = new Sprite();
	addChild(radioContainer);
 
	var left:RadioButton = new RadioButton(radioContainer, 0, 30, "Left", true, onRadioSelect);
	var right:RadioButton = new RadioButton(radioContainer, 120, 30, "Right", false, onRadioSelect);
	var up:RadioButton = new RadioButton(radioContainer, 60, 0, "Up", false, onRadioSelect);
	var down:RadioButton = new RadioButton(radioContainer, 60, 60, "Down", false, onRadioSelect);
 
	left.groupName = "direction";
	right.groupName = "direction";
	up.groupName = "direction";
	down.groupName = "direction";
 
	var button:PushButton = new PushButton(radioContainer, 42, 25, "MOVE", onButtonClick);
	button.width = 60;
}
// event variables
var event:String = 'left';
var events:Object = {};
 
// radio select callback
function onRadioSelect (e:MouseEvent):void {
	event = e.target.label.toLowerCase();
}
 
// button callback
function onButtonClick (e:MouseEvent):void {
	if (event && events[event]) events[event].dispatch(new Event(event));
}
// ------------------------------------------
// Custom Events
function setupEvents ():void {
	events.left = new Signal();
	events.right = new Signal();
	events.up = new Signal();
	events.down = new Signal();
}
// ------------------------------------------
// Ball
var ball:Sprite;
function setupBall ():void {
	ball = new Sprite();
	ball.graphics.clear();
	ball.graphics.beginFill(0xff6633);
	ball.graphics.drawCircle(-10, -10, 20);
	ball.graphics.endFill();
 
	ball.x = stage.stageWidth * .5;
	ball.y = stage.stageHeight * .5;
 
	addChild(ball);
 
	for (var signalName:String in events){
		events[signalName].add(onMove);
	}
}
 
function onMove (e:Event):void {
	switch (e.type) {
		case 'up' :
			ball.y -= 3;
			break;
		case 'down' :
			ball.y += 3;
			break;
		case 'left' :
			ball.x -= 3;
			break;
		case 'right' :
			ball.x += 3;
			break;
	}
}
 
// ------------------------------------------
// MAIN
function main ():void {
	// background
	var container:Sprite = new Sprite();
	container.graphics.clear();
	container.graphics.beginFill(0xefefef);
	container.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
	container.graphics.endFill();
 
	addChild(container);
 
	setupRadioButton();
	setupEvents();
	setupBall();
}
 
main();

Signal と dispatch の時の引数はどうする?

そもそも今回は、mimalcompsのコールバックから直接操作する方がコードが少なくて済む。
わざわざ Signals を使うメリットはどこにあるのか?

悩みはつきない。

2件のコメント

  1. ピンバック: Tweets that mention Actionscript 3, Event処理を簡単に – Signals, カスタムイベント その1 | イナヅマtvログ -- Topsy.com

  2. ピンバック: Actionscript 3, Event処理を簡単に – Signals, カスタムイベント その2 | イナヅマtvログ

コメントを残す

必須欄は * がついています


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください