package
{
//imports
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.Timer; //needed for the Timer
import flash.events.TimerEvent; //also needed for Timer
import flash.text.TextField;
import flash.media.Sound;
public class Poetry_Document extends MovieClip
{
// variables
var wordLoader:URLLoader;
var wordList:XML;
var nounCount:Number;
var verbCount:Number;
var adjCount:Number;
var advCount:Number;
var randNoun:Number;
var randVerb:Number;
var randAdj:Number;
var randAdv:Number;
var poem:String;
var totalSyls:Number;
var nounSyl:Number;
var verbSyl:Number;
var adjSyl:Number;
var advSyl:Number;
var MyTimer:Timer;
var attempts:Number;
var adjadv:Number;
//var bass:Sound
// constructor
function Poetry_Document()
{
this.wordLoader = new URLLoader();
this.wordLoader.addEventListener(Event.COMPLETE,onComplete,false,0,true);
this.wordLoader.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
this.wordLoader.load(new URLRequest("words.xml"));
//bass = new Sound(new URLRequest("mag.mp3"));
//this.bass.addEventListener(Event.COMPLETE,playSound);
}
// functions
function onComplete(event:Event):void
{
try
{
this.wordList = new XML(event.target.data);
this.wordLoader.removeEventListener(Event.COMPLETE,onComplete);
this.wordLoader.removeEventListener(IOErrorEvent.IO_ERROR,onIOError);
startPoem();
}
catch(error:Error)
{
MovieClip(root).haiku.text = "There was an error parsing the XML";
}
} // end onComplete
function onIOError(event:IOErrorEvent):void
{
MovieClip(root).haiku.text = "There was an error loading the XML";
} // end onIOError
///// BEGIN THE ACTION
function startPoem()
{
// get count of words
nounCount = this.wordList.nouns.noun.length();
verbCount = this.wordList.verbs.verb.length();
adjCount = this.wordList.adjects.adj.length();
advCount = this.wordList.adverbs.adv.length();
ShowHaiku();
// initiate the timer
MyTimer = new Timer(10000,0); // two seconds, forever
MyTimer.addEventListener(TimerEvent.TIMER, TimerExpired);
MyTimer.start();
} // end startPoem
function TimerExpired(event:TimerEvent):void
{
ShowHaiku();
} // end TimerExpired
function ShowHaiku()
{
MovieClip(root).haiku.text = poemLine(5) + "\n";
MovieClip(root).haiku.text += poemLine(7) + "\n";
MovieClip(root).haiku.text += poemLine(5) + "\n";
}
function ShowLimerick()
{
MovieClip(root).haiku.text = poemLine(9) + "\n";
MovieClip(root).haiku.text += poemLine(9) + "\n";
MovieClip(root).haiku.text += poemLine(5) + "\n";
MovieClip(root).haiku.text += poemLine(5) + "\n";
MovieClip(root).haiku.text += poemLine(9) + "\n";
}
function poemLine(s)
{
totalSyls = 0;
nounSyl = 0;
verbSyl = 0;
adjSyl = 0;
advSyl = 0;
// pick random words
randNoun = Math.floor(Math.random() * nounCount);
randVerb = Math.floor(Math.random() * verbCount);
randAdj = Math.floor(Math.random() * adjCount);
randAdv = Math.floor(Math.random() * advCount);
totalSyls += Number(wordList.nouns.noun[randNoun].syl);
poem = wordList.nouns.noun[randNoun].word;
if (totalSyls < s)
{
poem += " " + wordList.verbs.verb[randVerb].word;
totalSyls += Number(wordList.verbs.verb[randVerb].syl);
}
adjadv = Math.random();
if (adjadv > .5) // pick the adjective first
{
if (totalSyls < s)
{
poem = wordList.adjects.adj[randAdj].word + " " + poem;
totalSyls += Number(wordList.adjects.adj[randAdj].syl);
}
if (totalSyls < s)
{
poem += " " + wordList.adverbs.adv[randAdv].word;
totalSyls += Number(wordList.adverbs.adv[randAdv].syl);
}
}
else // pick the adverb first
{
if (totalSyls < s)
{
poem += " " + wordList.adverbs.adv[randAdv].word;
totalSyls += Number(wordList.adverbs.adv[randAdv].syl);
}
if (totalSyls < s)
{
poem = wordList.adjects.adj[randAdj].word + " " + poem;
totalSyls += Number(wordList.adjects.adj[randAdj].syl);
}
}
//// WHOO HOO My first successful recursive function EVER!
if (totalSyls != s)
{
attempts++;
poem = poemLine(s);
}
else
{
return poem;
}
} // poemLine
}
} // end package