1 2 Previous Next 48 Replies Latest reply on Mar 5, 2010 9:46 AM by kglad

# Increasing the number inside a text box...

Hey all!

I am currently trying to set up a mini-test inside of a project (Action Scipt 2) I'm creating and could use some help.

Basically, it's a multiple-choice test/quiz that adds 1 point to a text box (that will have the number 0 in it to start) if a wrong answer is selected.

Example:

Question 1                              Total: 0

So say answer 3 is the right answer, when clicked it'll go to the next frame/question. But if a,b or d is clicked, I need it to show some sort of 'Incorrect' meesage (which will be a text box), show the 'NEXT' button (which isn't visable/clickable until an answer is selected) to go to the next frame/question and add 1 to the 'Total' box, all at the same time.

There will be a total of 3 rounds of 20 questions (60 questions total) and at the end of 3 rounds, if the user ends up with say 20 in the 'Total' box, 5 points will automatically be subtracted from it leaving them with a total of 15 (the 5 points subtracted is a sort of bonus at the end). So on frame 60 for example (the last frame), there would be code telling the 'Total' box to subtract 5 from the ending number. If the total is higher than 0, it'll go to a certain frame and if the total is 0, it'll go to a different frame.

Does that make sense? Any ideas? Am I making it too difficult..is there an easier way?

• ###### 1. Re: Increasing the number inside a text box...

with what part of that do you need help?

• ###### 2. Re: Increasing the number inside a text box...

I need help with the code that would go on the answer(s) button, in this example, 'Answer 3', that would add 1 to the 'Total' box.

Then I would need to know how to subract 5 from the remaining 'Total' at the end of the 60-question quiz.

• ###### 3. Re: Increasing the number inside a text box...

use a numeric variable to store the user's score.  assign your textfield's text property to be that variables value each time the variable is updated.

• ###### 4. Re: Increasing the number inside a text box...

I have no idea how to do that...

• ###### 5. Re: Increasing the number inside a text box...

var scoreVar:Number = 0;  // initial scoreVar

scoreVar++;  // increment scroeVar because this is correct answer.

}

function scoreTF(n:Number){

yourtextfield.text=n;  // assigns scoreVar's value to yourtextfield's text property

}

• ###### 6. Re: Increasing the number inside a text box...

Ok, so my text box where the points are being added has the variable name 'total', and the 3 buttons have instance names of 'answer1', 'answer2' and 'answer3'.

On my first question 'answer2' is the correct answer, if they click it, it takes them to the next question cause it's right. However, 'answer1, and 'answer3' are the incorrect answers. When they are clicked they go to the frame where the user will see the 'INCORRECT', no problem, but the 'total' text box (pre-populated with a 0 and is a dynamic text box) is staying the same, not adding 1 to it.

Here's how the code looks on the 'answer1' button:

on (release) {
var scoreVar:Number = 0;

scoreTF(scoreVar);

scoreVar++;

scoreTF(scoreVar);

}

function scoreTF(n:Number){

total.text=n;

}

}

on (release) {
gotoAndStop(13);

}

• ###### 7. Re: Increasing the number inside a text box...

you should remove the textfield's associated variable and assign your textfield an instance name.

• ###### 8. Re: Increasing the number inside a text box...

Ok, I changed it to an instance name instead of variable but it didn't change anything so I removed the pre-populated 0 and left it blank, then when I clicked on the 'answer1' button and it entered a 0 into the 'total' text field. Tried the same code and stuff on question 2 but the field still stayed at 0.

• ###### 9. Re: Increasing the number inside a text box...

what's the textfield's instance name and does it match the name in scoreTF()?

• ###### 10. Re: Increasing the number inside a text box...

Hey,

Sorry, don't know if it was just me but I think the forum were down for a little while so here I am now, LOL!

Anyways...

The instance name of my text field is 'total' and this is how the code on the wrong answer buttons looks:

on (release) {
var scoreVar:Number = 1;

scoreTF(scoreVar);

scoreVar++;

scoreTF(scoreVar);

}

function scoreTF(n:Number){

total.text=n;

}

}

on (release) {
gotoAndStop(13);

}

Where's my mistake??

• ###### 11. Re: Increasing the number inside a text box...

1.  don't attach code to buttons/movieclips.  attach code to timelines, if you're going to add code in a fla.

2.  to do 1., you need to assign names to your buttons/movieclip buttons.  so, if you have a button named answer1 that should increase the user's score, use:

scoreVar++;

scoreTF(scoreVar);

}

function scoreTF(n:Number){

total.text=n;

}

• ###### 12. Re: Increasing the number inside a text box...

Once I pressed the button, the textfield changed from '0' to 'NaN'.

• ###### 13. Re: Increasing the number inside a text box...

