Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Old problem still killing me - image resizing

Status
Not open for further replies.

Leozack

MIS
Oct 25, 2002
867
0
0
GB
I don't know why, but the most basic of tasks sometimes seems to fail. I'm using flash mx 6. I dn't have the moviecliploader thing from flash 7 therefore.

For my flash I start by making an xml object with an onLoad function where the rest of my code resides. In there, I've tried all sorts of things. I can get an image to appear no problem, but it's too big and I need to resize it dynamically. I've made an onData and onLoad function for the containing movieclip but even a simpel trace in thsoe is never activated. I've tried making a sub sub container to load the image into incase it was wiping the details from the original container or something but that makes no difference.

I can use use all my .width and ._xscale stuff on a container that's already there as a test on the stage staticly, but not in the script it seems. I'd psot code here but I've tried so many ways to do it I've just ripped all my hair out! All I want is to make a movieclip container (done dynamically as the images I'm loading come from a loaded xml document) and then load an image into that movieclip, and then resize it if it's too big.

Can anyone sooth my headache? :(

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Yeah thanks that's what I'm after doing, though I have code to check the largest dimension and set the scale factor for that and then apply it to both dimensions.
Code:
var scaleFactor = thisImage_mc._width>thisImage_mc._height ? maxWidth/thisImage_mc._width : maxHeight/thisImage_mc._height;
if (thisImage_mc._width > thisImage_mc._height) {
	thisImage_mc._yscale = scaleFactor * 100;
	thisImage_mc._xscale = thisImage_mc._yscale;
} else {
	thisImage_mc._xscale = scaleFactor * 100;
	thisImage_mc._yscale = thisImage_mc._xscale;
}
thisImage_mc._xscale = thisImage_mc._yscale;
What I can't do it makeit work. If I trace the movie width/height I get 0 0 even on lines after the loadMovie. I know you're supposed to somehow shove it in a loader so it doesn't fire until the load is complete, but I made onLoad and onData and neither get fired! And the onloadinit is for flash 7+ isn't it? I had a full method for 7 which didn't seem to work for me in 6
Code:
var maxWidth = 640; //original width of container
var maxHeight = 480; //original height of container
var mcl = new MovieClipLoader();
mcl.loadClip(thisFile,thisImage_mc);
mcl.onLoadInit = function(thisImage_mc:MovieClip){
	if (thisImage_mc._width >= maxWidth ) {
		var ctnrWidth = thisImage_mc._width;
		// Calculate percentage by which image will need to change to fit 
		scalePercent = (ctnrWidth > maxWidth) ? (maxWidth * 100) / ctnrWidth : scalePercent;
		thisImage_mc._xscale = scalePercent;
	}
	if (thisImage_mc._height >= maxHeight) {
		var ctnrHeight = thisImage_mc._height;
		// Calculate percentage by which image will need to change to fit
		scalePercent = (ctnrHeight > maxHeight) ? (maxHeight * 100) / ctnrHeight : scalePercent;
		thisImage_mc._yscale = scalePercent;
	}
}
So any help with the blindingly obvious thing I'm missing that lets you createEmptyMovieClip (thisImage_mc) using a prebuilt one onstage (image_mc) which is actually an instance of a library one (Image) (not sure if I need to preplace one but it makes the initial position of the clones correct anyways) will be much appreciated! I'm sure it's all down to waiting for the image to load in somehow so the width/height etc aren't 0! And I have code examples/tutorials coming out my ears yet somehow my failiure triumphs! A frustrating keep-you-up-late problem ><

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
I can literally just post the lines that don't play ball if that's easier

This contains both code to just load the image into a movieclip, code to try an ondata/onload handler, and code to load an image into a sub sub movieclip. Nothing's worked so far. I can load the image of course but what doesn't work is any knid of control over it. If I get a trace at all for sizes it comes out as "0 0" :(

Code:
thisFile = "images/image1.jpg";
thisImage = 1;
var thisImage_mc = image_mc.createEmptyMovieClip("thisImage_mc",10+thisImage);
//thisImage_mc.onData = function() { trace("go"); }
thisImage_mc.createEmptyMovieClip("thumb_container",0); 
//thisImage_mc.loadMovie(thisFile);
thisImage_mc.thumb_container.loadMovie(thisFile);
trace(thisImage_mc.thumb_container._width + " " + thisImage_mc.thumb_container._height);

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
There give your email a check :)

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Any joy with that? =.=

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
x_x I guess if you got the email you had no luck :(

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Just to let you know, I have what I think is successful code example for image resizing though I'm not sure entirely how the prototype function is working maybe someone can tell me what it's doing precisely that I can't make work without it?

Code:
MovieClip.prototype.loadImage = function(url, vars) {
    if (this.onData != undefined && this.onData != null) {
        this._parent.createEmptyMovieClip("__fixEvents", 7777);
        this._parent.__fixEvents.theTarget = this;
        this._parent.__fixEvents.onData = this.onData;
        if (this.onLoad != undefined && this.onLoad != null) {
            this._parent.__fixEvents.onLoad = this.onLoad;
        }
        this._parent.__fixEvents.onEnterFrame = function() {
            this.oldv = this.v;
            this.v = this.theTarget.getBytesLoaded();
            if (this.v != this.oldv) {
                this.onData.call(this.theTarget);
            }
            if (this.v == this.theTarget.getBytesTotal() && (this.theTarget._width>0) && (this.theTarget._height>0)) {
                this.theTarget.onData = this.onData;
                if (this.onLoad != undefined) {
                    this.theTarget.onLoad = this.onLoad;
                }
                this.onLoad.call(this.theTarget);
                this.removeMovieClip();
            }
        };
    }
    this.LoadMovie(url, vars);
};

I then wrote my own code to just say

Code:
thisImage_mc._quality = "BEST";
thisImage_mc.onData = function() {
};
thisImage_mc.onLoad = function() {
    if (thisImage_mc._width > thisImage_mc._height) {
        thisImage_mc._xscale = thisImage_mc._yscale = (maxWidth/thisImage_mc._width) * 100;
    } else {
        thisImage_mc._yscale = thisImage_mc._xscale = (maxHeight/thisImage_mc._height) * 100;
    }
};
thisImage_mc.loadImage(thisFile);

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top