Skip to main content
Inspiring
October 26, 2008
Question

getImageReference and eventListeners

  • October 26, 2008
  • 2 replies
  • 605 views

Hi,

Through a textField you can access images (or it's Loader object) with
getImageReference( imgId ). This works fine.

Now I want to add eventListeners to the Loader to detect mouseClicks on
the images. But somehow that part won't work.


Code from a quick test:
In a function which loads the XML I have these lines:

var img:Loader = Loader( html_txt.getImageReference( "img_0" ) );
img.contentLoaderInfo.addEventListener( Event.COMPLETE, loadedImage );

the eventListeners are:

function clickImage( evt:MouseEvent ):void
{
trace( evt.target );
}
function loadedImage( evt:Event ):void
{
trace( evt.target.content, "is loaded" )
var li:LoaderInfo = LoaderInfo( evt.target );
li.content.addEventListener( MouseEvent.CLICK, clickImage );
li.loader.addEventListener( MouseEvent.CLICK, clickImage );
}

Al this traces:
[object Bitmap] is loaded

If I click on tha image in the textField however, nothing happens... Now
I could go with the textField, have it listen to mouseEvents, get the
bounds of all the images in it and see over which the mouse is at that
moment, but that seems rather silly if I could get each image to
register it's own listeners.

Anybody?

Thanks in advance.
Manno

--
----------
Manno Bult
http://www.aloft.nl
This topic has been closed for replies.

2 replies

kglad
Community Expert
Community Expert
October 27, 2008
try it. it works for me.
Inspiring
October 27, 2008

Hi kglad and others,

well, it works, but not as intended (at least, not my intention ;) ).

addChild( img ); adds the image to the root (or parent of the textField) thereby removing it, like documentation states, from the textField itself. Ok, it becomes clickable, but not as part of the content of the textField.

You cant issue addChild() on the textField in anyway so that won't work.

Has anyone been able to do sort of the same thing as I'm trying now?

Below is some simplified code working on this XML:
<?xml version="1.0" encoding="utf-8"?>
<rootnode>
<p>Lorem ipsum... </p>
<img src="plaatje.jpg" id="img_0" />
<p>Donec malesuada, arcu eget tristique convallis... </p>
</rootnode>

Thanks,
Manno

kglad
Community Expert
Community Expert
October 27, 2008
add img to the display list.
Inspiring
October 27, 2008
Thanks, I'll give it a try (not at the right place to do so now).

Never thought of that since it is already visible (so on the displayList, right?) as a sort of child of the textField...