show the code you're using.

• ###### 14. Re: Increasing the number inside a text box...

Wow, I'm dumb. I was missing a line of code, my bad.

Here's how it looks:

var scoreVar:Number = 0;

scoreTF(scoreVar);

scoreVar++;
scoreTF(scoreVar);
}

scoreVar++;
scoreTF(scoreVar);
}

function scoreTF(n:Number){
total.text=n;
}

-------------------------------------------------------------------------------

Part 2 (not sure if I need to start another thread or not)

Ok, so now that the above is figured out, is it possible to put in a code that tells the playhead to go to a certain frame based on the number in that textfield?

For example:

Frames 1-5 are question frames that have the above buttons (answer 1 and 3) and textfield (total). But, once the user gets to frame 6, the code would read what's in the 'total' textfield. If the number in 'total' is lower than 3, it would automatically go to frame 7, but if it's higher than 3 it would go to frame 8.

Any ideas?

• ###### 15. Re: Increasing the number inside a text box...

and you can use:

var scoreVar:Number = 0;

scoreTF(scoreVar);

scoreVar++;
scoreTF(scoreVar);
}

scoreVar++;
scoreTF(scoreVar);
}

function scoreTF(n:Number){
total.text=n;

if(this._currentframe>=6){

if(n<3){  // if you want to goto 7 if n =3, change to <=

this.gotoAndStop(7);

} else {

this.gotoAndStop(8);

}

}

}

• ###### 16. Re: Increasing the number inside a text box...

Ok, so I've gotten to the point where I need to insert this script but I'm having some issues. First thing, this is my code:

==============================

scoreTF(scoreVar);

scoreVar++;
scoreTF(scoreVar);
}

scoreVar++;
scoreTF(scoreVar);
}

function scoreTF(n:Number){
total.text=n;
if(this._currentframe>=225){
if(n<5){  // if you want to goto 7 if n =3, change to <=
this.gotoAndStop(226);
} else {
this.gotoAndStop(227);
}
}
}

==============================

Now I did have to change some of the numbers, however, when I get to frame 225, where this code is found, it does do the calculation but if the total score is higher than 5, it sends me back to frame 5. If the total score is lower than 5, if sends me to frame 4.

Any ideas?

• ###### 17. Re: Increasing the number inside a text box...

you have some other code causing that problem.  the code you showed wouldn't do that.

to start debugging you can put trace() statements on 226 and 227 to see if the timeline is going there before going to frames 4 and 5.

• ###### 18. Re: Increasing the number inside a text box...

Ok, so since I know nothing about trace statements, is it possible to come to (and stop on) frame 225 and have a 'calculate' button there along with the 'total' text field, and when pressed, the button will subtract 5 from whatever the amount is in 'total'?! Then, based on the final amount, it would go to frame 226 if the final amount is 5 or lower, or frame 227 if it's 6 and higher.

Doing it this way would be preferable actually now that I've thought about it.

• ###### 19. Re: Increasing the number inside a text box...

open the flash help files and read about the trace() function.  there's not much to read.  that's the single most important debugging tool a flash developer can use.  and, whatever's the 2nd most important is a distant 2nd.

anyway, the answer is yes to your question.  what's stopping you from doing that?

• ###### 20. Re: Increasing the number inside a text box...

That's a good idea, I will defiantely read up on the 'trace' function!

The only thing stopping me from the other idea is that I don't know how to create that math/if script.

• ###### 21. Re: Increasing the number inside a text box...

it's not much different than you have now:

subtractBtn.onRelease=function(){

subtractF(5);

}

function subtractF(n:Number){

scoreVar -= n;

if(scoreVar<=5){
this.gotoAndStop(226);
}  else {
this.gotoAndStop(227);
}

}

• ###### 22. Re: Increasing the number inside a text box...

OK, I'm getting a little frustrated now.

I dropped the calculate button onto the stage, named it 'subtractBtn', and added the code you provided (had to edit it a bit because of unneccessary spaces). However, when clicked it again took me to frame 5??

Guess I'm going to need that trace script sooner than later. Where would I put the trace function?

• ###### 23. Re: Increasing the number inside a text box...

you have some stray code causing the problem.  in particular, check frames 226 and 227 to see if there's a goto function/method.

to check that's the problem, in the top layer of frames 226 and 227 add:

trace(this._currentframe)

• ###### 24. Re: Increasing the number inside a text box...

Ok, ok, ok...

Let me do this in sections. Forget going to a frame as a result of the math for now. Right now I'm just trying to get the 'subtractBtn' to actually do the subtraction.

This is the code I have on the top layer frame:

stop();

subtractBtn.onRelease=function(){

subtractF(5);
}

function subtractF(n:Number){

scoreVar -= n;

}

