2 Replies Latest reply on Jun 6, 2007 12:37 AM by mforsberg

    as3 resizing loaded image

    asden
      I'm loading a image into a sprite:

      var loader:Loader = new Loader();
      addChild(loader);
      loader.load(new URLRequest("ball.png"));

      But if I try to resize the sprite the image stays the same size and the width of the sprite reports as zero. If I add graphics as in:

      graphics.beginFill(color);
      graphics.drawCircle(0,0,radius);
      graphics.endFill();
      var loader:Loader = new Loader();
      addChild(loader);
      loader.load(new URLRequest("ball.png"));:

      I can then resize the sprite and the image changes size accordingly and the sprite width is as expected. Why? How do I have the image resize as the Sprite resizes without adding graphics (which are not required).
        • 1. Re: as3 resizing loaded image
          vorpal
          your image doesn't resize in the movieClip because you apply width and height while the image isn't totaly loaded into the movieClip.
          Once the image is completely loaded, the movieClip get the width/height image properties, forgetting the ones you tell it to use.

          If you want to resize your image, you have to :
          - wait untill it s completely loaded before applying with/height properties.
          or
          - use the _xscale and _yscale properties whenever (they can be applied and remembered even if the image isn't totaly loaded)
          • 2. Re: as3 resizing loaded image
            mforsberg
            Since the graphic isn't there yet, the only width and height the clip has to relate to is 0 0 meaning that the xscale/yscale which are set when you set a _width _height to an object (relating to it's total _wdith _height) stay 100.

            You have to add an event to trigger the scale function once the image is loaded. Since I'm not very familiar to AS3 loading I'm not sure, but back in AS2 a movieclip could have a bytesLoaded equal to the bytesTotal but still could have no image drawn (I think even using an onEnterFrame function the image sometimes could wait a frame or two to be drawn once loaded). So the proper check to really see if an image had been loaded into a movieclip was to check the _width to be above 0. I suggest you do the same. It will be alot less performance demanding than drawing a circle, also, if your circle's radius does not match the image size the scale of the image once loaded could be off.