A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Duplicate movie based on array data

  1. #1
    Member
    Join Date
    Sep 2001
    Posts
    72

    Duplicate movie based on array data

    Hello flash enthusiasts,

    I am currently building a (mock up) to view subscriber information by duplicating a movie clip based on an array. I'm grabbing the data from an xml and trying to pass the array to duplicated clips. The problem I'm running into is that I can only get data to the first duplicated movie and the original remains blank.

    Here is the xml:
    Code:
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <content>	
    	<article>
    		<last4id>1111</last4id>
    		<fnameid>fname1</fnameid>
    		<lnameid>lname1</lnameid>
    		<expdate>11/11</expdate>
    		<addressid>1111 First St</addressid>
    		<cityid>Los Angeles</cityid>
    		<stateid>CA1</stateid>
    		<zipid>11111</zipid>		
    	</article>
    	<article>
    		<last4id>2222</last4id>
    		<fnameid>fname2</fnameid>
    		<lnameid>lname2</lnameid>
    		<expdate>22/22</expdate>
    		<addressid>2222 Second St</addressid>
    		<cityid>San Francisco</cityid>
    		<stateid>CA2</stateid>
    		<zipid>22222</zipid>		
    	</article>
    </content>
    Here is how I'm loading the xml:
    Code:
    function loadXML(loaded) {
    	if (loaded) {
    		xmlNode = this.firstChild;
    		last4id = [];
    		fnameid = [];
    		lnameid = [];
    		expdate = [];
    		addressid = [];
    		cityid = [];
    		stateid = [];
    		zipid = [];
    		total = xmlNode.childNodes.length;
    		for (i=0; i<total; i++) {
    			last4id[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
    			fnameid[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
    			lnameid[i] = xmlNode.childNodes[i].childNodes[2].firstChild.nodeValue;			
    			expdate[i] = xmlNode.childNodes[i].childNodes[3].firstChild.nodeValue;
    			addressid[i] = xmlNode.childNodes[i].childNodes[4].firstChild.nodeValue;
    			cityid[i] = xmlNode.childNodes[i].childNodes[5].firstChild.nodeValue;
    			stateid[i] = xmlNode.childNodes[i].childNodes[6].firstChild.nodeValue;
    			zipid[i] = xmlNode.childNodes[i].childNodes[7].firstChild.nodeValue;
    		}
    		nextFrame();
    	} else {
    		trace("Can not load edit card xml");
    	}
    }
    xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    xmlData.load("edittest.xml");
    And on the next frame I'm running a for loop:
    Code:
    for (i=1; i<total; ++i) {
    	item_mc.info_mc.duplicateMovieClip("info_mc"+i, i);
    	item_mc["info_mc"+i]._y = item_mc["info_mc"+i]._height+15;
    	item_mc["info_mc"+i].fname.text = fnameid[i];
    	item_mc["info_mc"+i].lname.text = lnameid[i];
    	item_mc["info_mc"+i].cdigits.text = last4id[i];
    	item_mc["info_mc"+i].cdate.text = expdate[i];
    	item_mc["info_mc"+i].caddress.text = addressid[i];
    	item_mc["info_mc"+i].ccity.text = cityid[i];
    	item_mc["info_mc"+i].cstate.text = stateid[i];
    	item_mc["info_mc"+i].czip.text = zipid[i];
    }
    I'm wondering if I'm missing an initial load step or that I'm not splitting the array correctly.

    Any and all help is greatly appreciated and thank you for taking a moment to look at this post.

    Thanks,
    Kumba

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Please post your FLA File
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Member
    Join Date
    Sep 2001
    Posts
    72
    Here is the fla and the xml.

    Also, I noticed I had the for loop set wrong and changed it back to this: for (i=0; i<total; ++i).

    Thanks for taking a minute to look at this Nig 13!
    Attached Files Attached Files

  4. #4
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Hahahaha, the problem wasn't your coding (as it looked perfectly fine), it was just the positioning, lol. You had them both above each other

    Change this line:

    Actionscript Code:
    item_mc["info_mc"+i]._y = item_mc["info_mc"+i]._height+15;

    to this:

    Code:
    item_mc["info_mc"+i]._y = item_mc["info_mc"+(i-1)]._height+15;
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  5. #5
    Member
    Join Date
    Sep 2001
    Posts
    72
    Hi again Nig 13,

    Fantastic!.....except it only works for one duplicated move. The xml will be dynamically updated with nodes so there may be as many as four or five movies I need to duplicate. Add this to the existing xml tree and you will see what I mean:

    Code:
    <article>
    		<last4id>3333</last4id>
    		<fnameid>fname3</fnameid>
    		<lnameid>lname3</lnameid>
    		<expdate>33/33</expdate>
    		<addressid>3333 Third St</addressid>
    		<cityid>Pheonix</cityid>
    		<stateid>AZ</stateid>
    		<zipid>33333</zipid>		
    	</article>
    Is there a workaround for this?

    Thanks again!

  6. #6
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Oh, the problem is that it only calculates the first mc's height all the time, making all the duplicates stay in the same position, and I didn't realize that until now

    Declare these variables before the for loop:

    Actionscript Code:
    mcHeight = item_mc.info_mc._height;
    mcY = item_mc.info_mc._y;

    and then change this line the for loop (2nd line in loop):

    Code:
    item_mc["info_mc"+i]._y = item_mc["info_mc"+i]._height+15;
    to this:

    Actionscript Code:
    if(i == 0){
            item_mc["info_mc"+i]._y = mcY;
        } else {
            item_mc["info_mc"+i]._y = (item_mc["info_mc"+(i-1)]._height+15)*i;
        }

    You're probably wondering how that worked. Well, since (i-1) calculated the position of the previous added movieclip and added 15 to it, it actually didn't work for the first duplicated movieclip, because if the first one is named info_mc0, then there's no mc with the instance name, info_mc-1, hence the first duplicated movieclip's Y position becomes NaN or undefined and is positioned on the original movieclip's Y position, and then the next duplicated movieclip is offset from the first duplicate movieclip's position and so are the rest as well. To solve this, I declared 2 variables in the beginning to store the original mc's Height and Y position, and then in the for loop checked if variable i in the for loop is equals to 0, and if it is, it means that it's the first loop in the whole for loop, and then I set the first duplicated movieclip's Y position equals to the original one which I saved in a variable. For the rest of the duplicated movieclips after the first one, I set their Y position equals to the previous duplicated movieclip's Height+15 multiplied with variable i's value, so let's say the height of the movieclip is 80, which is the space you want between each movieclip, then for the second duplicated movieclip, its Y position becomes 80*1, for the next one, 80*2, 80*3, and so on, we could actually just drop (item_mc["info_mc"+(i-1)]._height+15) and replace it with mcHeight variable's value, since all of the duplicated movieclips heights are the same

    Hope this helps, despite the messy explanation xD
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  7. #7
    Member
    Join Date
    Sep 2001
    Posts
    72
    Sweet!!!!! You made my day! It works perfectly

    Thanks soooooooo much for your help and especially for the explanation...I'm on a lifelong journey to get my mind from 'street smarts' to analytical!

    Take care Nig 13!

  8. #8
    Member
    Join Date
    Sep 2001
    Posts
    72

    One more quick question...

    So now that I have the clips duplicated, how would I access individual instances of buttons within each clip?

    So if I have a button called "edit_btn" in each duplicate. I can access the object path with an onRelease (in the 'for' loop) using a trace which returns:
    Code:
    _level0.content_mc.billing.item_mc.info_mc0.edit_btn
    I thought of converting the object path to a string and using "charAt" but wasn't too successful.

    What I'm trying to do is get the duplicate movie number (info_mc0) so I can use it to reference the xml array that needs to be edited.

    Make sense?

    LOLOLOL! I feel like I'm explaining this in a "loop"

  9. #9
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    READ THIS FIRST

    Make a function with onRelease handler for a button, and then call that function in the loop, example:

    Actionscript Code:
    function btnClick(btn){
        btn.onRelease = function(){
            trace(btn);
        }
    }

    for(i=0;i<4;i++){
        btnClick(_root["mc"+i]);
    }

    This will work, but the below won't:

    Code:
    for(i=0;i<4;i++){
    	_root["mc"+i].onRelease = function(){
    		trace(_root["mc"+i]);
    	}
    }
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  10. #10
    Member
    Join Date
    Sep 2001
    Posts
    72
    I'm able to get the object path returned (via trace) but the problem I have is how do I convert it to a string or variable?

    What I initially thought was I could grab the mc number (["mc"+i]) via the object path and use "charAt" to get the individual number...but converting the object path to a string isn't working.

    Any suggestions?

  11. #11
    Member
    Join Date
    Sep 2001
    Posts
    72
    Thanks anyway but I figured out a way to do it:

    Code:
    function editClick(edit_btn){
        edit_btn.onRelease = function(){
    		var editnum:String = targetPath(edit_btn);
    		var actnum:String = editnum.charAt(42);		
        }
    }
    Thanks for all your help, you are a tremendous asset to us non-coders!

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