MMOCC the isometric world scene, 2006–now
Archive. Archived from the original MMOCC Forum (2006–2010), recovered from the Internet Archive. The forum is read-only history now — the scene talks on Discord. If you posted here and want something removed, get in touch.

NEED HELP: Resizing Externally Loaded Image In AS3

2 posts · 2011-11-17 · MyBB era
11-17-2011 02:21 AM #1
Code:
import flash.net.URLLoader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.Sprite;
import flash.display.Loader;

var currentContainer:Sprite;
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, loadXML);
xmlLoader.load(new URLRequest('advertimageloader.xml'));

function loadXML(e:Event):void
{
    xmlData = new XML(e.target.data);
    trace(xmlData);
    trace(xmlData.image[0].filename);

    imageData = new XML(xmlData.image[0].filename);
    trace(imageData);
    
    var url:String = imageData;
    trace("URL: "+url);
    var urlRequest:URLRequest = new URLRequest(url);
        var imag:Loader = new Loader();
            imag.load(urlRequest);
                addChild(imag);
}

When I add imag.width or imag.height, the area the image should be loaded goes black... I think it has something to do with adding it to the stage as a bitmap but I'm unsure how to implement that from where I am now... Can anybody help?

Edit: Without the imag.width or imag.height, it shows fine but if the image loaded is too big or two small it wont take the full space I'd like it to, as it would.

Added "NEED HELP:" to title to show it as a question and not a tutorial. Added "In AS3" to indicate it's in AS3.

11-17-2011 10:08 AM #2
Maybe getting the bitmapdata of the image, then having an empty bitmap and then drawing the bitmapdata of the image into it with a matrix, then just scale the matrix.
Something like this:

Code:
package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.geom.Matrix;
    public class Main extends Sprite
    {
        [Embed(source = "image.png")] private var imgClass:Class;
        public var canvas:Bitmap = new Bitmap();
        public var myImage:BitmapData = new imgClass().bitmapData;
        public var myMatrix:Matrix = new Matrix();
        public var scaleNumb:Number = .2;
        public function Main()
        {
            myMatrix.scale(scaleNumb, scaleNumb);
            canvas.bitmapData = new BitmapData(myImage.width*scaleNumb, myImage.height*scaleNumb, true, 0xffffff);
            canvas.bitmapData.draw(myImage, myMatrix);
            addChild(canvas);
        }
    }
}


Recovered from /printthread.php?tid=18854 · captured 2012.