2

我想实现一种非常简单的方法来存储包含我单击的最后一个特定“CustomObject”的变量。我想点击其他对象被忽略。以下面的示例代码为例,假设 CustomObject 扩展了 MovieClip:

//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)将我单击的最后一个方块存储到文档类中的一个变量中 2)当我单击另一个方块时,从所有其他方块中移除发光

非常感谢任何反馈!

4

1 回答 1

1

我建议创建一个作为 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();
    }
}

然后,您可以将文档类中的代码修改为:

var collection:CustomCollection = new CustomCollection(3, this);
collection.select(1);

您需要为处理选择按钮的单击事件添加自己的逻辑。我建议index为每个 CustomObject 添加一个属性以及对它添加到的集合的引用。这样,您可以简单地将 click 事件添加到 CustomObject 类中,并使用如下处理函数:

private function _click(e:MouseEvent):void
{
    _collection.select(index);
}
于 2012-06-26T01:54:13.523 回答