PDA

View Full Version : Text-based Adventure?


gav.cooper
10-22-2007, 10:04 PM
I’ve been putting together a text-based adventure game (http://en.wikipedia.org/wiki/Interactive_fiction) with the intent of using this compiling program to make the actual game... www.adrift.org.uk

As good as this program is for putting the game together, the problem is that the end format of the game file is a weird .TAF format and the user needs to download a player program to run the game through… a bit of a mess about I was thinking!

I’d prefer to use Flash because the end result can be made much more assessable to the final user.

Are there any text adventure tutorials out there?
I’ve googled it but can’t find anything… :confused:

Noct
10-23-2007, 02:27 PM
Heh, do people still play text based adventure games? I mean, I loved Zork as a kid, but that was because there were no options...

Anyways, I don't know of any tutorials, but the entire thing would be just checking an input text string against possible answers and then displaying the results. You could set it up a million different ways, and I would probably use XML for it personally, but this should get you started.

//Array of possible answers to type:
var p1_answerArray:Array = ["Look North", "Look South"];
//Responses to those answers
var p1_answerArray2:Array = ["To the north of you are trees.",
"To the south of you are trees."];
//Array of page texts
var p1_textArray:Array = ["You are standing in a clearing,
there are trees on all sides of you."];
//Send page text to dnynamic field
this.readText.text = p1_textArray[0];
//Check the input Field on button press
this.enterBtn.onRelease = checkText;
function checkText() {
//Grab text from input field
var iT:String = inputText.text;
var ansGiven:Number;
//Loop through possible answers
for (a=0; a<p1_answerArray.length; a++) {
//If the inout text matches an answer
if (iT.toUpperCase() == p1_answerArray[a].toUpperCase()) {
//Return its corresponding text from the array
readText.text += "\r"+p1_answerArray2[a];
var foundAnswer:Boolean = true;
}
}
//Else it didn;t find an answer, so return a message
if (!foundAnswer) {
readText.text += "\r"+"I do not understand "+iT;
readText.text += "\r"+"Your available commands are: "+"\r"+p1_answerArray;
}
foundAnswer = false;
}

gav.cooper
10-25-2007, 11:56 AM
thanks for that... but... it made no sense at all to me :confused:

also the .ZIP file didnt work, i'm using MX 2004 (still)

lets try to keep things simple, cos tbh i am - lol

here is what I want to do... I have created a flash document with an INPUT text field so the player can input a command into my game, I will then have a button that acts as an ENTER button which will commit the players input.

if the player inputs the wrong answer i want my Flash to respond with some generic "try again" answer, if the player inputs the correct answer i want my Flash to jump to a different frame, thus advancing through my game.

I'm guessing I need to create a variable but I have no idea where to start or how to write one (that will work)...

HELP...

Noct
10-25-2007, 02:00 PM
Well, I commented out each line of the script man, everything you're asking for is right in there. If you found it totally incomprehensible you might want to do some reading up on beginners actionscript before you get started with a project like this...
I'll try to break it down a bit:

This is the code on your ENTER button, I called it "enterBtn" in my script:
//Check the input Field on button press
this.enterBtn.onRelease = checkText;
That tells actionscript to run the function "checkText" when the enter button is clicked.
The checkText function then stores the contents of the input text field (I'm calling it "inputText" here) in a variable called "iT":

function checkText() {
//Grab text from input field
var iT:String = inputText.text;


I then loop through the possible correct answers they can pick to determine if what they typed is correct or not.
This is the array of answers they can type (you could put as many in here as you wanted):

//Array of possible answers to type:
var p1_answerArray:Array = ["Look North", "Look South"];

This is the responses that will be returned for those answers. They are kept in the same order as the answers so if they first answer was given, the first response here will be returned:

//Responses to those answers
var p1_answerArray2:Array = ["To the north of you are trees.",
"To the south of you are trees."];

My checkText function then loops through the answer array.
The loop checks each slot of the answerArray to see if that slot holds the same text as the user has input. You use "toUpperCase" on both the answer they gave and the answer in the array to ensure that it will match regardless of how they capatalized it when they typed it in.

//Loop through possible answers
for (a=0; a<p1_answerArray.length; a++) {
//If the input text matches an answer
if (iT.toUpperCase() == p1_answerArray[a].toUpperCase()) {


If the answer they typed matches an answer in the array, the loop tells the response array (answerArray2) to send the response it has in the same position to the other text field (readText) so the user can see the response. It then sets a boolean (true/false) variable so we know that they reponded with an answer that matched.

//Return its corresponding text from the array
readText.text += "\r"+p1_answerArray2[a];\
//Set variable to true
var foundAnswer:Boolean = true;

The script then checks that boolean to see if the user has a matching answer or not:
If a correct answer was not given, the "foundAnswer" variable will not be true.
This line says, if foundAnswer is not(!) true

//it didn't find an answer, so return a message
if (!foundAnswer) {

Do this:

//Send a message to the output text field (readText).
//Here I have it say it doesn't understand what they typed.
//The \r just adds a line break to the text
//The +iT adds the vriable we created from the input text to the msg
readText.text += "\r"+"I do not understand "+iT;
//Here I am displaying the answer array so they can see what the possible choices they have are:
readText.text += "\r"+"Your available commands are: "+"\r"+p1_answerArray;
}
//THen after this loop has finish, we set the variable back to false
//This ensures that the next time we run the function it will work again
foundAnswer = false;


I'll give you the simple version now with what you mentioned you wanted it to do, but this is hard coded for just one answer.
You would be better off modifying the full script I typed out as it would work for every screen of your game, you would just keep adding answers to the arrays.


//on Enter button click:
this.enterBtn.onRelease = function() {
//Store the input text in a variable:
var iT:String = inputText.text;
//Check that variable against an answer:
if (iT == "this is the correct answer") {
//Main timeline jumps to a frame:
this._parent.gotoAndPlay("frameName");
//else the answer was wrong:
} else {
//Display a message in another text field:
outPutText.text = "Wrong!";
}
};


I have also attached the file again in mx format, good luck!