# Input text into flash game



## El-Grosso (Oct 4, 2007)

I'm about to finish this school project, a game in flash, and I am kind of stuck. It's all about getting this code, and once you get it, you win. Now, personally, I think the code cracking thing:

http://www1.edexcel.org.uk/dida-0906-d202/links/code.swf (code is 123456789)

.. the project website have provided us with is rather pathetic, and my teacher has given me permission to make my own one.

And I have no idea how to. I know how to make text to be written in, but I don't understand the script needed. Like, I want one set of letters to lead to one frame, and *anything* other than those letters to lead to another frame. Someone from another forum provide me with:

if ( txtbox.text == "whatever" ) {
gotoAndStop( "frame" );
} else {
gotoAndStop( "other frame" );
}

.. but I don't understand how to get it to work. Something about where it says txtbox, the varaible name of the input text should have the same. I don't understand .. can someone please help me?

Thanks a lot. :up:


----------



## obidon (Aug 12, 2007)

Well if you don't know Actionscript, then you should start learning it. Its really not that hard to learn. And if you think the project that was provided by your school is pathetic, you should at least be proficient enough in that language to be able to create something better. Theres plenty of Actionscript tutorials on the internet for beginners. So search around on the net or ask your teacher (I'm assuming the class is programming in Actionscript) for some help. If you still can't understand after you've searched hard and long for tutorials (not posting in online forums), then PM me and I might be able to assist.

Good luck.


----------



## TheRobatron (Oct 25, 2007)

It's just a simple bit of Actionscript that you need. Create a text box and make its instance name to "txtbox". Then in the button, put this code:


```
on (release) {
if (_root.txtbox.text == "123456789") {
gotoAndStop(2);
} else {
gotoAndStop(3);
}
}
```
This is pretty much the same as the code you were given, but hopefully a bit clearer. On frame 2 you would put the winning frame and on frame 3 you would put the losing frame. On frame 1, you would need to put stop(); in the Actionscript of the frame itself, to stop it from just looping through. To stop people from easily cheating at it (by right clicking -> Play) you can put the whole thing inside a movieclip object or move frames 2 and 3 along 1, to frames 3 and 4, and put Actionscript on frame 2:

```
gotoAndStop(1);
```
I hope that helps. My code my be a little old (it's actionscript 2) and depending on which version of Flash you have, you may have to change the settings of the document. If it's Flash CS3 you will probably have to create an Actionscript 2.0 document.

Let me know if you have any problems.


----------



## Kale542 (Mar 16, 2008)

I need help too!

I'm making a game where I want it to skip 30 levels when you put in the code *A23F7B3H* I tried the things above me but they don't work! Please help! I've got flash 8 by the way, and it just goes to the "else" condition if you get the right answer!


----------



## TheRobatron (Oct 25, 2007)

Can you upload the file or post the code? It's quite likely that you've made a mistake in the statement somewhere. Did you use == and put quotes round it?


----------



## Kale542 (Mar 16, 2008)

Nevermind about that. For some reason, my flash game didn't wan't to work properly but I fixed the problem


----------



## Kale542 (Mar 16, 2008)

=)!


----------



## 1987 (Apr 5, 2008)

Hi, i was wondering if any one could help me with my project, its a flash game and i am strugling to get it to do what i want it 2 do, would apreciate if any 1 can help asap

many thanks


----------



## Kale542 (Mar 16, 2008)

hey 1987! what did you want your flash game to do then?


----------



## 1987 (Apr 5, 2008)

hi, i wanted to create a maths game page wich has sevral questions, i want the user to be able to insert the answer themselves but on the right answer. but i also want them to answer all the question and them telling them their score


----------



## 1987 (Apr 5, 2008)

any sugestion Kale542???

thnks


----------



## 1987 (Apr 5, 2008)

hi TheRobatron, could you possibly please help me?


----------



## TheRobatron (Oct 25, 2007)

Sure 

Are all the questions going to be on the same page and are they going to be randomly generated or set before? You'll need a two-dimensional array containing the question number and the corect answer. Then the answers the user inputs can be checked against these for each question (using a function) and correct or incorrect can be returned.

If you post a little more info on what you want your game to be, I can be more specific with the code.


----------



## 1987 (Apr 5, 2008)

