Trouble getting/using _width/_height of a loaded jpeg

Hi,

I’m having trouble getting the _width & _height of a jpeg loaded using loadMovie.

I’m trying to reposition it (pseudo-registration point) for rotation in a container movie clip.

If I use hard-coded numbers, it works fine. However, if I try to position it based on its _width and _height properties, it’s no go… in fact, it doesn’t even show up on stage.

Are there problems getting those properties from loadMovie clips?

It’s the same idea as assigning an event handler to the loaded picture. Check number 3 on the following post:
http://www.kirupaforum.com/forums/showthread.php?t=61309

In that thread, you mention that you can’t assign event handlers (or access these properties) until the clip is done loading. However, that’s precisely where I’m trying to access the clip’s properties.

In the container clip, I’ve got an onEnterFrame function which checks to see that it’s 100% loaded and then it attempts to set the image’s position (based on it’s measurements). Perhaps I’m trying to reference the clip incorrectly?

Have a look:

<!-- an excerptfrom the outer clip’s onEnterFrame function
<!-- “jpg” is the name of the loadMovie clip

                  if(this.percent.text == "100") {
                      this.percent.text = "";
                      
                      var w = this.jpg._width/2;// didn't work
                      var h = this.jpg._height/2;//didn't work
                      //this.jpg._x = -(this.jpg._width/2);//didn't work
                      //this.jpg._y = -(this.jpg._height/2);//didn't work

//this.jpg._x = -w;//didn’t work
//this.jpg._y = -h;//didn’t work
with (this.jpg){
_x = -60;//static position I gave
_y = -60;//static position I gave
_yscale = 24;
_xscale = 24;
}

                      delete this.onEnterFrame;
                  }

As you can see, I’ve tried a few ways to reference the clip, thinking that’s the problem. How would you write this code?

Cheers,

Mike

This will fail:

createEmptyMovieClip("container_mc", 1);
container_mc.loadMovie("picture.jpg");
trace(container_mc._height);//will return 0
trace(container_mc._width);//will return 0

This will work:

createEmptyMovieClip("container_mc", 1);
createEmptyMovieClip("loader_mc", 2);
container_mc.loadMovie("picture.jpg");
loader_mc.onEnterFrame = function() {
	var t = container_mc.getBytesTotal();
	var l = container_mc.getBytesLoaded();
	if (t && l == t) {
		trace(container_mc._height);
		trace(container_mc._width);
		this.removeMovieClip();
	}
};

Ok, I used your code and approach but I think because it’s all taking place within a function on the outer clip, there’s still some problem.

<!-- short example of my situation


 
 _root.outerclip.loadPic = function(){
 
 // your code (modified)
 
 this.createEmptyMovieClip("container_mc", 1);
 this.createEmptyMovieClip("loader_mc", 2);
 this.container_mc.loadMovie("picture.jpg");
 this.loader_mc.onEnterFrame = function() {
      var t = this._parent.container_mc.getBytesTotal();
      var l = this._parent.container_mc.getBytesLoaded();
      if (t && l == t) {
 //unfortunately, I can't trace at this point
 //because I'm testing through my server (using PHP)
          //trace(this._parent.container_mc._height);
          //trace(this._parent.container_mc._width);
         var h = this._parent.container_mc._height;
         var w = this._parent.container_mc._width;
         this._parent.container_mc._x = -(w/2);
         this._parent.container_mc._y = -(h/2);
          this.removeMovieClip();
      }
  }
 
 }
 
 

Anyway, this above code doesn’t work – however, if I set the _x and _y values with hard-coded numbers, it works. I even tried to put an if statement ( if (this._parent.container_mc._width > 0) … )… didn’t work.

Any ideas?

Thanks for your attention, Claudio.

Cheers,

Mike

Have you tried tracing your paths?
Does the following variables return any values at all?
var t = this._parent.container_mc.getBytesTotal();
var l = this._parent.container_mc.getBytesLoaded();

Ok, I’ve gotten it to work – that should be good news, but it’s not that good because I feel more confused than ever about the details of Flash’s referencing.

This is my function:

Upimage = the outer clip
Jpg = the container clip that loads the Jpg
Loader_mc = as you suggested, the clip that checks the loading info for “jpg”


  _root.upimage.loadJPG = function (jpg1 : String){
              
              // create mc
              this.createEmptyMovieClip("jpg", 1);
              this.jpg.loadMovie(jpg1);
              this.createEmptyMovieClip("loader_mc", 2);
              this.loader_mc.onEnterFrame = function() {
                  var t = this._parent.jpg.getBytesTotal();
                  var l = this._parent.jpg.getBytesLoaded();
                  
                  if (t && l == t) {
                      //this.percent.text = "loaded!!!!!!!!!!!!!!!!!!!!!";
                      if (this._parent.jpg._width > 0){
                                   
                              this._parent.jpg._x = -(this._parent.jpg._width/2);
                              this._parent.jpg._y = -(this._parent.jpg._height/2);
                              this._parent.jpg._yscale = 24;
                              this._parent.jpg._xscale = 24;    
                          }
                      //this.removeMovieClip(); // notice this is commented out
                  }
              }
          }
  

As you can see, I had to comment out the “this.removeMovieClip” line. Why? Because if I left it in, it would remove “upimage” and everything inside. This has always been a problem for me. When I tried your previous code (with the “loader_mc” and “container_mc” references):


  createEmptyMovieClip("container_mc", 1);
  createEmptyMovieClip("loader_mc", 2);
  container_mc.loadMovie("picture.jpg");
  loader_mc.onEnterFrame = function() {
      var t = container_mc.getBytesTotal();
      var l = container_mc.getBytesLoaded();
      if (t && l == t) {
          trace(container_mc._height);
          trace(container_mc._width);
          this.removeMovieClip();
      }
  };
  

… the Flash compiler gave me errors, saying that there were no loader_mc or container_mc properties. So, I used the “this._parent” prefix. Inside a function on clip, “this” stands for what? I’m really confused now.

I’m having trouble tracing my paths because this section is only reachable with the database. I did a small experiment before, comparing “this”, “_parent”, and “this._parent” in an event handler and the results were:

“this” is the clip that has the event handler
"_parent" and “this._parent” both refer to the parent of the clip

HOWEVER, when I compile code, Flash won’t accept “_parent”… it will only accept “this._parent”… I’m lost with referencing in Flash… do you have a guideline, rulebook, etc.?

Thanks again for your time, mate.

Mike

Just tried it like this to see what would happen:


  _root.upimage.loadJPG = function (jpg1 : String){
              //set image var
              this._parent.image = jpg1;
              // create mc
              createEmptyMovieClip("jpg", 1);
              jpg.loadMovie(jpg1);
              createEmptyMovieClip("loader_mc", 2);
              loader_mc.onEnterFrame = function() {
                  var t = jpg.getBytesTotal();
                  var l = jpg.getBytesLoaded();
                  
                  if (t && l == t) {
                       if (jpg._width > 0){
                          jpg._x = -(jpg._width/2);
                          jpg._y = -(jpg._height/2);
                          jpg._yscale = 24;
                          jpg._xscale = 24;    
                      }
                      this.removeMovieClip();
                  }
              }
          }
  

Check out the errors I get from the compiler:

Error code.as: Line 414: There is no method with the name ‘createEmptyMovieClip’.
createEmptyMovieClip(“jpg”, 1);

Error code.as: Line 415: There is no method with the name ‘jpg’.
jpg.loadMovie(jpg1);

etc.

Total ActionScript Errors: 11 Reported Errors: 11

What do you think?

Mike