开发者

Noob AS3 question regarding using event handlers to remove MovieClip object from stage

I'm an AS3 noob just trying to get more comfortable working with event handlers in Flash and build interactivity into my application.

In the code below, I have created an instance of the DrawLineChart class called LineChart1. When I test the movie, it shows up on the stage just fine and when I click on it, I can use a trace command to get a string statement written to the output window.

开发者_开发问答

However, now I want to be able to click on LineChart1 on the stage and have it be removed. When I do that, I get an error message 1120: Access of undefined property LineChart1.

Could someone please explain to me why I'm unable to refer to my instance LineChart1 and what I need to do so that I can refer to it and remove it when it gets clicked? Also, I'd love to know why the trace statement works when I click on LineChart1 during runtime, but not the removechild command.

I'm sorry if this question is too simple, but thank you all for your help in advance. I really appreciate it.

package{
import flash.display.*;
import flash.events.*;

public class Main extends MovieClip{ 

var recWidth:Number = 250;
var recHeight:Number = 550;
var recX:Number = 50;
var recY:Number = 50;
var recScaleY:Number = 30;

public function Main(){

var LineChart1 = new DrawLineChart(recX, recY, recWidth, recHeight, recScaleY);
LineChart1.addEventListener(MouseEvent.CLICK, onClickHandler);
addChild(LineChart1);
}

function onClickHandler(e:Event):void{
trace("hello"); // This works.  When I click on the LineChart1 MovieClip on the stage during runtime, I get "hello" as an output.
removeChild(LineChart1); // throws an error 1120: Access of undefined property LineChart1.  Why?  
}
}
}


Your variable is scoped locally to Main, you need to declare it as an instance variable (class level), to properly define its scope.

private var _lineChart1:DrawLineChart; 

//main function
_lineChart1 = new DrawLineChart(...

//handler function
this.removeChild(_lineChart1);

For more information about scope in AS3 = check out the livedocs.

Cheers


Your problem is that you have defined LineChart1 as a local variable. This means that because you declare it inside a function, it is only visible within that function.

Make LineChart1 a property of your class, then you will be able to see it from your event handler. Alternatively, use e.target as DrawLineChart.


All answer's is good but if u have more then one on the stage what can u do ? You can use an Array to take a list of your mc's and then u can use that Array to remove mc's on the stage. Here is a Simple Example:

  package
 {

    import flash.display.*;

    import flash.events.*;

    public class Main extends MovieClip{ 

    private var recWidth:Number = 250;
    private var recHeight:Number = 550;
    private var recX:Number = 50;
    private var recY:Number = 50;
    private var recScaleY:Number = 30;
    private var lineArray:Array = new Array();

    public function Main()
    {
        for(var i:int = 0;i<10;i++)
        {
            var LineChart1 = new DrawLineChart(recX, recY, recWidth, recHeight, recScaleY);
            LineChart1.addEventListener(MouseEvent.CLICK, onClickHandler);
            LineChart1.name = line+i.toString(); // u can use whatever u want for name's
            lineArray.push(lineChart1);
            addChild(LineChart1);
        }
        //if u want to place this 10 LineChart1 u can set x and y values like recX += recX and ect.
    }
    private   function onClickHandler(e:Event):void
    {
        //when u click one of your LineChart1 and want to remove it from stage u can use this
        trace(e.currentTarget.name); // if u want to see what is the name of ur mc
        var myId:String = e.currentTarget.name.substring(4,10);
        removeChild(getChildByName("line"+myId));
    }
}

hope it works for u

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