i have to have 2 parts one wher there is a table and the user have to enter the correct answer in there so im guesing thats done using input text,

then i have the questions, i wanted to have them on the same page about a topic;
e.g 10 questions on algebra, and then on the next page have 10 questions about fraction.

but only at the end after answering all the question, it gives feedback on how many questions answered corect and how many incorrect.

e.g SCORE: fraction 9/10
algebra 7/10
total - 16/20

Hope this is i abit clearer

many thanks

1987


----------



## TheRobatron (Oct 25, 2007)

If the questions are preset and the answers are strings it is much easier to code . 
You will need to declare the answers to the questions:

```
var fractionAnswers:Array = new Array("3/4", "4/7", "4/342");
```
And then get the input from each text box:

```
var fractionUserInput:Array = new Array(_root.textbox1, _root.textbox2);
```
And then to get the score you need a for statement:

```
numberOfQuestions = 10;
for (x = 1; x < numberOfQuestions; x++) {
if (fractionAnswers[x]==fractionUserInput[x]) {
score = score + 1;
}
}
finalScore = score + "/" + numberOfQuestions
```
Then you can repeat that for each section.


----------



## 1987 (Apr 5, 2008)

hey thanks 4 that, i will try and create a sample to try it out and see if it works, all this is new for me. but thanks anyway rely apreciate it.


----------



## 1987 (Apr 5, 2008)

hey, this is the file let me no if it opens
ta


----------



## TheRobatron (Oct 25, 2007)

OK after a bit of fiddling I got it working 

