Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>eventListeners won't register unless the sprite is attached to the application.</p> </blockquote> <p>Can you give a reference? Because I believe the issue is something else.</p> <p>When you declare an mxml file with <code>PhotoClass</code> as its root tag, you're extending the PhotoClass. The <code>&lt;mx:Image&gt; tag</code> in the mxml defines a public variable of type Image in the mxml class; and you already have a <code>public var image:Image;</code> in the super class. This will create a conflict - I'd expect a compile time error. </p> <p>My guess is that since constructor is called first, it assigns an <code>Image</code> object to the public variable <code>image</code>. When the mxml is executed and the children are created, it assigns a new <code>Image</code> object to the public variable <code>image</code> (instead of correctly throwing an error). Now, the event listener was added to the <code>Image</code> instance created in the constructor, but by the time you set <code>image.source</code> in <code>setProperty</code>, it is a different object altogether. That is why it works when you move the event listener to <code>setPicture</code> - there you're assigning it to the new object. </p> <p>Change the <code>id</code> of the mxml Image tag to something else and it will work even if the event listener was added from the constructor. Change the name of public var image to something else and you'll get a compile time error in <code>PhotoClass</code>.</p> <p>Btw, what does the Image tag in the mxml do - is that for showing a different image? In that case you must change its <code>id</code>.</p> <pre><code>&lt;!-- MyCanvas.mxml --&gt; &lt;mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"&gt; &lt;mx:Label id="lbl"/&gt; &lt;/mx:Canvas&gt; &lt;!-- the main application class --&gt; &lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:local="*" creationComplete="onCreate()" layout="vertical"&gt; &lt;local:MyCanvas id="canvas1"/&gt; &lt;mx:Script&gt; &lt;![CDATA[ public var canvas2:MyCanvas; public function onCreate():void { this.canvas1.lbl.text = "MyCanvas_1"; canvas2 = new MyCanvas(); this.addChild(canvas2); canvas2.addEventListener(FlexEvent.CREATION_COMPLETE, onCanvas2Created); } public function onCanvas2Created(e:FlexEvent):void { this.canvas2.lbl.text = "MyCanvas_2"; } ]]&gt; &lt;/mx:Script&gt; &lt;/mx:Application&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload