ForumsProgramming ForumHelp in Math!

13 4564
mystera
offline
mystera
39 posts
Nomad

I'm trying to make a game where you have to answer the math problems then press enter. I got the problems generated, but for some reason when I press enter, it goes to the first slide instead of the 4th. Can anyone help?

  • 13 Replies
mystera
offline
mystera
39 posts
Nomad

So there are 2 sections in the question. num1 and num2 (variables and instance name)

the answer box is called ans12.

On the screen, it looks like this: num1 + num2 (the random generated numbers)

Then, I made a little button and it says that when you press enter, it will go to the next problem if you got it right. If you got it wrong, it will go to the game over screen.

This is how I coded it.
on (keyPress "<Enter>&quot

{
if(ans12_txt.text == num1+num2) {
nextFrame();
}else{
gotoAndStop(1); //change to right gameover frame
}
}


It doesn't work. I need help!

dank
offline
dank
983 posts
Peasant

That's because ans12_txt.text is a a String where num1+num2 is a Number. You'll either have to convert the number into a string or convert the string into a number (though it's more likely to exceptions).

mystera
offline
mystera
39 posts
Nomad

How can you do that though? I'm not very good in AS2...or just AS in general.

PixelSmash
offline
PixelSmash
566 posts
Nomad

While I can't exactly remember how it's done in AS2, I believe it's the same as in AS3... basically, instead of your line if(ans12_txt.text == num1+num2) you use if(ans12_txt.text == String(num1+num2))

This should convert the resulting number of num1+num2 to a string, after which you can compare it with the ans12_txt.text! Hope this helps!

mystera
offline
mystera
39 posts
Nomad

That doesn't work. Sorry. It still doesn't go to the next frame. It goes to the first frame instead, where the else code is. I think it says that the answer is wrong, so it will gotoAndStop frame 1.

Fighterlegend
offline
Fighterlegend
44 posts
Nomad

Try:

if (Key.isDown(Key.ENTER)){
{
if(ans12_txt.text == num1+num2) {
nextFrame();
}else{
gotoAndStop(1); //change to right gameover frame
}
}

Fighterlegend
offline
Fighterlegend
44 posts
Nomad

if (Key.isDown(Key.ENTER)){
{
if(ans12_txt.text == (num1+num2)) {
nextFrame();
}else{
gotoAndStop(1); //change to right gameover frame
}
}


Try this one, might work.

mystera
offline
mystera
39 posts
Nomad

I'll try, but I'm using AS2.0 by the way. Is that AS2?

Fighterlegend
offline
Fighterlegend
44 posts
Nomad

Yeah, it should be..

I don't know, you might need to change:
if(ans12_txt.text == (num1+num2)) {

to
if(ans12_txt._text == (num1+num2)) {


But, I'm used to AS3... So..

dank
offline
dank
983 posts
Peasant

Try casting (DataType(datatoCast)) or using the toString method (Number.toString()). You might have to make a string comparison, though I haven't done something like this in a while.

mystera
offline
mystera
39 posts
Nomad

To Fighterlegend:

I used your first and second help but sadly it didn't work. These are the errors from the compiler.
:
Statement block must be terminated by '}' if (Key.isDown(Key.ENTER)){

Statement must appear within on handler if (Key.isDown(Key.ENTER)){

Fighterlegend
offline
Fighterlegend
44 posts
Nomad

Oh, add another } at the end..

That's possibly what happened.

Fighterlegend
offline
Fighterlegend
44 posts
Nomad

OHHH, my bad!

if (Key.isDown(Key.ENTER)){
if(ans12_txt.text == num1+num2) {
nextFrame();
}else{
gotoAndStop(1); //change to right gameover frame
}
}

Showing 1-13 of 13