So, I'm pre-populating the 'total' text box (instead of going through the whole thing) with the number 7 and trying to hit the button to subtract 5 from it, but nothing.

I am lost on this...

• ###### 25. Re: Increasing the number inside a text box...

if you want to update the display in your textfield, you need to add code to do that:

stop();

subtractBtn.onRelease=function(){

subtractF(5);
}

function subtractF(n:Number){

scoreVar -= n;

total.text=n;

}

• ###### 26. Re: Increasing the number inside a text box...

It didn't subtract 5, it changed the 7 to a 5.

• ###### 27. Re: Increasing the number inside a text box...

oops.  my error:

stop();

subtractBtn.onRelease=function(){

subtractF(5);
}

function subtractF(n:Number){

scoreVar -= n;

total.text=scoreVar;

}

• ###### 28. Re: Increasing the number inside a text box...

Sheeesh, I feel like I have to apologize for this taking so long.

Now it changed the 7 to NaN...

• ###### 29. Re: Increasing the number inside a text box...

what's the following show:

stop();

subtractBtn.onRelease=function(){

subtractF(5);
}

function subtractF(n:Number){

trace(scoreVar+" : "+typeof(scoreVar));

scoreVar -= n;

total.text=scoreVar;

}

• ###### 30. Re: Increasing the number inside a text box...

It displays NaN in the 'total' text box again but then the Output box popped open and said:

undefined : undefined

• ###### 31. Re: Increasing the number inside a text box...

what happened to scoreVar?  are you still using that?  are you coding in a timeline different from the timeline that contains scoreVar?

• ###### 32. Re: Increasing the number inside a text box...

No, everything is in the same timeline.

I was assuming that my 'total' text box was the scoreVar?!

The dynamic textbox that I have the score going into has the instance name of 'total', no variable name. Should I change it to 'scoreVar' instead?

• ###### 33. Re: Increasing the number inside a text box...

you're going backwards and undoing things that were settled previously.  in message 5, i suggested:

var scoreVar:Number = 0;  //  initial scoreVar

scoreVar++;  // increment scroeVar because this is  correct answer.

}

function  scoreTF(n:Number){

yourtextfield.text=n;  // assigns scoreVar's  value to yourtextfield's text property

}

now, something's changed.

• ###### 34. Re: Increasing the number inside a text box...

No, no...all that code works just fine. So as the user is going through the game, the score (or the 'total') textbox is increasing as they go...works great!

Now they get to frame 225 (end of the game) and they have a score of 7 (just as an example). In this game I did the lower the score the better, so, they are given a free 5 points to be subtracted from their final total. To calculate that on frame 225, I need the 'subtractBtn' to subtract 5 points from 'total' number once pressed.

It's at this point I need the animation to go to either frame 226 if their score is 5 or lower, or, go to frame 227 is it 6 or higher.

• ###### 35. Re: Increasing the number inside a text box...

if scoreVar were defined on the timeline with the subtract button before the subtract button is pressed, you wouldn't be seeing undefined when trace(scoreVar) executes.

• ###### 36. Re: Increasing the number inside a text box...

Ok, so, in my movie this script is on frame 12 which is the first question in my game:

stop();

var scoreVar:Number = 0;

scoreTF(scoreVar);

scoreVar++;
scoreTF(scoreVar);
gotoAndStop(13);

}

scoreVar++;
scoreTF(scoreVar);
gotoAndStop(13);
}

function scoreTF(n:Number){
total.text=n;
}

Everything works great!

However, from there to frame 225 there are several keyframes in between that are also question frames and therefore has almost the same code on them:

stop();

scoreTF(scoreVar);

scoreVar++;
scoreTF(scoreVar);
gotoAndStop(19);

}

scoreVar++;
scoreTF(scoreVar);
gotoAndStop(19);
}

function scoreTF(n:Number){
total.text=n;
}

It jusdt doesn't have var scoreVar:Number = 0; in it because the score (or 'total') textbox is now displaying an ongogin score, adding to it as the game goes on. There are also various frames in between that have stop(); functions on them. All that above code is not on it's own layer. Is that messing things up?

Cause once you get to frame 225, then I had the code you gave me on it but it was sending me back to frame 5 or whatever when the button was clicked.

• ###### 37. Re: Increasing the number inside a text box...

if all the frame numbers you mentioned are on the main timeline there won't be a problem.  once scoreVar is defined it remains defined no matter what keyframes are encountered (as long as you don't delete scoreVar).

so, does frame 12 play before frame 225 in your current (possibly testing) set-up?

• ###### 38. Re: Increasing the number inside a text box...

Wow, ok so I'm an idiot. The answer is no. I was skipping all the in between frames so I didn't have to go through the whole animation.