A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Getting the width of a MovieClip after new image was load on it

  1. #1
    Member
    Join Date
    Feb 2008
    Posts
    34

    [Solved] Getting the width of a MovieClip after new image was load on it

    Hi

    I'm doing an image gallery from xml file, and the thumbnails have diferent sizes. Around the thumbnail there is a border, both are diferent movie clips (thumb and border). they reside inside a main movieclip wich is being duplicated.

    So I need to resize the border according the new size of the thumb movieclip, but flash actually gets the value from the starting value of the thumb, not the size after the file was load on to it.

    I just thought flash would read the new value if i set the var for the width after the load movie, but that ain't happening =(.

    My code so far:

    Code:
    function createButton(newObj, thumbNode, bigImageNode){
    	 duplicateMovieClip(thumb_base, newObj, depthCount++);
    	 var tempName = eval(newObj);
    	 tempName._y = 0;
    	 loadMovie (thumbNode, tempName.thumb);
    	 var objBorder = tempName.border;
    	 var thumbWidth = tempName.thumb._width;
    	 objectSize (objBorder, thumbWidth, objHeight);
    	_root.txt1.text = thumbNode;
    	_root.txt2.text = bigImageNode;
    }
    
    function objectSize (object, objWidth, objHeight){
    	object._width = objWidth;
    	_root.txt3.text = objWidth;
    	
    	}
    Last edited by vbkun; 08-23-2010 at 03:02 AM. Reason: Solved

  2. #2
    Member
    Join Date
    Feb 2008
    Posts
    34
    ok I've played a bit with the debug tool and i learnt this bit might be important too, i'm calling the createButton function inside a for loop.

    Code:
    		for (i=0; i<totalNodes; i++){
    			var bigImageNode = imageNode.firstChild.firstChild;
    			var thumbNode = imageNode.childNodes[1].firstChild;
    			createButton("botao" + i, thumbNode, bigImageNode);
    			var imageNode = imageNode.nextSibling;
    			}
    So looking at debug i could see that flash performs the whole for loop and then "updates" stuff. I think this is probably so that it won't stuck in the loadMovie waiting it completion to move forward on the loop.

    Is this a case for the MovieClipLoader class? would setting an custom function for resize after the load is complet work? ~~~~~~~~~~

  3. #3
    Member
    Join Date
    Feb 2008
    Posts
    34
    Ok, so I tried the movieClipLoader class, and it's working great, locally, but when it loads from server it becomes just a total mess =(.

    I tried cleanin' up my code, cause i messed around a lot trying to fix it, before i would post it here:

    Code:
    stop();
    var imagesXML = new XML();
    var depthCount = 1;
    var position_y = 0;
    var prevObjHeight = 0;
    imagesXML.ignoreWhite = true;
    imagesXML.load("update/xml/images.xml");
    imagesXML.onLoad = checkLoading;
    
    
    function checkLoading(success) {
    	if (success == true) {
    
    		var rootNode = imagesXML.firstChild;
    		var totalNodes = rootNode.childNodes.length;
    		var imageNode = rootNode.firstChild;
    		relativePath = 'update/';
    		var firstImage = imageNode.firstChild.firstChild;
    		var firstLoad = relativePath+firstImage;
    		for (i=0; i<totalNodes; i++){
    			var bigImageNode = imageNode.firstChild.firstChild;
    			var thumbNode = imageNode.childNodes[1].firstChild;
    			createButton( "botao" + i, thumbNode, bigImageNode, i);
    			var imageNode = imageNode.nextSibling;
    			}
    		}
    	}
    
    function createButton( newObj, thumbNode, bigImageNode, i){
    	 duplicateMovieClip(thumb_base, newObj, depthCount++);
    	 var objName = eval(newObj);
    	 relativePath = 'update/';
    	 thumbToLoad = relativePath+thumbNode;
    	 listener = new Object();
    	 loader = new MovieClipLoader();
    	 loader.addListener(listener);
    	 loader.loadClip(thumbToLoad,objName.thumb);
    	 listener.onLoadInit = function (target) {
    	 var objWidth = objName.thumb._width;
    	 var objHeight = objName.thumb._height;
    	 var prevObjName = "botao" + (i-1); 
    	 prevObj = eval (prevObjName);
    	 var prevObjHeight = prevObj.thumb._height;
    	 objectSize (objName.border, objName, objWidth, objHeight, prevObjHeight );
    	}
    }
    
    function objectSize (object, objpos, objWidth, objHeight, prevObjHeight ){
    	object._width = objWidth + 9;
    	object._height = objHeight + 9;
    	objpos._x = -objWidth/2;
    	position_y = position_y + prevObjHeight+ 9 + 10;
    	objpos._y = position_y;
    	botao0._y = 0;
    	}
    My hunch is that i probably can't really have a listener and a loader produced in each turn at the for loop like that. So when loading stuff the listeners just get all messed.

    I sorta have no clue of how to make the loaders/listeners have different names, i tried doing something similar to what I've done on the "newObj" entry but i guessed I only know how to attribute values to variables in a function. =(

    If anyone can give me a tip, an advice, anything that can point me a way to solve this i'm very thankful in forward.

  4. #4
    Member
    Join Date
    Feb 2008
    Posts
    34
    Solved!


    The problem wasn't having one, or 10, or no listener. The problem was that the position Y for the duplicated objects depends on loaded image. As the load of the thumbs was running parallel, botao3 wasn't necessarily done before botao4 and so on.

    So I solved by setting a initial thumb to load, then the onLoadInit for it have a custom function that holds another loader, so it just "loops" from load to onloadinit to load to ... and so on. Load one image just after the previous is done, having all the data done for positioning the next thumb =).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center