//Steve Rhoades //Sample Code //Art Institute of Seattle //TextAdventure package{ import flash.display.*; //we've got import a couple of new things for this program import flash.text.*; import flash.events.*; /** *This class is meant as an example of the basics we've learned so far. With a couple of exceptions *All of the code in here should make some form of sense. In fact, you should be able to *modify this program to make a text adventure of your own choosing. */ public class TextAdventure extends MovieClip{ //currentText is going to be whatever text we are outputting to the user var currentText:String; //user input is whatever they are saying back to us var userInput:String = ""; //chapter will indicate what chapter of the story we are on. var chapter:uint = 1; //we'll be using this variable, a textfield, to talk to the player var output:TextField = new TextField(); const ERROR_TEXT:String = "I didn't catch that, please try again
"; public function TextAdventure(){ /** *This next section is a bit new, and we'll understand more of what's going on here in the long run *but for now: we're creating a mega string with HTML formatting, and then setting our output *text field to display that string. To change what's getting output, we'd merely change the lines *where currentText is getting modified. */ output.height = 400; output.width = 550; output.multiline = true; output.wordWrap = true; this.stage.addChild(output); currentText = "The Non Adventures of Herbert the Lazy

"; currentText +="One day, Herbert was walking to school. He hated walking. It was so much "; currentText +="more work than riding the bus. Then again, sleeping was so much nicer "; currentText +="than waking up, so he had missed the bus. Now he was walking.

"; currentText +="On his way to school, Herbert couldn't help but notice that Millie, his very "; currentText +="cute classmate also seemed to have missed the bus. She was walking to school "; currentText +="as well, and if Herbert hurried a bit, he could catch up with her, and walk "; currentText +="the rest of the way to school with her.

"; currentText +="What should Herbert do?

"; currentText +="1. Slow down, because he's getting tired.
"; currentText +="2. Call out to Millie, in hopes that she'll slow down, but not loud enough "; currentText +="that she is likely to hear.
"; currentText +="3. Feign injury, go home, and call in sick.
"; output.htmlText = currentText; /** *This next part isn't something we're ready to tackle completely yet, let's but suffice to say, this *is going to capture the user input. */ this.stage.addEventListener(KeyboardEvent.KEY_DOWN, getKeyPress); } /** *This is part of the user input process. You don't need to worry about it for now. */ public function getKeyPress(event:KeyboardEvent):void{ userInput = String.fromCharCode(event.charCode); //we won't talk about exactly what this next line does for a little bit //suffice to say, we are going to go down to our continueStory function below continueStory(); } /** *Here is where we will continue the story, based on user input */ public function continueStory():void{ //first, we need to figure out how far we've gone into the story if(chapter == 1){ //remember, user Input is a string, so we have to check for something in quotes! if(userInput == "1"){ //Herbert is getting tired, he needs to slow down currentText = "Forget her. She's already like... thirty feet ahead. "; currentText += "Instead, Herbert decided to slow down. He ended up late to class."; currentText += "As a result, he got detention. He didn't mind. In detention you "; currentText += "got to just sit there, for like an hour. The best part was they thought "; currentText += "detention was punishment!

"; currentText += "Game over. You win?"; chapter++; }else if(userInput == "2"){ //Herbert quietly calls out currentText = "'Millie? Millie?'

"; currentText += "Oh well, she didn't hear me. Herbert walked to school, making it "; currentText += "on time. However, he fell asleep in History class. He had a dream he was "; currentText += "back in the Roman Empire, as a senator. It was the Ides of March, and he "; currentText += "was supposed to go somewhere with Brutus, but he fell asleep instead. "; currentText += "He dreamed he was a cat in Egypt, and could sleep as much as he wanted. "; currentText += "This marked the fourth best dream within a dream that he had ever had. "; currentText += "

The end."; chapter++; }else if(userInput == "3"){ //Herbert feigns injury currentText = "Herbert fell over, clutching at his ankle. Unfortunately, it was the laziest "; currentText += "dive ever taken, and no one even noticed. He laid on the grass for a few "; currentText += "minutes, pretending to cry. When no one seemed to take notice or care, "; currentText += "Herbert decided to go to school after all. Absolutely nothing interesting "; currentText += "happened the rest of the day. Herbert was exceedingly happy.

"; currentText += "Game too boring to continue. You may or may not win, it's too boring to say."; chapter++; }else{//I don't know what key this is currentText += ERROR_TEXT; } output.htmlText = currentText; } } } }