Skip to main content
January 6, 2016
Answered

Having problems comparing with If statement (Basic)

  • January 6, 2016
  • 2 replies
  • 955 views

Hi, at school we are learning to use Flash and we are using AS2, and I was working on an example code, but it doesn't seem to want to work. Here is the code:

var money:Number=0;

dollar_btn.onPress=function()

{

money+=1;

box_txt.text=money;

}

if(money>5){

check_btn.onPress=function()

{

box2_txt.text="Yay!";

   

}

}

As you can see, all I want to do is have a button, that once pressed, adds 1 to the money counter, and one it is higher than 5, I can press the next button and have it display "Yay!". The problem is, the if doesn't seem to work, even if the money variable is way above 5. I don't think it's a problem with the buttons, because I have tried changing the code to declare the variable with a starting value of 6, and the If works. Any ideas? It would be greatly appreciated !

Thanks in advance

This topic has been closed for replies.
Correct answer nezarov

Put the if() statement inside the function:

var money:Number=0;

dollar_btn.onPress=function()

{

money+=1;

box_txt.text=money;

}

check_btn.onPress=function()

{

if(money>5){

box2_txt.text="Yay!";

}

}

2 replies

January 7, 2016

Wow! Thank you so much! I've been having trouble with this for multiple days and I can finally continue my work! I'm surprised it's so simple . +1

Inspiring
January 7, 2016

You're welcome and good luck.

nezarovCorrect answer
Inspiring
January 7, 2016

Put the if() statement inside the function:

var money:Number=0;

dollar_btn.onPress=function()

{

money+=1;

box_txt.text=money;

}

check_btn.onPress=function()

{

if(money>5){

box2_txt.text="Yay!";

}

}