What I did was take the variable attribute out of the text box and instead put the instance name as 'answer'. Then in the button code, I put if (_root.answer.text == "0.25") {. 

Hopefully that will work for you as well.


----------



## 1987 (Apr 5, 2008)

thanks ever so much for that,

i was wondering if there is any code wich can b put in for different question page so that for each question i dont need to put in another right and wrong page


----------



## 1987 (Apr 5, 2008)

i have created a page which has few empty boxes, they are input text, so that the users can insert the answer into it,. i have about 5 input text and in each box there should be a certain answer e.g
input text1=1
input text2=2
input text3=3

but i want it to give a score afta they submit the answer foe example state how many they got right or show the ones they got right and the one they didnt.

i have attached a doc file to give an example of what the page wil look like and so thats it better to understand.


----------



## TheRobatron (Oct 25, 2007)

That's easy to do. At the top of the next page you can put (scorebox being the text box)

```
_root.scorebox.text = "You scored " + score + " out of " + numberOfQuestions;
```


----------



## 1987 (Apr 5, 2008)

it says error on the script? am i puttin the code in a button or on a frame?


----------



## TheRobatron (Oct 25, 2007)

I tried putting it in the frame and that worked fine. This is the exact code I used to test it:

```
score = 3;
numberOfQuestions = 5;
_root.scorebox.text = "You scored " + score + " out of " + numberOfQuestions;
```
Then I made a text box with an instance name of scorebox.


----------



## 1987 (Apr 5, 2008)

stil no luck... 
shal i send u d file?


----------



## TheRobatron (Oct 25, 2007)

Yeah, I'll have a look at it.


----------



## 1987 (Apr 5, 2008)

this is d file, the white boxes are wher the user will enter their anser, each 1 should counts as 1point


----------



## TheRobatron (Oct 25, 2007)

I got it working 

I delared the variables as global (some people will say this is bad practice - it may be, but for small projects like this there is no problem with it). I also fixed the if statements. Here is the working file.


----------



## 1987 (Apr 5, 2008)

thanks genius, is it possible to change it around so that while they are playin and inserting the value, it shows how much they have score?

again thnk u very much!


----------



## TheRobatron (Oct 25, 2007)

No problem *blushes* 

So do you want the score displayed immediately after they enter the answer in the text box?


----------



## 1987 (Apr 5, 2008)

yeh kind of this is so that it can be seen how much is being scored and wher kind of thing,. this is an example of what i mean

its another part of the game.


----------



## TheRobatron (Oct 25, 2007)

To do this you need to add an event listener:

```
myListener1 = new Object();
myListener1.focusOut = function(evtObj) // makes object listen for when user leaves one of the text fields
{
if(_root.textbox1.text=="1" && !done1)
{
score = score + 1;
_root.scorebox.text = "You scored " + score + " out of " + numberOfQuestions;
done1 = true;
}
}
_root.textbox1.addEventListener("focusOut", myListener1); // adds listener to first textbox
```
You will need to do this for each text box. There is probably a quicker way of doing this, but I can't think of one. The 'done1' variable is a boolean and tells us if the answer is already correct. Each text box willl need its own and they all need to be false at the start (you could make an array to speed things up).


----------



## 1987 (Apr 5, 2008)

how do u mean put for each box, does the code go in the key frame or on that button


----------



## 1987 (Apr 5, 2008)

does any one no whether it is possible to make a game which has different score for different part of the game, and then at the end of the game i want it to show the score for each section and the total score meaning the total of the different score.
i.e
section a = 10/10
section b = 5/10
total = 15/20

thanks


----------



## TheRobatron (Oct 25, 2007)

The code needs to go in the frame - what I meant was you need to repeat it and change it for each text box. To make a different score for each section all you need to do is make a different variable for each section's score. Remember to make the variables global so you can access them from diferent frames.

At the end you can print each variable and then add them all together and make a total score:

```
totalScore = score1 + score2;
_root.txtbox1.text = "Section A: " + score1 + "/" + numberOfQuestions1;
_root.txtbox2.text = "Section B: " + score2 + "/" + numberOfQuestions2;
_root.txtbox3.text = "Total score: " + totalScore;
```


----------



## 1987 (Apr 5, 2008)

hi, im having problem with a button, iv put the right code and evrything but it doesnt go and stop on the page i want it to. the button is on frame 3 and i want it to go and stop on frame 4 but it doesnt, this is the code i have used.

on (release) {
gotoAndStop(4);
}


----------



## 1987 (Apr 5, 2008)

hi does anyone no how to create feedback form using php, i kind of got it wrkin, but the msg doesnt apear wen its received

hope some 1 can asist on that asap

thanks


----------



## TheRobatron (Oct 25, 2007)

Take a look here for a good tutorial on a feedback form.


----------



## 1987 (Apr 5, 2008)

hey dats site i looked at, it sends d msg n everything, but it doesnt appear on the email received.


----------



## TheRobatron (Oct 25, 2007)

So are you getting a message without any content?


----------



## 1987 (Apr 5, 2008)

yeh :S


----------



## TheRobatron (Oct 25, 2007)

Can you post the code for the page that you're using?


----------



## 1987 (Apr 5, 2008)

hey i managed to get it working, but now im having problem with the validation, it should appear if the user leaves any ieild empty, but instead its appears as soon as u go to that page? any sugestion on how to sort that out. 
i also need help in figuring out how to get the users name 2 display on a result page when they enter it

any help given as soon as possible wil be very much apreciated

thnk u
1987


----------



## TheRobatron (Oct 25, 2007)

To validate empty fields you can use an if statement and the empty() function:


```
if (empty($field1) || empty($field2) || empty($field3)) {
echo "Please fill in all the fields";
} else {
// succeed and send the data.
}
```
To display a user's name all you would do is echo the variable that you requested from the form onto the page:

```
$name = $_REQUEST['name'];
echo "Thank you, $name, for submitting the form.";
```


----------



## 1987 (Apr 5, 2008)

hey therobatron, thank you 4 ur reply, the php code u mention is the one i used but it wnt show it? dreamweaver alredy has validation function in it so feilds can be validated but the problem is i dont want the validation error 2 come up as soon as go on the page if u get me? :S


----------



## TheRobatron (Oct 25, 2007)

How is it deciding when to validate? Is it on the same page as the form or a different one - can you post the PHP code?


----------



## Kale542 (Mar 16, 2008)

TheRobatron said:


> There's no place like 127.0.0.1
> 
> Robatron, 127.0.0.1 is a loop back connection. I googled it and found out you got that off a t-shirt. Wow.


----------



## TheRobatron (Oct 25, 2007)

Kale542 said:


> Robatron, 127.0.0.1 is a loop back connection. I googled it and found out you got that off a t-shirt. Wow.


Indeed - 127.0.0.1 is used as an IP address to point to localhost, so it's a kind of pun. And no, I didn't get it off a t-shirt, but I'm sure you can get one with it on. I don't quite see the wow factor in it... but I'm glad you took the time to look it up...


----------

