A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Need some help with some loops I can't get my head around :/

  1. #1
    Member
    Join Date
    Mar 2006
    Posts
    41

    Need some help with some loops I can't get my head around :/

    Flash CS3, using AS2

    I need some serious help here :/

    I'm trying to recreate a UI for an application that my company makes in order to create an in-depth training companion to the application.

    One of the features is a weird tree view that works quite different from any tree view component I've ever seen, so I decided to create my own version of it as the Flash AS2 Tree Component doesn't have the functionality, and I haven't found any Tree Component for sale or download that works like that either.

    At first glance it seems like a standard Tree, except that it behaves differently if you click on the icon for each node than if you click on the label. Some labels are links, some nodes can have 2 different labels, each with their own link, and most nodes have a second label underneath the label next to the icon (these don't have events assigned to them; they're just a quick way to view information).

    So anyway, I set up an XML file with all the information that I need (the easy part), but populating the whole thing, making sure each subnode can properly exist within each parentnode while allowing me to open and close them has been a real challenge. Basically, I decided to use a whole bunch of loops to go through the nodes in the XML, but my lack of experience in using them is kicking my rear end.

    Basically, I need some code that can evaluate all the nodes in the XML and sort them within the proper hierarchies and then create all the clips in the stage to add the mouse events for opening, closing, and handling the links (which do not direct to the same event as when the icon next to that link is clicked).

    I have been hitting this problem from many angles, but I haven't been able to squash it yet. I'm including my horribly convoluted and not too pretty code to demonstrate my problems.

    (I'll post some of my code to demonstrate the mess I have in the next message)

    Help!

  2. #2
    Member
    Join Date
    Mar 2006
    Posts
    41
    The aforementioned code:
    Code:
    /****************************************************************************************************************
    /******************************************************************************************************************
    *******************************************VARIABLES AND IMPORTS***************************************************
    *****************************************************************************************************************/
    import mx.styles.CSSStyleDeclaration;
    import mx.controls.Label;
    import mx.controls.Loader;
    
    // The following variable exists to keep track of the vertical positioning of the Expandos
    var nLevelOneTopY:Number = 10;// Starting position of the first Expando; all other Expandos are placed according to this.
    var nShiftAmount:Number = 22;// Variable that tells me how many pixels down an Expando must move when the one atop it is clicked.
    var nShiftBelowOne:Number = 14;// Variable keeping track of how much to shift the Expandos down from the first one in pixels
    var nGNodeIteration:Number = 0;
    var nTest:Number = 0;
    var firstLabel:Array = [];
    var nTestArray:Array = [];
    ////////////XML OPERATION AND RELATED LOOPS///////////////
    var xmlExpandos:XML = new XML();
    xmlExpandos.ignoreWhite = true;
    xmlExpandos.load("xml/expandos.xml");
    xmlExpandos.onLoad = function():Void  {
    	var mlImageLoader:MovieClipLoader = new MovieClipLoader();
    	if (xmlExpandos.firstChild.hasChildNodes()) {
    		// I'm using firstChild to iterate through the childNodes of each Node.
    		for (var aNode:XMLNode = xmlExpandos.firstChild.firstChild; aNode != null; aNode = aNode.nextSibling) {
    			if (aNode.nodeType == 1) {
    				aNodeIndex = aNode.attributes.nodeIndex;
    				firstLabel.push(aNode.attributes.label);
    				// To begin with, I'm creating a few empty movieclips which will store the image and the label (or labels) per node
    				localRoot.createEmptyMovieClip(["mLevelOne" + aNodeIndex],localRoot.getNextHighestDepth());
    
    				localRoot["mLevelOne" + aNodeIndex].createEmptyMovieClip("mImage",localRoot["mLevelOne" + aNodeIndex].getNextHighestDepth());
    				mlImageLoader.loadClip(aNode.attributes.icon,localRoot["mLevelOne" + aNodeIndex].mImage);
    				localRoot["mLevelOne" + aNodeIndex].createEmptyMovieClip("tField",localRoot["mLevelOne" + aNodeIndex].getNextHighestDepth());
    				localRoot["mLevelOne" + aNodeIndex].tField.createClassObject(mx.controls.Label,"tLabel",1);// Avoiding a depth conflict
    				localRoot["mLevelOne" + aNodeIndex].tField.tLabel.setStyle("fontSize","12");
    				localRoot["mLevelOne" + aNodeIndex].tField.tLabel.setStyle("fontFamily","Arial, Helvetica");
    				localRoot["mLevelOne" + aNodeIndex].tField.tLabel.text = aNode.attributes.label;
    				localRoot["mLevelOne" + aNodeIndex].tField.tLabel._x = 22;
    				localRoot["mLevelOne" + aNodeIndex].tField.tLabel._y = 2;
    				localRoot["mLevelOne" + aNodeIndex].tField.tLabel._width = localRoot["mLevelOne" + aNodeIndex].tField.tLabel.text.length * 14;
    			}
    			// Does the current node represented by the iteration of the loop have any childNodes?
    			if (aNode.hasChildNodes()) {
    				for (bNode = aNode.firstChild; bNode != null; bNode = bNode.nextSibling) {
    					if (bNode.nodeType == 1) {
    						bNodeIndex = bNode.attributes.nodeIndex; // Workaround variable I created to help me identify the hierarchy of the current node
    						localRoot["mLevelOne" + aNodeIndex].createEmptyMovieClip(["mLevelTwo" + bNodeIndex],localRoot["mLevelOne" + aNodeIndex].getNextHighestDepth());
    						localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].createEmptyMovieClip("mImage",localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].getNextHighestDepth());
    						mlImageLoader.loadClip(bNode.attributes.icon,localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].mImage);
    						localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].createEmptyMovieClip("tField",localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].getNextHighestDepth());
    						localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].tField.createClassObject(mx.controls.Label,"tLabel",1);
    						localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].tField.tLabel.setStyle("fontSize","12");
    						localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].tField.tLabel.setStyle("fontFamily","Arial, Helvetica");
    						localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].tField.tLabel.text = bNode.attributes.label;
    						localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].tField.tLabel._x = 22;
    						localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].tField.tLabel._y = 2;
    						localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex]._x = 8;
    						localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].tField.tLabel._width = localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex].tField.tLabel.text.length * 14;
    						localRoot["mLevelOne" + aNodeIndex]["mLevelTwo" + bNodeIndex]._visible = false;
    
    					}
    }
    				}
    			}
    			// From what I gather, there are up to SEVEN levels that an Expando can open, thus I will need loops to handle at least 7 levels of Expandos. Oh joy. I'm naming each variable that holds a level levelOne, levelTwo, and so on. This is, keeping in mind that the actual XML file contains seven levels because of the parent node (<Expandos>), which I include in order to have a well-formed XML file. The loops need to evaluate if the current level has entries or not before proceeding. This will be fun.                                     
    /************************************************************************************************************
    *************************************************************************************************************
    This is where the code to control the whole Expandos deal should be. Placement, functions, transformations.
    It's kicking my ass...
    *************************************************************************************************************/
    			if (aNodeIndex == 0) {
    				localRoot["mLevelOne" + aNodeIndex]._y = 0;
    			} else {
    				localRoot["mLevelOne" + aNodeIndex]._y = localRoot["mLevelOne" + (aNodeIndex - 1)]._height + nLevelOneShift;
    				nLevelOneShift += nShiftAmount;
    				trace(nLevelOneShift);
    
    			}
    			nTestArray.push(nTest);
    			nTest++;
    		}
    	}
    	// Placement for the Top Expandos       
    	for (var aNode:XMLNode = xmlExpandos.firstChild.firstChild; aNode != null; aNode = aNode.nextSibling) {
    		trace(nTestArray + " the array nTest");// I got my test array working alright. Too bad I'm not experienced at all with them yet....
    		trace(localRoot["mLevelOne" + nTest].mImage + " testing if this is even recognized as existing");// returns undefined...
    		//nTest++;
    		aNodeIndex = aNode.attributes.nodeIndex;
    		trace(aNodeIndex + " aNode Index");
    		localRoot["mLevelOne" + aNodeIndex].onRelease = function():Void  {
    
    			trace(this);
    			trace(localRoot["mLevelOne" + aNodeIndex]._height + " height");
    			trace(localRoot["mLevelOne" + aNodeIndex].mImage);
    			//trace (aNode[nTestArray[aNodeIndex]].attributes.label);
    			trace([nTestArray[aNodeIndex]]);// Always return last index in array. Shouldn't this loop through them?
    			//localRoot["mLevelOne" + aNodeIndex].mImage.hitArea = localRoot["mLevelOne" + aNodeIndex].mImage;
    			//localRoot["mLevelOne" + aNodeIndex].mImage.onRelease = function():Void  {
    			//trace(this);
    			//};
    		};
    	}
    	var oMouseListener:Object = new Object();
    	oMouseListener.onMouseUp = function() {
    		trace(nShiftBelowOne);
    	};
    	Mouse.addListener(oMouseListener);
    	trace(firstLabel);
    };

  3. #3
    Member
    Join Date
    Mar 2006
    Posts
    41
    The XML
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <Expandos nShiftAmount = '24' nLevelTopOne = '10'>
    	<Recordatorios nodeIndex = '0' id = 'rec' icon = 'images/arrow_plus_med.gif' altIcon = 'arrow_minus.gif' label = 'Recordatorios' heading = 'RECORDATORIOS' desc = ' ' >
    		<MisRecordatorios nodeIndex = '0' id = 'recSub0' icon = 'images/arrow_plus_med.gif' altIcon = 'arrow_minus.gif' label = 'Mis Recordatorios' heading = 'MIS RECORDATORIOS'desc = '<p>Mis Recordatorios'>
    			<misrecordatorios nodeIndex = '0' id ='recSub0Sub0' label = 'Nombre de Caso (#) Fecha' heading = 'INFORMACIÓN' desc = ' ' >
    				<tareaenrecordatorio nodeIndex = '0'  id ='recSub0Sub0Sub0' label = 'Tarea (#) Fecha' heading = 'INFORMACIÓN' desc = ' ' />
    			</misrecordatorios>
    		</MisRecordatorios>
    		<RecordatoriosManuales nodeIndex = '1' id = 'recSub1' icon = 'images/arrow_plus_med.gif' altIcon = 'arrow_minus.gif' label = 'Recordatorios Manuales' heading = 'RECORDATORIOS MANUALES' desc = ' '>
    			<recordatoriomanual nodeIndex = '0' id = 'recSub1Sub0' label = 'Nombre de Caso (#) Fecha' heading = 'INFORMACIÓN' desc = ' ' />
    		</RecordatoriosManuales>
    		<RecordatoriosEscalados nodeIndex = '2' id = 'recSub2' icon = 'images/arrow_plus_med.gif' altIcon = 'arrow_minus.gif' label = 'Recordatorios Escalados' heading = 'RECORDATORIOS ESCALADOS' desc = ' '>
    			<recordatorioescalado nodeIndex = '0' id = 'recSub2Sub0' label = 'Nombre de Caso (#) Fecha' heading = 'INFORMACIÓN' desc = ' ' />
    		</RecordatoriosEscalados>
    	</Recordatorios>
    	<Casos nodeIndex = '1' id = 'cas' icon = 'images/arrow_plus_med.gif' altIcon = 'arrow_minus.gif' label = 'Casos' heading = 'CASOS' desc = ' '>
    		<NombreDeCaso nodeIndex = '0' id = 'casSub0' icon = 'images/case.gif' label = 'Nombre (Fecha de Caso) Acciones' heading = 'INFORMACIÓN' desc = ' '>
    			<trabajos nodeIndex = '0' id = 'casSub0Sub0' label = 'Trabajos' heading = 'TRABAJOS' desc = ' ' />
    		</NombreDeCaso>
    	</Casos>
    	<Proveedores nodeIndex = '2' id = 'pro' icon = 'images/arrow_plus_med.gif' altIcon = 'arrow_minus.gif' label = 'Proveedores' heading = 'PROVEEDORES' desc = ''>
    		<NombreProveedor nodeIndex = '0' id = 'proSub0' icon = 'images/provider.gif' label = 'Nombre del Proveedor (#) Acciones' heading = 'INFORMACIÓN' desc = ' ' >
    			<tareas nodeIndex = '0' id = 'proSub0Sub0' label = 'Tareas' heading = 'TAREAS' desc = ' ' />
    		</NombreProveedor>
    	</Proveedores>
    	<TSTSF nodeIndex = '3' id = 'tsf' icon = 'images/arrow_plus_med.gif' altIcon = 'arrow_minus.gif' label = 'TS/TSF' heading = 'TS/TSF' desc = '<p>Utilice este Expando para ver los Trabajadores Sociales que se le han asignado y los Casos y Proveedores asignados a sus Trabajadores Sociales.'>
    		<SupervisorTS nodeIndex = '0' id = 'tsfSub0' icon = 'images/worker2.gif' label = 'Nombre de Supervisor (#) Localización' heading = 'INFORMACIÓN' desc = ' ' >
    			<Trabajador nodeIndex = '0' id = 'tsfSub0Sub0' icon = 'images/worker2.gif' label = 'Nombre (#) Acciones' heading = 'INFORMACIÓN' desc = ' '>
    				<Proveedor nodeIndex = '0' id = 'tsfSub0Sub0Sub0' icon = 'images/provider.gif' label ='Asignaciones / Proveedores' heading = 'INFORMACIÓN' desc =' '>
    					<Asignaciones nodeIndex = '0' id ='tsfSub0Sub0Sub0Sub0' icon = 'images/assign.gif' label = 'Asignaciones'>
    						<subAsignaciones nodeIndex = '0' id = 'tsfSub0Sub0Sub0Sub0sub0' icon = 'images/assign.gif' label = 'Acosta, Evelyn' subLabel = 'Acciones' >
    							<entrada nodeIndex = '0' id = 'tsfSub0sub0sub0Sub0sub0sub0sub0' label = 'Entrada'                                         />
    						</subAsignaciones>
    					</Asignaciones>
    					<Basica nodeIndex = '1' id = 'tsfSub0Sub0Sub0Sub1' label = 'Básica'>
    					
    					</Basica>
    					<LicenciaCertificaciones nodeIndex = '2' id = 'tsfSub0Sub0Sub0Sub2' label = 'Licencia / Certificaciones' >
    					
    					</LicenciaCertificaciones>
    					<Miembros nodeIndex = '2' id = 'tsfSub0Sub0Sub0Sub3' label = 'Miembros' >
    					
    					</Miembros>
    					<AgenciaPrimaria nodeIndex = '3' id = 'tsfSub0Sub0Sub0Sub4' label = 'Agencia Primaria' >
    					
    					</AgenciaPrimaria>
    					<Servicios nodeIndex = '4' id = 'tsfSub0Sub0Sub0Sub5' label = 'Servicios' >
    					
    					</Servicios>
    				</Proveedor>
    			</Trabajador>
    		</SupervisorTS>
    	</TSTSF>
    	<Aprobaciones nodeIndex = '4' id ='apr' icon = 'images/arrow_plus_med.gif' altIcon = 'arrow_minus.gif' label = 'Aprobaciones' heading = 'APROBACIONES' desc = '<p>Utilice este Expando para ver y accesar todos los expedientes que se encuentren en el proceso de aprobación o que han sido aprobados en las últimas 48 horas.'>
    		<MisAprobaciones nodeIndex = '0' id  = 'aprsub0' icon = 'images/arrow_plus_med.gif' altIcon = 'arrow_minus.gif' label = 'Mis Aprobaciones' heading = 'MIS APROBACIONES' desc = ' '>
    			<node nodeIndex = '0' id = 'aprsub0sub0' label = 'No rows found' heading = 'INFORMACIÓN' desc = ' ' />
    		</MisAprobaciones>
    		<AprobacionesEnProgreso nodeIndex = '1' id = 'aprsub1' icon = 'images/arrow_plus_med.gif' altIcon = 'arrow_minus.gif' label = 'Aprobaciones en Progreso' heading = 'APROBACIONES EN PROGRESO' desc = ' '>
    			<node nodeIndex = '0' id = 'aprsub1sub0' label = 'No rows found' heading = 'INFORMACIÓN' desc = ' ' />
    		</AprobacionesEnProgreso>	
    		<HistorialDeAprobaciones nodeIndex = '2' id = 'aprsub2' icon = 'images/arrow_plus_med.gif' altIcon = 'arrow_minus.gif' label = 'Historial de Aprobaciones' heading = 'HISTORIAL DE APROBACIONES' desc = ' '>
    			<node nodeIndex = '0' id = 'aprsub2sub0' label = 'No rows found' heading = 'INFORMACIÓN' desc = '  ' />
    		</HistorialDeAprobaciones>
    	</Aprobaciones>
    	
    </Expandos>
    Last edited by ankhcomm; 03-04-2010 at 11:46 PM. Reason: Removing the information inside the desc tags

  4. #4
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    it will definitely make it easier for us to help if you upload your files
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  5. #5
    Member
    Join Date
    Mar 2006
    Posts
    41
    Well, those are my files since there are no entities in the stage, just those scripts in the main timeline.

    But anyway, I see your point, and I'm grateful that you're willing to look at it. Now, I'm not sure if it would be wise to upload them from my company's laptop so I will do so from mine when I'm off for the day.

  6. #6
    Member
    Join Date
    Mar 2006
    Posts
    41
    Here are the files.
    Attached Files Attached Files

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