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

where and how to declare global variables

New Here ,
Mar 30, 2009 Mar 30, 2009
I am creating a multiple choice quiz in Flash. I have created one movie clip named mc_Quiz. This movie clip contains an action layer and an object layer. Thw action layer contains 1 keyframe with code ( code for a button that moves to the next frame).

The object layer contains 11 keyframes 10 of the keyframes contains
a single movie clip question, (mc_Q1 through mc_Q10) and a btnNext to move to the next frame. The question movie clips (e.g. mc_Q1) contains a “btnCheck” button and the code to check the answer.

The 11th frame contains a movie click that contains a “userName” input text box, a user “score” dynamic text box, and a “submit” button. I am not submitting the information anywhere yet, but have it so that when I click the ‘submit” button it enters “Please enter your name” if it is empty. And if is not empty it enters “Thank you.”

What I need to do is to have the “btnCheck” to add 10 points for every correct answer to a variable. Then place that total in the “score” dynamictext box when the user moves from the 10th frame to the 11th. I am not sure where to declare the variable and how to add to the variable each time the correct answer is given.

How is this done?

Here is one thing that does not work.

First frame of “mc_Quiz”:

stop()
var advanceNext:Number = currentFrame;
var static; userScore:Number;
btnNext.addEventListener(MouseEvent.CLICK, nextQuestion);

function nextQuestion(evt:MouseEvent):void{
gotoAndStop(advanceNext ++);
}



In frame on of “mc_Q1”

stop()
messageBox.text = ""; // Clears the Response box
var correctAnswer:String = "sanctify"; // Sets correct answer for comparison
var userAnswer:String; // with value from radio button group.
var rbg:Object = rbA.group; // Get the name of the ragio button group from the first button.


btnCheck1.addEventListener(MouseEvent.CLICK, checkAnswer1);

function checkAnswer1(evt:MouseEvent):void {
userAnswer = String(rbg.selectedData);
if (userAnswer == correctAnswer) {
messageBox.text = "Yes, " + userAnswer + " is CORRECT!";
userScore = score.value + 10;
rbA.enabled = false;
rbB.enabled = false;
rbC.enabled = false;
rbD.enabled = false;
}
else {
messageBox.text = "Your answer is INCORRECT.";
rbA.enabled = false;
rbB.enabled = false;
rbC.enabled = false;
rbD.enabled = false;
}
}

I actually have no real clue where to declare the variable or how to add 10 points to it when I select the correct answer. It appears to me that I must some how change this variable within the function of the button.
TOPICS
ActionScript
3.2K
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 , Mar 31, 2009 Mar 31, 2009
AS3 does not directly support global variables as its predecessors do, but there are ways around it. Without going the global route, to communicate from a child object to a parent object the normal approach in AS3 would be (literally)...

MovieClip(this.parent).totalScore += 10;

Alternatively, you can create a global class and utilize that. If you save the following in a file named "global.as" in your class path (or in the folder your project is in), you can utilize the variable in a global sen...
Translate
LEGEND ,
Mar 30, 2009 Mar 30, 2009
If you define all of the code in the main timeline, then that's all you'll need. You can simply declare a var...

var totalScore:Number;

btnCheck.addEventListener(MouseEvent.CLICK, checkAnswer);

function checkAnswer(evt:MouseEvent):void{
if(answer_correct){ // made up a variable
totalScore += 10;
}
}
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
New Here ,
Mar 31, 2009 Mar 31, 2009
Thank you for the suggestion but it did not work. It seems that I am unable to set a variable from outside a movie clip and then change it from inside a function that is in a movie clip. This doe not make sense to me. There has got to be a way to do this. This seriously limits the functionality of actionscript 3.0 I am sure that something is being missed here. Perhaps a global variable, but I have not been able to find anything. That explains how and where to set a global variable.
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 ,
Mar 31, 2009 Mar 31, 2009
AS3 does not directly support global variables as its predecessors do, but there are ways around it. Without going the global route, to communicate from a child object to a parent object the normal approach in AS3 would be (literally)...

MovieClip(this.parent).totalScore += 10;

Alternatively, you can create a global class and utilize that. If you save the following in a file named "global.as" in your class path (or in the folder your project is in), you can utilize the variable in a global sense, just making sure to import the class where it's needed:

package
{
public const global:Object = new Object();
}


Using it in the Flash file...

import global;

global.totalScore = 0;
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
New Here ,
Mar 31, 2009 Mar 31, 2009
Thanks for the info. I am trying this first.

MovieClip(this.parent).totalScore += 10;
This appears to be working


Also, in my last frame I am using dynamictext to display the score. Will I be able to display the value of totalScore in this dynamictext at the end of the quiz?

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 ,
Mar 31, 2009 Mar 31, 2009
Yes.

dynamicTFName.text = totalScore;

where dynamicTFName is whatever name you give it.
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
New Here ,
Mar 31, 2009 Mar 31, 2009
Ok the text box named (dispScore) is in frame 11. (Not part of MovieClip)

in frame 1 of the action layer I have:

var totalScore:Number = 0;
dispScore.text = totalScore;

I get an error when testing the movie.

1067: Implicit coercion of a value of type Number to an unrelated type String.

So I made it a part of the MovieClip that includes the user name also on frame 11 then placed this in the MovieClip

dispScore.text = MovieClip(this.parent).totalScore;

And it works

just thought I would let you know

Thanks for your help

P.S. would still like to know why it did not work
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 ,
Mar 31, 2009 Mar 31, 2009
textfields holds strings, your variable is a number, AS3 is a pr_ck about having data types be the same and gets its jollies from saying so (AS2 wouldn't care)...

dispScore.text = String(totalScore);

would have worked, but only in frame one that one time. You'd have to assign it each time it changes to have it update.


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
New Here ,
Mar 31, 2009 Mar 31, 2009
Thanks so very much Ned

That makes sense.

And this MovieClip(this.parent).totalScore += 10; is going to be very valuable to me.

I have some more stuff that I have not done before. But I am learning. Of course. I just started learning action script 3, so I have a lot to learn.

I am thinking though the since I have only one keyframe in my action layer, would this...dispScore.text = String(totalScore); not have also worked in that frame?
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 ,
Mar 31, 2009 Mar 31, 2009
LATEST
You're welcome. If you have any kind of programming background, it'll be a a bonus for you, and anyone who tries 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