Hi,

I have a movie clip called dsp_albumcover (Already created and on the stage, instance name is dsp_albumcover)

I also have a variable with a .jpg file (Can be changed to any other though) defined as

var albumimg:String = '/albumcover.jpg';

How do I load this in to dsp_albumcover?
dsp_albumcover is 128 pixles high x wide and the image is the same dimentions.

Thanx,
Sam

P.S. Before anyone says that I should just include it in my libary, /albumcover.jpg?ID={ID} is the actual image that would be called and would be retrieved from a DB

Dani AI

Generated

As 's example shows, a Loader is the right tool to push a dynamic JPG into an on-stage MovieClip (dsp_albumcover). The snippet below builds on that by adding basic error reporting, a cache-bust for DB-driven URLs, smooth rendering, fit-to-box scaling for the 128×128 container, and safe cleanup — common things that break a quick tutorial copy/paste.

Quick checklist before loading:

  • Confirm the exact image URL (a leading / hits the webroot; test the full URL in a browser to catch 404s).
  • When testing locally, set Publish → Local Playback Security appropriately (network access vs. local).
  • If loading from another domain and pixel access is needed, ensure a suitable crossdomain.xml; display-only loads usually work without it.
  • If the DB reuses the same URL, append a timestamp query (ts=) to avoid cached images.

Example: robust onComplete handler that fits, centers and smooths the bitmap (adds simple IO error handling and removes previous children):

var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageComplete);
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onImageError);

// cache-bust for DB-driven images
var cacheBuster:String = (albumimg.indexOf("?") >= 0) ? "&ts=" + new Date().time : "?ts=" + new Date().time;
imageLoader.load(new URLRequest(albumimg + cacheBuster));

function onImageComplete(evt:Event):void {
    // clear previous content (careful if dsp_albumcover has static artwork)
    while (dsp_albumcover.numChildren > 0) dsp_albumcover.removeChildAt(0);

    var bmp:Bitmap = evt.target.content as Bitmap;
    if (bmp) {
        bmp.smoothing = true;
        var origW:Number = bmp.width;
        var origH:Number = bmp.height;
        var scale:Number = Math.min(dsp_albumcover.width / origW, dsp_albumcover.height / origH);
        bmp.scaleX = bmp.scaleY = scale;
        bmp.x = Math.round((dsp_albumcover.width - origW * scale) / 2);
        bmp.y = Math.round((dsp_albumcover.height - origH * scale) / 2);
        dsp_albumcover.addChild(bmp);
    } else {
        // fallback: add raw loader display object
        dsp_albumcover.addChild(evt.target.loader);
    }

    imageLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onImageComplete);
    imageLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onImageError);
}

function onImageError(evt:IOErrorEvent):void {
    trace("Image load error:", evt.text);
    // optional: display a placeholder graphic inside dsp_albumcover
}

Notes: avoid blindly removing every child if dsp_albumcover contains masks or UI chrome. When frequently swapping images, call imageLoader.unloadAndStop() (Flash Player 10.1+) to free memory. This expands on 's simple example and addresses the common runtime issues that cause a load to appear to "not work."

Recommended Answers

All 4 Replies

Member Avatar for Member #46692

What the hell is this? Go get yourself a flash actionscript 3.0 book or something.

I'm being SERIOUS.

I have been following tutorials all day and none of them work, I just thought that an WEB DESIGN FORUM might be able to help, I didn't see the point in saying "This has taken me the last 8 hours to do but it doesnt work

Some Code

"

Member Avatar for Member #334542

As Imthwee said, please try to find tutorials too.

Anyway Please follow this to get your work done:

1) Just create an empty movie clip with a instance name (emptyMC)
2) Place your jpeg file in the same location

use this code in timeline:

const IMAGE_URL:String = "followme.png";

var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, ldr_complete);
ldr.load(new URLRequest(IMAGE_URL));



function ldr_complete(evt:Event):void 
{
	emptyMC.addChild(ldr);
}

Hope this helps!

Note: Don't worry about the size of the movie clip, Just an empty movie clip thats it!

commented: Soved a problem I had been struggeling with for days :D +2
commented: Although it's a question that could be answered with any actionscript 3.0 resourse, still...props to you. +11

As Imthwee said, please try to find tutorials too.

Anyway Please follow this to get your work done:

1) Just create an empty movie clip with a instance name (emptyMC)
2) Place your jpeg file in the same location

use this code in timeline:

const IMAGE_URL:String = "followme.png";

var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, ldr_complete);
ldr.load(new URLRequest(IMAGE_URL));



function ldr_complete(evt:Event):void 
{
	emptyMC.addChild(ldr);
}

Hope this helps!

Note: Don't worry about the size of the movie clip, Just an empty movie clip thats it!

Thank you so much, I would buy you a virtual pint but I don't think that is possible =P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.