Ultrashock Forums > Flash > ActionScript
[AS2] Scope Issue Using "onLoadInit" While Loading Images

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
[AS2] Scope Issue Using "onLoadInit" While Loading Images
Old 2009-06-24 Last edited by creationmachine : 2009-06-25 at 09:28.

Background:

I'm creating a scrolling function that will scroll a dynamic list of images. I am putting all content into MovieClip container that will then scroll. The images are being loaded via XML but there is no height or width passed from the XML spec. I must detect the height and width post load.

The Problem:

The container name is "image_mc" (see attached code). The "height" attribute needs to be updated by the total height of the loaded images in the stack. This of course needs to happen after all files have been loaded. I must be missing something. Any help is appreciated. I simplified the code for clarity.

The Question:

How do I pass a variable from within "onLoadInit" to update a MovieClip's height? I can pass variable into "onLoadInit" just fine but getting a variable out has been a pain.

ActionScript Code:
  1. /*
  2. * ImageLoaderScope.as
  3. * Place "ImageLoaderScope.main(this);" in the root of your FLA doc.
  4. * Put any image in the "image_url[i]" variable.
  5. */
  6.  
  7. class ImageLoaderScope {
  8.    
  9.     private var image_clip:Array = [];
  10.     private var image_url:Array = [];
  11.    
  12.     public function ImageLoaderScope(target:MovieClip){
  13.         build(target);
  14.     }
  15.    
  16.     public function build(target:MovieClip) {
  17.         var thisMC = this;
  18.         var app = { stack_x: 50, stack_y: 50 };
  19.        
  20.         // This "height" needs to be updated by the total height of the loaded images in the stack.
  21.         var image_mc:MovieClip = createRectangle(350, 200, 0x000000, target);
  22.        
  23.         for (var i = 0; i < 3; i++) {
  24.             image_url[i] = "bacon.jpg";
  25.             image_clip[i] = image_mc.createEmptyMovieClip("image_clip" + i, image_mc.getNextHighestDepth());
  26.             var mc_listener:Object = new Object();
  27.            
  28.             mc_listener.onLoadInit = function(target_mc:MovieClip) {
  29.                 target_mc._x = app.stack_x;
  30.                 target_mc._y = app.stack_y;
  31.                 app.stack_y = app.stack_y + target_mc._height;
  32.                 app.image_mc._height = app.stack_y;
  33.             }
  34.            
  35.             mc_listener.onLoadStart = function(target_mc:MovieClip) {
  36.                
  37.             }
  38.            
  39.             mc_listener.onLoadComplete = function(target_mc:MovieClip) {
  40.                
  41.             }
  42.            
  43.             var mc_loader:MovieClipLoader = new MovieClipLoader();
  44.             mc_loader.addListener(mc_listener);
  45.             mc_loader.loadClip(image_url[i], image_clip[i]);
  46.         }
  47.     }
  48.  
  49.     public function createRectangle(width:Number, height:Number, color:Number, scope:MovieClip):MovieClip {
  50.         scope = (scope == undefined) ? this : scope;
  51.         var depth:Number = scope.getNextHighestDepth();
  52.         var mc:MovieClip = scope.createEmptyMovieClip("mc_" + depth, depth);
  53.         mc.beginFill(color);
  54.         mc.lineTo(0, height);
  55.         mc.lineTo(width, height);
  56.         mc.lineTo(width, 0);
  57.         mc.lineTo(0, 0);
  58.         return mc;
  59.     }
  60.  
  61.     public static function main(target:MovieClip){
  62.         var player:ImageLoaderScope = new ImageLoaderScope(target);
  63.     }
  64. }

Attachment 499
Attached Files
File Type: zip ImageLoaderScope.zip (17.4 KB, 3 views)
postbit arrow 4 comments | 199 views postbit arrow Reply: with Quote   
Registered User
creationmachine is offline
seperator
Posts: 10
2008-09-11
creationmachine lives in United States
seperator

Ultrashock Member Comments:
creationmachine creationmachine is offline creationmachine lives in United States 2009-07-01 #2 Old  
I am assuming it is impossible to get "onLoadInit" to return a value?
Reply With Quote  
Isocase's Avatar Isocase Isocase is offline Isocase lives in United States 2009-07-01 #3 Old  
In your onLoadInit method you can just redraw your rectangle. You don't need to recreate it, just redraw the dimensions.
Reply With Quote  
creationmachine creationmachine is offline creationmachine lives in United States 2009-07-06 #4 Old  
The problem is; I have to set variables (OO callbacks) after onLoadInit runs. onLoadInit runs after everything else depending on the items being loaded. I am tinkering with onEnterFrame now to check if a variable has been loaded yet. this seems kind of clunky though. I'm almost there.
Reply With Quote  
creationmachine creationmachine is offline creationmachine lives in United States 2009-07-06 #5 Old  
It seems like a state machine would be useful for stuff like this.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: