Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Help on changing variables value

Guest
Jan 28, 2013 Jan 28, 2013

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?

TOPICS
ActionScript
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 28, 2013 Jan 28, 2013

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

Translate
LEGEND ,
Jan 28, 2013 Jan 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013

can you show me the code please?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 28, 2013 Jan 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013
Scene 1, Layer 'Layer 2', Frame 1, Line 401180: Call to a possibly undefined method now.

I get this error with your code is there a mistake I'm doing?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 28, 2013 Jan 28, 2013

Yes, there must be some mistake you are doing.  If you create a new file and just paste in the code I showed you should not get any error and the trace results I indicated should appear.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013

where do you think my mistake is at?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013

var new1:String = text1.text;

var new2:String = text2.text;

trace(new1);

trace(new2);

 

btnNew.addEventListener(MouseEvent.CLICK, reNew);

function reNew(e:MouseEvent):void

{

  makeNew(new1, new2);

}

function makeNew(new1:String, new2:String):void

{

 

          var new11:int;

          var new12:int;

          var new21:int;

          var new22:int;

          if (new1 == "Cloud")

          {

                    new11 = 2;

                    new12 = 3;

          }

          if (new2 == "Mud")

          {

                    new21 = 1;

                    new22 = 3;

          }

          trace(new11 + new21);

          trace(new11 + new22);

          trace(new12 + new21);

          trace(new12 + new22);

}

So far this is my renewed code but I still can't fix it

the only object I have is text1, text2, and btnNew

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 28, 2013 Jan 28, 2013

If you have all of this code and the two text input fields and the button on frame 1, then this will not work as it is.  You are trying to define the values of "new1" and "new2" in this frame. If these are input text fields then they, probably, won't have any text in them yet. This means that when you use those variables later in the function, they will have a value of "".

You are going to have to set the value of those variables after the text is entered.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 28, 2013 Jan 28, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013

http://img560.imageshack.us/img560/8825/96bfb56da08b42769f0a131.png

This is exactly how mine looks like but it doesn't work the same

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 28, 2013 Jan 28, 2013

If that's the case, then I'm guessing that you have a typo someplace or that there is some sort of corruption in your movie. Start over with a new fresh movie and see what you get.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013
LATEST

thanks for taking your time to help

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013

I used yours and I think the only mistake I'm making is I convert btnNew to a button not to a MovieClip

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 28, 2013 Jan 28, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013

var now1:String = text1.text;

var now2:String = text2.text;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines