Skip to main content
July 22, 2013
Answered

Why won't Variable A equal Variable B

  • July 22, 2013
  • 1 reply
  • 971 views

Hello all

I have a page in my Flash file where a user can select one of three values, and it will add or subtract from the variable I have on Frame 1.

So on Frame 1 I say:

var f2f:Number = 5;
var coach:Number = 5;
var mentor:Number = 5;

And on Frame 13 I was saying:

btnP13toP14.addEventListener(MouseEvent.CLICK, NavPage14);

btnContentSmall.addEventListener(MouseEvent.CLICK, contentSmall);
btnContentLarge.addEventListener(MouseEvent.CLICK, contentLarge);

function contentSmall(event:MouseEvent):void
{
f2f = -10
coach = -10
mentor = -10
}

function contentLarge(event:MouseEvent):void

{

f2f = +10

coach = +10

mentor = +10

}

But it occurred to me that if the user clicks on btnContentSmall, and then on btnContentLarge, the values would become 5, rather than 10. 

So now what I am trying to do is add temporary variables to Frame 13 that store the correct value, and then update the original values when I go to the next frame (which will have a similar process).

Here is what I have now on Frame 13:

btnP13toP14.addEventListener(MouseEvent.CLICK, NavPage14);

btnP13toP14.addEventListener(MouseEvent.CLICK, copyBack);


btnContentSmall.addEventListener(MouseEvent.CLICK, contentSmall);
btnContentLarge.addEventListener(MouseEvent.CLICK, contentLarge);

var f2fTemp:Number = f2f;
var coachTemp:Number = coach;
var mentorTemp:Number = mentor; 

function contentSmall(event:MouseEvent):void
{
f2fTemp = f2f-10
coachTemp = coach-10
mentorTemp = mentor-10
}

  

function contentLarge(event:MouseEvent):void

{

f2fTemp = f2f+10

coachTemp = coach+10

mentorTemp = mentor+10

}


function copyBack(event:MouseEvent):void
{
f2f = f2fTemp;
coach = coachTemp;
mentor = mentorTemp;
trace ("I ran the CopyBack script");
}

The Trace statement on the last function is displayed, but when I trace the variables on Slide 14, the f2f, coach and mentor variables have not updated: they are still all 5.

Does anyone have any idea what I'm doing wrong?  I found one website that said I might need to use a class; but I didn't really understand how to translate that information into a solution.

Thank you

M Hetherington

This topic has been closed for replies.
Correct answer Ned Murphy

For the scenario you just outlined at the end, if you want the value to be 15 then you need to assign it a value of 15. 

In your first posting, in the first part, you were doing more like what you say you want to do...

f2f = +10

coach = +10

mentor = +10

Those are not adding 10 to the current value of those variables, they are assigning fixed values...  +10 = 10,   -10 = -10, etc

What you explained just now sounds like you wanted to try to have...

f2f += 10

coach += 10

mentor += 10

those would be adding 10 to the current value, so -5 would become +5 and 5 would become 15

But what you just said is that if you click the btnContentLarge button you want the value to be 15 regardless of what the current value is... so that means you needs to assign it a value of 15

1 reply

Ned Murphy
Legend
July 22, 2013

Your reasoning for the first part of your posting is wrong, which makes it harder to understand what you are trying to do with the second explanation you offfer.  In the first part you are directly assign diiferent fixed values to the variables in each case... 5, 10, or -10, and they will all be equal.  The values will not become 5, they will become whatever values relate to whatever button is clicked.

What are you really trying to accomplish?

July 22, 2013

The variables assign a value of a particular type of training to a scenario.  So if we start with a default value of 5.

If you indicate the size of the project is small, by clicking btnContentSmall, then the types of training are less important, so each one becomes -5 (Original value of 5 minus 10).  There are more variables that I didn't list that would increase.

Similiarly if you indicate the size of the project is large, by clickign btnContentLarge, then the types of training are more important, so each one becomes 15 (original value of 5 plus 10).

If a person clicks the btnContentSmall, which makes the value -5, and then changes their mind and clicks the btnContentLarge; the value will currently become +5 instead of 15.

I want the value to become 15.

Does that give an adequate explanation?

July 22, 2013

And just to clarify, I do have all the correct semicolons in the code; even though I didn't add them to the quoted text (I'm working on two PCs so i can't copy and paste).