views:

407

answers:

1

i'm attempting to position a textfield to the bottom left of an image that is added to the display list from the Loader() class. i don't know how to access the width/height information of the image.

var dragSprite:Sprite = new Sprite();
this.addChild(dragSprite);

var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest("picture.jpg"));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, displayPic, false, 0, true);

function displayPic(evt:Event):void
    {
    dragSprite.addChild(evt.target.content);
    evt.target.removeEventListener(Event.COMPLETE, displayPic);
    }

var tf:TextField = new TextField();
tf.text = "Picture Title";
tf.width = 200;
tf.height = 14;
tf.x //same x coordinate of dragSprite
tf.y //same y coordinate of dragSprite, plus picture height, plus gap between picture and text

addChild(tf);

within the displayPic function, i could assign the evt.target.content.height and evt.target.content.width to variables that i could use to position the text field, but i assume there is a more appropriate and direct way?

+1  A: 

There is no direct way since you have to wait the image to be loaded to access width, and height.

But you can place you text as soon as the complete is done if it`s fit your design. Store the value into some var so you can reuse it when moving the sprite.

//...
var tf:TextField = new TextField();
tf.text = "Picture Title";
tf.width = 200;
tf.height = 14;

addChild(tf);

var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest("picture.jpg"));
imageLoader.contentLoaderInfo.addEventListener(
     Event.COMPLETE, displayPic, false, 0, true
);

var offsetX:Number=0;
var offsetY:Number=0;

function positionText():void {
  tf.x=dragSprite.x + offsetX;
  tf.y=dragSprite.y + offsetY;
}

function displayPic(evt:Event):void {
 var li:LoaderInfo=evt.target as LoaderInfo;

 if (li===null)
    return;

 li.removeEventListener(Event.COMPLETE, displayPic);

 var dob:DisplayObject=li.content;
 if (dob!==null) {
  dragSprite.addChild(dob);

  // set only once the offset depending on the loaded image
  offsetX = ...//
  offsetY = dob.height+gap //...

  // position text using the offset setted
  // so you can reuse the function when moving your sprite
  positionText();
 }
}
Patrick
thanks for this. i was assuming that maybe i could have called the dragSprite's child and get the info from it, but this way seems just as well.also, i spotted a small error in your example. var dob:DisplayObject = evt.target.content as DisplayObject;, without content it doesn't work. but besides that, according to the documentation the loader() class only loads display objects (images, swf's) so evaluating if it's a display object upon completion is redundant.
TheDarkInI1978
@TheDarkIn1978 Good catch, yes i forget it, thanks. Since your content is a child of dragSprite you can get information from there (after content is loaded), but since i don't know how it will be used, how many child in your drag sprite,etc... i just put the part based on the loaded content.
Patrick