How to access the variable “abc” in the following code structure


1) First of all I don't wanna use CustomEvent class. Some solution I am looking without using CustomEvent.

2) One of the solution can be having abc variable in ClassA. And then dispatching directly via ClassA ( rathar than saying classB.dispatchEvent() ). But still looking if there is some better solution than this.


//Frame1 code :
import flash.events.Event;

var classA:ClassA = new ClassA() ;

classA.addEventListener("hello", hello); 
classA.init();
function hello(e:Event)
{
    trace(e.currentTarget.abc); //<<<< NEVER EXECUTED
}

//classA
package
{
    import flash.display.MovieClip;
    import flash.events.Event;

    public class ClassA extends MovieClip 
    {

        var classB:ClassB ; 

        public function ClassA()
        {
            classB = new ClassB(); 

        }

        public function init()
        {
            classB.dispatchEvent( new Event("hello"));
        }

    }
}

//classB
package
{
    import flash.display.MovieClip;
    public class ClassB extends MovieClip
    {
        public var abc:Number =123;
        public function ClassB()
        {


        }




    }
}

Answers


You are missing a couple key concepts before you can get your example to work. First you are dispatching the event on an instance of ClassB, however you are listening on an instance of ClassA. So, they have to be related in some way, in order for event to be properly orchestrated when it gets dispatched. One way to do that is to use event bubbling. One caveat to that is that native event bubbling only really works for DisplayObjects, but both of your classes inherit from MovieClip so thats not a big deal.

So the first thing, you have to understand how bubbling events work. A simplified explanation is that events start at the top of the display hierarchy and capture down the display tree towards the element, they are finally dispatched on the target, then they turn around and bubble back out in the opposite direction.

This means that your instance of ClassB has to be a child of ClassA. So the first thing you'll have to change is in your ClassA constructor:

    public function ClassA()
    {
        classB = new ClassB();
        addChild(classB);
    }

Next, when you dispatch the event, you'll need to explictly say that its a bubbling event, otherwise it'll be triggered on the target, and neither capture nor bubble through the display stack.

    public function init()
    {
        classB.dispatchEvent( new Event("hello", true));
    }

The second argument of true sets the event to a bubbling event.

Finally you'll need to change your handler. Right now, it's using e.currentTarget, which isn't going to be what you expect in this case (usually it is, thought).

You have to understand the difference between e.target and e.currentTarget. e.target is the actual target of the event, independent of how its bubbling or capturing. e.currentTarget on the other hand is the element which is presently handling the event. So in your case e.currentTarget is an instance of ClassA (the instance that the event handler is actually attached to) and e.target is an instance of ClassB (the instance that the event was dispatched on). During the event lifecycle, e.currentTarget will change as the event moves around, but e.target should always be the same.

In this case, you want to reference the actual target of the event, not the element that is currently processing the event. So you need to change your handler to:

function hello(e:Event)
{
    trace(e.target.abc);
}

And then it should work. You can find a working example here that encapsulates the changes I've described.

If these classes weren't DisplayObjects then you would have to take a different approach -- either by using a signal pattern or to manually listen for an retrigger the event inside ClassA.


Need Your Help

where syslog store error messages

c linux syslog

syslog() generates a log message syslog(LOG_ERR, "%s failed: %d (%m)", str, errno); syslog(LOG_NOTICE, "%s failed: %d (%m)", str, errno); syslog(LOG_INFO,"%s",str); where does it store ...

Using other documents outside a jar file

java jar executable-jar

I just realised that I cant use files from outside a jar archive. If thats the case then when I deploy apps that need other documents, say an xml file, do i send the xml alongside the app or there ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.