
ActionScript3.0でオブジェクトの子にあるボタンの有効/無効を切り替える「SmartButton」クラスを作成しました。
静的クラスメンバ“enabled”は、指定したオブジェクトの [mouseChildren]と [mouseEnabled]の値を個別に設定します。操作したいオブジェクトと、ボタンの有効/無効[Boolean]を指定して使用します。
静的クラスメンバ“change”は、予め配列化したメニューと、ボタンを無効にしたい配列メニューのインデックス値を指定して使用します。指定したインデックスの子にあるボタンが無効となり、それ以外のインデックスの子にあるボタンが有効になります。
ボタンの有効/無効を切り替えるというより、厳密には指定したオブジェクトの [mouseChildren]と [mouseEnabled]の値を true/falseに切り替えます。
ファイル
使用方法
- _sp[Sprite]の子にあるボタンを無効化する
1
| SmartButton.enabled(_sp, false);
|
- _sp[Sprite]の子にあるボタンを有効化する
1
| SmartButton.enabled(_sp, true);
|
- sampleMenu[Array]の1番目のインデックスの子にあるボタンを無効化し、それ以外のインデックスの子にあるボタンを有効化する
1
2
3
4
5
6
7
| var sampleMenu:Array = [sampleMenu1, sampleMenu2, sampleMenu3]
SmartButton.change(
sampleMenu, //配列メニュー
1, //ボタンを無効化したい配列要素のインデックス値
sampleMenu.length, //配列要素の総数
0 //配列要素の先頭
);
|
- sampleMenu[Array]の子にある全てのボタンを有効化する
1
2
3
4
5
6
| var sampleMenu:Array = [sampleMenu1, sampleMenu2, sampleMenu3]
SmartButton.clear(
sampleMenu, //配列メニュー
sampleMenu.length, //配列要素の総数
0 //配列要素の先頭
);
|
ソースコード
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| package net.dolice.utils{
import flash.display.Sprite;
public class SmartButton {
private static var buttons:Array;
private static var id:int;
private static var length:uint;
private static var index:uint;
public static function change(buttons:Array,currentButtonID:int,buttonNum:uint=0,indexButtonID:uint=1):void {
SmartButton.initialize(buttons,currentButtonID,buttonNum,indexButtonID);
SmartButton.exec();
}
public static function clear(buttons:Array,buttonNum:uint=0,indexButtonID:uint=1):void {
SmartButton.initialize(buttons,-1,buttonNum,indexButtonID);
SmartButton.exec();
}
public static function enabled($scope:Sprite,b:Boolean):void {
$scope.mouseChildren = b;
$scope.mouseEnabled = b;
}
private static function initialize(buttons:Array,id:int,buttonNum:uint,indexButtonID:uint):void {
SmartButton.buttons = buttons;
SmartButton.id = id;
buttonNum <= 0 ? SmartButton.length = SmartButton.buttons.length:SmartButton.length = buttonNum;
SmartButton.index = indexButtonID;
}
private static function exec():void {
for (var i:uint = SmartButton.index; i <= SmartButton.length; i++) {
SmartButton.enabled(SmartButton.buttons[i],i != SmartButton.id);
}
}
}
}
|