0
votes

I have loaded movieClip to stage and was performing some events on that movieClip. Movie clip has own public functions and variables, and those are NOT accessible through currentTarget object in events.

Here is sample class:

package {
    import flash.display.MovieClip;
    import flash.display.Shape;
    public class SampleClass extends MovieClip {
        var str:String;
        public function SampleClass() {
            str="Some string";
            /* draw just a sample rectangle to click on it */
            var rectangle:Shape=new Shape  ;
            rectangle.graphics.beginFill(0x000000);
            rectangle.graphics.drawRect(0,0,100,100);
            rectangle.graphics.endFill();
        }
        public function getStr():String {
            return str;
        }
    }
}

And here is loading on the stage and creating event:

package {
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    public class MainClass extends MovieClip {
        var a:SampleClass;
        public function MainClass() {
            a=new SampleClass();
            addChild(a);
            a.addEventListener(MouseEvent.CLICK,clickEvent);
        }
        function clickEvent(evt:MouseEvent):void {
            var Obj=evt.currentTarget;
            trace (Obj.getStr());
        }
    }
}

Tracing will return null instead of string value cause currentTarget is an Object, not a Class (movieClip). Any idea how to solve this?

2
Seems like you need to cast it first: var Obj=SampleClass(evt.currentTarget);Ivan Chernykh

2 Answers

0
votes

//use this code it will work

 function clickEvent(evt:MouseEvent):void {
        var Obj:SampleClass = evt.currentTarget as SampleClass;
        trace (Obj.getStr());
 }
0
votes

I dont know if your problem is solved now but the code you posted in your question worked okay for me..

What I did to test it..

  1. In a new blank document, open Library (ctrl+L) and right-clicked to make symbol (MovieClip)
  2. In linkage section, tick Export for Actionscript and call it SampleClass
  3. Right click symbol item now added in Library list and choose Edit Class option
  4. In that SampleClass I replace all with a paste of your code BUT NOTE: after that line rectangle.graphics.endFill();.. I also added the line addChild(rectangle);
  5. Now when I test (Debug: Ctrl+Shift+Enter).. I see a black square that traces "Some string" everytime I click it..

Your MainClass.as was attached to the FLA as the Document Class (see Properties with Ctrl+F3)

I hope this is useful to you or anyone else trying this kind of code. Any issues just add a comment. Thanks.