Skip to main content
January 28, 2013
Answered

Help on changing variables value

  • January 28, 2013
  • 2 replies
  • 1486 views

trace(now1);

trace(now2);

function now(now1:String, now2:String):void

{

          var now11:int;

          var now12:int;

          var now21:int;

          var now22:int;

          if (now1 == "Cloud")

          {

                    now11 = 2;

                    now12 = 3;

          }

          if (now2 == "Mud")

          {

  now21 = 1;

  now22 = 3;

          }

          trace(now11 + now21);

          trace(now11 + now22);

          trace(now12 + now21);

          trace(now12 + now22);

}

and on the output this is what I get:

Cloud

Mud

0

0

0

0

How do I make the variables inside the function change value?

This topic has been closed for replies.
Correct answer robdillon

text1 and text2 is a text field and they do have a value since i get their output:

Fonts should be embedded for any text that may be edited at runtime, other than text with the "Use Device Fonts" setting. Use the Text > Font Embedding command to embed fonts.

Cloud

Mud

If I do anything wrong can you show me how I should write the code?


OK, I made a new movie, copied your code from the last listing into the movie. I created two input text fields and and named them "text1" and "text2" and put "Cloud" and "Mud" into them. I created a button and named it "btnNew".

I got this in the output window:

Cloud

Mud

3

5

4

6

Do you have more in your movie, or do you have some of the assets in different frames?

Here is a copy of the movie that I made: http://www.ddg-designs.com/downloads/gentifa.zip

2 replies

robdillon
Participating Frequently
January 28, 2013

Where are you declaring the values for "now1" and "now2"? How are you calling the function "now"?

January 28, 2013

var now1:String = text1.text;

var now2:String = text2.text;

Ned Murphy
Legend
January 28, 2013

You can only change the values of the variables inside the function within the function since they only have scope within the function.  Thetraces you get should not be what you get if you are implementing this correctly.  When I implement it correctly I get the following output....

Cloud

Mud

3

5

4

6

So maybe you are calling the function at a time when neither now1 and now2 are defined or assigned the values you test for.  Try tracing the values of now1 and now2 inside the function

January 28, 2013

can you show me the code please?

Ned Murphy
Legend
January 28, 2013

Did you try tracing the values of now1 and now2 inside the function?  Do that if you haven't.

The ciode I used is not any different than yours, but it is written to execute properly with values defined as/when needed...

var now1 = "Cloud";
var now2 = "Mud";


trace(now1);
trace(now2);

function now(now1:String, now2:String):void
{

          var now11:int;
          var now12:int;
          var now21:int;
          var now22:int;

          if (now1 == "Cloud")
          {
                    now11 = 2;
                    now12 = 3;
          }

          if (now2 == "Mud")
          {
                   now21 = 1;
                   now22 = 3;
          }

          trace(now11 + now21);
          trace(now11 + now22);
          trace(now12 + now21);
          trace(now12 + now22);

}

now(now1, now2);  // calling the function after the values are defined