【问题标题】:how can i make button delete one of the boxes?我怎样才能让按钮删除其中一个框?
【发布时间】:2012-03-23 18:37:10
【问题描述】:

这段代码在几分钟内就可以正常工作,我突然收到错误 #2032。我刚开始学习 AC3,显然做错了什么。有人可以帮我解释一下吗?

<fx:Script>

    <![CDATA[
        import mx.containers.Canvas;
        import mx.containers.Panel;
        import mx.events.FlexEvent;
        import mx.events.FlexMouseEvent;

        import spark.components.Button;


        public function createBoxes():void 
        {

            //create a Panel 
            var colorsPanel:Panel = new Panel(); colorsPanel.layout = "absolute"; colorsPanel.width = 250; colorsPanel.height = 250;
            //add the Panel to the Application
            addElement(colorsPanel);
            //create a red box 
            var redBox:Canvas = new Canvas(); redBox.x = 70; redBox.y = 70; redBox.width = 50; redBox.height = 50; redBox.setStyle("backgroundColor", 0xFF0000);
            //create a green box 
            var greenBox:Canvas = new Canvas(); greenBox.x = 90; greenBox.y = 90; greenBox.width = 50; greenBox.height = 50; greenBox.setStyle("backgroundColor", 0x00FF00);
            //create a blue box 
            var blueBox:Canvas = new Canvas(); blueBox.x = 100; blueBox.y = 60; blueBox.width = 50; blueBox.height = 50; blueBox.setStyle("backgroundColor", 0x0000FF);
            //add the boxes to the Panel
            var Button:spark.components.Button = new spark.components.Button(); Button.x = 80; Button.y =160; Button.label ="removeG";
            colorsPanel.addElement(redBox); 
            colorsPanel.addElement(greenBox); 
            colorsPanel.addElement(blueBox);
            colorsPanel.addElement(Button); 
            Button.addEventListener(MouseEvent.CLICK,removeBox);

        }

    public function removeBox(evt:MouseEvent):void
    {

    }

    ]]>
</fx:Script>
<s:Button id="But1" x="45" y="40" label="Click" click="createBoxes()"/>

【问题讨论】:

  • 将您的按钮“按钮”重命名为可读的名称。

标签: actionscript-3 actionscript flex4


【解决方案1】:

是的,就像 Sam 说的,您需要使用按钮实例来添加事件侦听器。

        <![CDATA[
            import mx.containers.Canvas;
            import mx.containers.Panel;
            import mx.events.FlexEvent;
            import mx.events.FlexMouseEvent;

            import spark.components.Button;

            private var buttonToPanelMap:Object={};

    public function createBoxes():void 
    {

        //create a Panel 
        var colorsPanel:Panel = new Panel();
        colorsPanel.layout = "absolute";
        colorsPanel.width = 250;
        colorsPanel.height = 250;
        //add the Panel to the Application
        addElement(colorsPanel);

        //create a red box 
        var redBox:Canvas = new Canvas();
        redBox.x = 70;
        redBox.y = 70;
        redBox.width = 50;
        redBox.height = 50;
        redBox.setStyle("backgroundColor", 0xFF0000);

        //create a green box 
        var greenBox:Canvas = new Canvas();
        greenBox.x = 90;
        greenBox.y = 90;
        greenBox.width = 50;
        greenBox.height = 50;
        greenBox.setStyle("backgroundColor", 0x00FF00);

        //create a blue box 
        var blueBox:Canvas = new Canvas();
        blueBox.x = 100;
        blueBox.y = 60;
        blueBox.width = 50;
        blueBox.height = 50;
        blueBox.setStyle("backgroundColor", 0x0000FF);

        //add the boxes to the Panel
        colorsPanel.addElement(redBox); 
        colorsPanel.addElement(greenBox); 
        colorsPanel.addElement(blueBox);

        var boxRemoverBtn:Button = new Button();
        boxRemoverBtn.x = 80;
        boxRemoverBtn.y =160;
        boxRemoverBtn.label ="removeG";
        colorsPanel.addElement(boxRemoverBtn);
        boxRemoverBtn.addEventListener(MouseEvent.CLICK,removeBox);
        buttonToPanelMap[boxRemoverBtn] = colorsPanel;

    }

    public function removeBox(evt:MouseEvent):void
    {
        removeElement(buttonToPanelMap[evt.target] as Panel);
    }

        ]]>
    </fx:Script>
    <s:Button id="But1" x="45" y="40" label="Click" click="createBoxes()"/>

【讨论】:

  • 我对这条线有点困惑 removeElement(buttonToPanelMap[event.target] as Panel);你能解释一下吗?谢谢
  • 是的,这不一定是实现目标的最佳方法,但我认为它应该有效并且非常简单。基本上,AS3 中的 Object 被标记为动态类,这意味着可以动态附加属性,所以我所做的基本上是告诉它使用 Button 本身作为属性来创建该对象的属性称为关联数组。如果出现问题,请告诉我,我会进行更多调试以更正我的答案。
  • 嗨。我收到错误 1120:访问我之前提到的行的未定义属性事件
  • 确保在 createBoxes() buttonToPanelMap[boxRemoverBtn] = colorsPanel 的底部有这一行我刚刚在带有 Flex 4.6 SDK 的 AIR 环境中对此进行了测试,因此我不得不调整一些代码,但只要该对象填充了面板,它就可以正常工作。
  • 还要注意 evt.currentTarget 可能是一种更准确的方法,以防以某种方式将目标设置为按钮的子组件,因为按钮本身被用作对象的引用/地图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
  • 2014-04-26
  • 2014-01-03
  • 1970-01-01
相关资源
最近更新 更多