Parapal Design Flash

A A A Text size

Actionscript source code and FLA files

Hangman XML source code and FLA. Learn how to load and read external XML data in Flash.

Create your own version of Hangman. Customise the colours, fonts and behaviour, or simply use it to learn some Actionscript. Download hangman fla and xml file.

Main timeline script

xmlCounter = 0;
clueWarning._visible = false;
wordsXML = new XML();
wordsXML.ignoreWhite = true;
wordsXML.load("hangman.txt");
wordsXML.onLoad = function(success) {
if (success) {
_root.exTitle = wordsXML.firstChild.attributes.exTitle;
setWord();
}
};
//set up variables to track if exercise is done, or student is dead
_root.correct = false;
_root.dead = false;
//hide "Start again" barrier/button
_root.barrier._visible = false;
_root.barrier.lbl = "Click here to continue";
_root.feedback._visible = false;
_root.feedback.lbl = "Word set completed";
//Hide text box that allows student to type in the answer
_root.typeAnswer._visible = false;
//set up array to track if letters have been used and to make them visible after a reset
_global.lettersArray = new Array();
//function to write correct letters in or add to the hangman graphic
function checkEx(selLet) {
//Only do this if the exercise is not finished and the student is not dead
if (_root.correct == false && _root.dead == false) {
letExists = false;
//variable to track if this single letter is correct
//Loop through each letter of the correct answer
for (i=0; i<firstWord.length; i++) {
tempLet = firstWord.charAt(i);
//check if student letter matches current letter
if (tempLet == selLet) {
letExists = true;
//add correct letter to feedback student sees
wordFeedbackTemp += selLet;
} else {
//add feedback student already has
wordFeedbackTemp += wordFeedback.charAt(i);
}
}
wordFeedback = wordFeedbackTemp;
wordFeedbackTemp = "";
if (letExists == false) {
//advance hangman graphic
hangMan.gotoAndStop(hangman._currentframe+1);
if (hangman._currentframe == 10) {
_root.clueBut._visible = false;
_root.clueLabel._visible = false;
}
}
//Check if finished
if (wordFeedback == firstWord) {
_root.xmlCounter++;
wordFeedback += " is correct";
//Make "start again" barrier visible
if (wordsXML.firstChild.childNodes.length>(xmlCounter)) {
_root.barrier._visible = true;
} else {
_root.feedback._visible = true;
}
_root.correct = true;
}
}
}
//function to start again
function reset() {
//reset variables and hide "start again" button
_root.barrier._visible = false;
_root.dead = false;
_root.correct = false;
//loo through letterArray switching letter buttons back on
for (i=0; i<25; i++) {
eval(_global.lettersArray[i])._visible = true;
eval(_global.lettersArray[i])._alpha = 100;
}
//start hangman graphic again
_root.hangMan.gotoAndStop(1);
_root.clueBut._visible = true;
_root.clueLabel._visible = true;
_root.wordFeedback = "";
setWord();
}
//function to set up new word
function setWord() {
firstWord = wordsXML.firstChild.childNodes[xmlCounter].attributes.word;
progress = "Word "+(xmlCounter+1)+" of "+wordsXML.firstChild.childNodes.length;
//Write a hyphen for each letter
for (i=0; i<firstWord.length; i++) {
wordFeedback += "-";
}
startHang = _root.hangMan._totalFrames-Number(wordsXML.firstChild.childNodes[xmlCounter].attributes.tries);
_root.hangman.gotoAndStop(startHang);
}
//Check if student has typed the correct answer
_root.typeAnswer.submitBut.onPress = function() {
if (_root.attempt == _root.firstWord) {
_root.xmlCounter++;
_root.wordFeedback = _root.firstword+" is correct";
_root.correct == true;
if (wordsXML.firstChild.childNodes.length>(xmlCounter)) {
_root.barrier._visible = true;
} else {
_root.feedback._visible = true;
}
} else {
_root.hangMan.gotoAndStop(_root.hangman._currentframe+1);
}
_root.typeAnswer._visible = false;
};
_root.typeAnswer.cancelBut.onPress = function() {
_root.typeAnswer._visible = false;
};
//start again button action
_root.barrier.onPress = function() {
_root.clue = "";
_root.reset();
};
_root.feedback.onPress = function() {
_root.feedback._visible = false;
_root.xmlCounter = 0;
_root.reset();
};
//show box for student to guess the answer
_root.allowTypeAnswer.onPress = function() {
if (_root.dead == false && _root.correct == false) {
_root.typeAnswer._visible = true;
_root.attempt = "";
}
};
_root.guessBut.onPress = function() {
if (_root.dead == false && _root.correct == false) {
_root.typeAnswer._visible = true;
_root.attempt = "";
}
};
_root.clueBut.onRollOver = function() {
_root.clueWarning._visible = true;
};
_root.clueBut.onRollOut = function() {
_root.clueWarning._visible = false;
};
_root.clueBut.onPress = function() {
_root.clue = wordsXML.firstChild.childNodes[xmlCounter].attributes.hint;
if (_root.hangman._currentframe+2>_root.hangman._totalframes) {
tempFrame = _root.hangman._totalframes;
} else {
tempFrame = _root.hangman._currentframe+2;
}
_root.hangman.gotoAndStop(tempFrame);
_root.clueBut._visible = false;
_root.clueLabel._visible = false;
_root.clueWarning._visible = false;
};

Code on alphabet buttons

onClipEvent (load) {
lbl = "a";
_global.lettersArray.push(_target);
}
on (press) {
_root.checkEx(lbl);
this._visible = false;
}
on(rollOver){
this._alpha = 50;
}
on(rollOut){
this._alpha = 100;
}

 

XML external data

<ex exTitle = "Testing Times">
<item word = "psychology" hint = "Freud thought he was good at this" tries = "4"/>
<item word = "technology" hint = "Dave Pullin is the TLL for this" tries = "6"/>
<item word = "media" hint = "John Harper works in this area" tries = "10"/>
<item word = "flash" hint = "This program was made with it" tries = "5"/>
</ex>