April 27, 2004
Hall allihopa!
Har postat denna fraga pa ett flertal forum utan framgang sa jag provar har ocksa.
Jag haller pa med en liten simulator for en MP3 spelare (man ska kunna klicka sig runt i menyerna). Koden nedan fungerar, men den ar inte elegant pa nagot satt...
Det jag skulle vilja ha lite hjalp med ar den stora klumpen med if/if else-satser som skapar en sokvag i XML-filen som innehaller datan for menyn.
Alltsa: Jag skulle vilja ersatta den stora raddan if-satser mot nagonting intelligentare. Den ska ga oandligt genom att den skapar sokvagen till en del av XML-filen beroende pa langen av 'currentLevel' och helt enkelt lagger till childNodes[currentLevel[x]]... Jag vill inte behova hardkoda sokvagarna som jag gjort nu utan nagonting som skapar sokvagarna och lagrar densamma i en behallare som min kod kan lasa. (:XMLNode typ)...
Nagra idéer
Detta ar koden pa huvudtidslinjen. Det finns aven en lite kodsnutt for att formatera xmlOutPut med CSS och sjalva XML-filen forstas.
stop();
// Create tracker array for index No's in the XML file:
var currentLevel:Array = new Array();
// Create tracker for the NAMES clicked in the XML tree:
var branchNames:Array = new Array();
// Create new XML object:
var hdd120Data:XML = new XML();
// Set the ignoreWhite property to true (default value is false)
hdd120Data.ignoreWhite = true;
// Test to see if XML data loads properly:
hdd120Data.onLoad = function(success)
{
// Clear textField for new input:
_root.xmlOutPut.htmlText = " ";
// We know (by looking at the XML file that node 0 is <root> and node 0 of <root> is <root_menu ="ROOT MENU"> so
// we can hard code the currentLevel tracker that information right away:
_root.currentLevel.push(0);
// Run menuClick for the root menu!
menuClick("ROOT MENU");
};
// Load XML data:
hdd120Data.load("hdd120.xml");
// Main click handler for menus!
function menuClick (branchName)
{
trace ("MENUCLICK RECIEVES:");
trace ("BranchName= " + branchName);
trace ("BranchNames = " + branchNames);
trace ("CurrentLevel = " + currentLevel.length);
trace ("Current path: " + currentLevel);
trace ("---------------------------------");
// Define a level for each path to take (The code I want replaced):
if (_root.currentLevel.length == 1)
{
var path:XMLNode = hdd120Data.firstChild;
trace ("REF: hdd120Data.firstChild");
}else if (_root.currentLevel.length == 2)
{
var path:XMLNode = hdd120Data.firstChild.firstChild;
trace ("REF: hdd120Data.firstChild.firstChild");
} else if (_root.currentLevel.length == 3)
{
var path:XMLNode = hdd120Data.firstChild.firstChild.childNodes[currentLevel[2]];
trace ("REF: hdd120Data.firstChild.firstChild.childNodes[currentLevel[2]]");
} else if (_root.currentLevel.length == 4)
{
var path:XMLNode = hdd120Data.firstChild.firstChild.childNodes[currentLevel[2]].childNodes[currentLevel[3]];
trace ("REF: hdd120Data.firstChild.firstChild.childNodes[currentLevel[2]].childNodes[currentLevel[3]]");
} else if (_root.currentLevel.length == 5)
{
var path:XMLNode = hdd120Data.firstChild.firstChild.childNodes[currentLevel[2]].childNodes[currentLevel[3]].childNodes[currentLevel[4]];
trace ("REF: hdd120Data.firstChild.firstChild.childNodes[currentLevel[2]].childNodes[currentLevel[3]].childNodes[currentLevel[4]]");
}
// End code I want replaced. The code beneath is the code that actually uses the above specified path to loop through nodes.
trace ("Level " + _root.currentLevel.length + " in the XML tree! " + "(" + branchName + ")");
// Loop through the childNodes of selected branch:
for (var i = 0; i < path.childNodes.length; i++)
{
// If any of the nodes in the the selected branch is branchName loop through the nodes of that branch:
if (path.childNodes[i].attributes.name == branchName)
{
// Clear textField for new list of options:
_root.xmlOutPut.htmlText = " ";
// Loop through the nodes in branch extracted above and show names of those:
for (var j = 0; j < path.childNodes[i].childNodes.length; j++)
{
// Index number for the current branch to push to the tracker array:
var branchID:Number = i;
// Create a temporary string for the output so we don't have to write this line several times:
var tempOutPut:String = path.childNodes[i].childNodes[j].attributes.name;
// Display current level in the xmlOutPut textField:
_root.xmlOutPut.htmlText += "<a href="asfunction:menuClick," + tempOutPut + "">" + tempOutPut + "</a>
";
}
}
}
// Update tracker array:
_root.currentLevel.push(branchID);
// Add the latest branch name to the array:
_root.branchNames.push(branchName);
trace ("MENUCLICK EXITS:");
trace ("BranchName= " + branchName);
trace ("BranchNames = " + branchNames);
trace ("CurrentLevel = " + currentLevel.length);
trace ("Current path: " + currentLevel);
trace ("---------------------------------");
}
Har ni nagra fler fragor sa ar det bara att fraga...
[EDIT] PROBLEM LOST!
1 Guest(s)