Actionscript 3 使对象的行为类似于AS3中的单选按钮
我想实现一种非常简单的方法来存储一个包含我上次单击的特定“CustomObject”的变量。我希望忽略对其他对象的单击。以以下示例代码为例,给定CustomObject扩展MovieClip:Actionscript 3 使对象的行为类似于AS3中的单选按钮,actionscript-3,flash,flash-cs5.5,Actionscript 3,Flash,Flash Cs5.5,我想实现一种非常简单的方法来存储一个包含我上次单击的特定“CustomObject”的变量。我希望忽略对其他对象的单击。以以下示例代码为例,给定CustomObject扩展MovieClip: //Code within the Document Class: var square1:CustomObject = new CustomObject(); var square2:CustomObject = new CustomObject(); var square3:CustomObject
//Code within the Document Class:
var square1:CustomObject = new CustomObject();
var square2:CustomObject = new CustomObject();
var square3:CustomObject = new CustomObject();
var triangle1:DifferentObject= new DifferentObject();
square1.x=100; square2.x=200; square3.x=300;
addChild(square1);
addChild(square2);
addChild(square3);
addChild(triangle1);
//Code within the CustomObject Class:
this.addEventListener(MouseEvent.CLICK,radioButtonGlow);
public function radioButtonGlow(e:MouseEvent):void
{
var myGlow:GlowFilter = new GlowFilter();
myGlow.color = 0xFF0000;
myGlow.blurX = 25;
myGlow.blurY = 25;
this.filters = [myGlow];
}
这对我每次点击方块都非常有效——它们会像预期的那样亮起来。但是,我想实现一个功能:
1) 将我单击的最后一个方块存储到document类中的变量中
2) 当我单击其他方块时,移除所有其他方块的光晕
非常感谢您的反馈 我建议创建一个类,作为CustomObject实例的集合,并以这种方式管理它们(即确保只能选择该集合中的一个,等等) 样本:
public class CustomCollection
{
// Properties.
private var _selected:CustomObject;
private var _items:Array = [];
// Filters.
private const GLOW:GlowFilter = new GlowFilter(0xFF0000, 25, 25);
// Constructor.
// @param amt The amount of CustomObjects that should belong to this collection.
// @param container The container to add the CustomObjects to.
public function CustomCollection(amt:int, container:Sprite)
{
for(var i:int = 0; i < amt; i++)
{
var rb:CustomObject = new CustomObject();
rb.x = i * 100;
_items.push(rb);
container.addChild(rb);
}
}
// Selects a CustomObject at the specified index.
// @param index The index of the CustomObject to select.
public function select(index:int):void
{
for(var i:int = 0; i < _items.length; i++)
{
if(i == index)
{
_selected = _items[i];
_selected.filters = [GLOW];
continue;
}
_items[i].filters = [];
}
}
// The currently selected CustomObject.
public function get selected():CustomObject
{
return _selected;
}
// A copy of the array of CustomObjects associated with this collection.
public function get items():Array
{
return _items.slice();
}
}
您需要为处理选择按钮的单击事件添加自己的逻辑。我建议为每个CustomObject添加一个索引
属性,以及对它添加到的集合的引用。这样,您只需将click事件添加到CustomObject类中,并使处理程序函数类似于:
var collection:CustomCollection = new CustomCollection(3, this);
collection.select(1);
private function _click(e:MouseEvent):void
{
_collection.select(index);
}
先生,您是位绅士和学者。创建一个类来管理自定义对象是一个天才的想法。非常感谢!