开发者

ActionScript 3.0 Getting Size/Coordinates From Loader Content

i'm开发者_JAVA百科 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?


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();
 }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