Hey all -
I’m trying to make a word search puzzle which calls whatever.xml.
Here’s my code:
stop();
// do stuff here
//kill 'em all
for (var i=0; i<400; i++) {
this["tile"+i].removeMovieClip();
}
// array of vectors [dx, dy]
dirVect = new Array(8);
dirVect["N"] = [0, -1];
dirVect["NE"] = [1, -1];
dirVect["E"] = [1, 0];
dirVect["SE"] = [1, 1];
dirVect["S"] = [0, 1];
dirVect["SW"] = [-1, 1];
dirVect["W"] = [-1, 0];
dirVect["NW"] = [-1, -1];
//helpbox.gotoAndStop(1);
// The var xmlFile should be set if called with a query string e.g www.myServer.org/wordsearch.swf?xmlFile=ws-animals.xml
// Avoid URL encoding by using simple filenames :-)
// debug = "xmlFile:" + xmlFile
if (xmlFile == undefined) {
//debug = "xmlFile: default";
// otherwise this hard coded default is used:
xmlFile = "ws-default.xml";
// xmlFile = "non-existant.xml";
} else {
xmlFile = unescape(xmlFile);
}
loadWordSearch(xmlfile);
getWordList();
/*
if (!xmlLoaded) {
start_mc.gotoAndStop(1);
start_mc.theText = "No words loaded...";
// do this manually now.
} else {
start_mc.gotoAndStop(2);
start_mc.theText = title;
}
*/
function loadWordSearch(fileName) {
start_mc.theText = "Creating XML object...";
wordsearch_xml = new XML();
wordsearch_xml.ignoreWhite = true;
wordsearch_xml.onLoad = function(success) {
if (success) {
start_mc.theText = "Parsing " + fileName;
getWordList();
start_mc.gotoAndStop(32);
} else {
xmlLoaded = false;
start_mc.theText = "Failed to load " + fileName;
start_mc.gotoAndStop(32);
}
}
start_mc.theText = "Loading " + fileName;
wordsearch_xml.load(fileName);
}
function getWordList() {
// There should be an XML object wordsearch_xml already loaded, otherwise... who knows?
//
var allNodes = wordsearch_xml.firstChild.childNodes; // this is the <wordsearch> element
title = allNodes[0].firstChild; // the <title> element
var grid_xml = allNodes[1]; // This is the <grid> element
grid_w = grid_xml.attributes.width;
grid_h = grid_xml.attributes.height;
//trace("Width: " + grid_w + ", Height: " + grid_h);
//trace("Title: "+title.toString());
grid = new Array(grid_h);
// now populate the grid. grid[x,y] is char at (x,y)
for (var i=0; i<grid_h; i++) {
grid* = new Array(grid_w);
var myGridLine = grid_xml.childNodes*.firstChild.nodeValue; // content of <gridline>
for (var j=0; j<grid_w; j++) {
grid*[j] = myGridLine.charAt(j);
}
trace("grid[" + i + "] =" + grid*.join());
}
// now fill up the word list
var words_xml = allNodes[2]; // this is the <words> element
numWords = words_xml.childNodes.length;
words = new Array(numWords);
// an element words[0..numWords] is an object:
// .x = x-coordiante
// .y = y-coordinate
// .txt = full text ( e.g. "Brian's word")
// .t = normalised text ( e.g."BRIANSWORD")
// .dir = direction of word ( e.g. "SE")
// .colour = colour number
for (var i=0; i<numWords; i++) {
words* = new Object();
var wordElm_xml = words_xml.childNodes*; // the <word> element
words*.x = wordElm_xml.attributes.x;
words*.y = wordElm_xml.attributes.y;
words*.dir = wordElm_xml.attributes.dir;
words*.txt = wordElm_xml.firstChild.nodeValue; // the element's text
words*.t = normalise(words*.txt);
// code to make new colour; 27 sould be enough!
// tries to cover the space 0xd0d0d0 to 0x707070
// essentially convert i to ternary.
var red = 220 - 50*(Math.floor(i/9));
var green = 220 - 50*Math.floor((i % 9) / 3);
var blue = 220 - 50*(i % 3);
// now construct the colour
words*.colour = red*65536 + green*256 + blue;
// trace(words*.colour.toString(16));
}
xmlLoaded = true;
start_mc.theText = title;
}
function normalise(s) {
// strips non-alpha from s, and converts to upper case
// "Amy's £5 note" -> "AMYSNOTE"
var result = new String();
for (var i=0; i<s.length; i++) {
var theChar = s.charAt(i).toUpperCase();
if (theChar >= "A" && theChar <= "Z") {result += theChar;}
}
return result;
}
function reverseDirection(d) {
// replaces S<->N, W<->E in string d
var result = "";
for (var i=0; i<d.length; i++) {
var theChar = d.charAt(i);
switch (theChar) {
case "N" : result += "S"; break;
case "S" : result += "N"; break;
case "E" : result += "W"; break;
case "W" : result += "E"; break;
}
}
return result;
}
The XML file is called in the HTML using this line:
<script type="text/javascript">putFlash6(960, 420, 'dww_wordsearch.swf?xmlFile=ws-animals1.xml', 'ws', '#cccccc');</script>
I keep getting the following error:
“grid_mc loaded
populating grid…
NaN
Error opening URL ‘file:///C|/Users/AJcupcake/Desktop/Broadplay/Universal/Source%20Files/undefined’”
Everything exists! There are multiple XML files, one of which is being called by the script, and a ws-default.xml as well. Where am I going wrong?