View Full Version : dispatchevents between components andsprite classes
giohappy
06-27-2008, 04:48 PM
Hello list. I'm a newbie with AS 3 and Flex so maybe this is a stupid question...
I'm creating an MXML project inside wich I've incorporated a previous project I had made (an object composed by multiple .as classes).
Inside the MXML file I've added it with:
gr = new OF(); //my class
OFpanel.rawChildren.addChild(gr) // OFpnel is my mx:Canvas component;
then I've simply added an eventlistener to OFpanel:
OFpanel.addEventListener("atry",myFunction);
public function myFunction(e:Event):void
{
// the code
}
My OF class contains just a line to dispatch the event when a specific action happens (a mouseOver in my case):
dispatchEvent(new Event("atry"));
Nothing happens.
I've tried adding [Event(name="atry",type="flash.events.Event")], to the MXML file, without results...
What am I missing?
Thanks for hints,
Giovanni
drkstr
06-27-2008, 06:24 PM
Do you mean?
gr.addEventListener("atry",myFunction);
If that doesn't work, provide the complete code you use for capturing the mouse over Event and dispatching the custom Event.
Best Regards,
~Aaron
giohappy
06-28-2008, 02:06 AM
Hy drkstr. Thanks for answering.
My mouseOver event fires the following function correctly:
private function mouse_move_closest( event:Event ):void {
//various code
dispatchEvent(new Event("atry"));
}
My MXML contains (I past just an excerpt):
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundColor="#FFFFFF"
applicationComplete="init();"
layout="absolute"
>
<mx:Metadata>
[Event(name="atry", type="flash.events.Event")]
</mx:Metadata>
<mx:Script>
<![CDATA[
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
public var gr:OF;
public function init():void
{
gr = new OF();
OFpanel.rawChildren.addChild(gr);
OFpanel.addEventListener("atry", drawok);
}
public function drawok(e:Event):void
{
myArea.text = "ok";
}
]]>
</mx:Script>
<mx:Canvas id="OFpanel" top="10" left="10" height="300" width="400"></mx:Canvas>
<mx:TextArea id="myArea" bottom="10" left="10" width="100%" click="disp()" />
</mx:Application>
I've added the EventListener to OFpanel, but it could be any other component inside the MXML.
My task is: make a loosely coupling between my OF class and the Flex application. OF dispatches an event, and I want any component nide Flex to be able to detect it.
drkstr
06-28-2008, 03:28 AM
The problem is that OFpanel is not dispatching the event, the instnce of 'OF' is.
OFpanel.addEventListener("atry", drawok);
For this to work, you would either have to attach your event listener to 'gr' or capture the event in OFPanel and dispatch it again (called bubbling) .
Try this:
<mx:Canvas id="OFpanel" top="10" left="10" height="300" width="400" atry="dispatchEvent(new Event('atry'));" />
This would assume the following line is in your OF.as (right before the class deceleration).
[Event(name="atry",type="flash.events.Event")]
Best Regards,
~Aaron
drkstr
06-28-2008, 07:26 AM
Sorry, I just realized that was totally wrong. The syntax I gave you is for capturing internal events. For instance, if you made a custom component called OFpanel which dispatched the event from within.
There is no way around it. You will need to have an event listener attached to your instance of 'OF' somewhere or nothing will capture the event it dispatches.
If your aim is to keep things loosely coupled, I would remove the 'OF' code from the main mxml and put it in a custom component that listens for the event and bubbles it up to the main class. If you are instantiating it in the the main class, this inherently makes it tightly coupled to 'OF'. It also violates the encapsulation principal.
So to sum it all, you will need gr.addEventListener somewhere or no dice.
Best Regards,
~Aaron
giohappy
06-28-2008, 02:38 PM
Thanks again...
Well, I think I have to learn better the event handling mode lin Flex and AS.
I thought that the event raised from the OS instance would raise the display objects hierarchy and that it could be grabbed by the parent objects of OF.
So being OF a child added to OFpanel I thought that OFpanel could listen to it...
I've never tried to make a Flex custom component. OF is a class independent frm Flex, as it can be used directly in a Flash Player by itself... I'll consider your advice.
My wish is to realize a publisher/subscriber model: OF renders graphical datas, and whenever the user interact in some ways with them, OF should throw an event.
All the components and object subscribed to such an event should be able to grab it and react in their own ways.
Is it possible only incorporating it in a customobject?
giohappy
06-28-2008, 04:35 PM
I think I've solved the problem.
I simply had to set to true the bubble property when instantiating the Event object:
dispatchEvent(new Event('atry',true));
I thought that bubbling was set to true by default...
Now my OFpanel component can grab the event.
I need to reach a better application design yet,but at least it works :-)
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.