Copy link to clipboard
Copied
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?
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
can you show me the code please?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Scene 1, Layer 'Layer 2', Frame 1, Line 40 | 1180: Call to a possibly undefined method now. |
I get this error with your code is there a mistake I'm doing?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
where do you think my mistake is at?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
This is exactly how mine looks like but it doesn't work the same
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
thanks for taking your time to help
Copy link to clipboard
Copied
I used yours and I think the only mistake I'm making is I convert btnNew to a button not to a MovieClip
Copy link to clipboard
Copied
Where are you declaring the values for "now1" and "now2"? How are you calling the function "now"?
Copy link to clipboard
Copied
var now1:String = text1.text;
var now2:String = text2.text;
Find more inspiration, events, and resources on the new Adobe Community
Explore Now