View Full Version : [AS2] Help in making Game Scores using global variable
ghpnewyork
06-23-2009, 10:00 PM
Hi,
I need some help with AS2, using global variable to track game scores throughout the game.
The game has 7 questions. Each frame will have one question and 3 answer options (they are in buttons).
If you get the right answer, the user will be taken to a new frame where he/she is congratulated.
If you get the wrong answer, the user will be taken to another new frame where the correct answer is given.
Right/Wrong answer frame will have a "next" button on the bottom for the user to move to the next question.
Now here is where I need help with!
I want to have a score board in every frame. Each time the user makes the right choice, he/she is given 1 or 5 points. If not, no score is given.
Since I am moving from frame to frame I know I have to use a global variable.
I'm a beginner when it comes to AS2 so if anyone can give me a detailed code in how to make the scores possible, it will be greatly appreciated.
Thank you!
ghpnewyork
06-23-2009, 10:44 PM
Hi,
I need some help with AS2, using global variable to track game scores throughout the game.
The game has 7 questions. Each frame will have one question and 3 answer options (they are in buttons).
If you get the right answer, the user will be taken to a new frame where he/she is congratulated.
If you get the wrong answer, the user will be taken to another new frame where the correct answer is given.
Right/Wrong answer frame will have a "next" button on the bottom for the user to move to the next question.
Now here is where I need help with!
I want to have a score board in every frame. Each time the user makes the right choice, he/she is given 1 or 5 points. If not, no score is given.
Since I am moving from frame to frame I know I have to use a global variable.
I'm a beginner when it comes to AS2 so if anyone can give me a detailed code in how to make the scores possible, it will be greatly appreciated.
Thank you!
DJRoberts
06-23-2009, 10:49 PM
on the _root timeline (that is the main timeline you see when you first open a flash document), just create a simple number variable which stores the score. Since the variable is declared on the _root timeline, it has global scope, available anywhere in the movie.
var score:Number = 0;
Then if the user is directed to the frame displaying "correct", increment the score:
score++;
bluemagica
06-24-2009, 12:08 PM
in the very first frame put
_global.myscore = 0;
_global.tempscore = 0;
Later each time player answers the correct answer, and you do gotoAndStop(2) or something like that, add this before the gotoAndStop line:
_global.tempscore = Math.ceil(Math.random()*5);
_global.myscore+= _global.tempscore;
_global.tempscore = 0;
As for the score box, create a dynamic textfield, and give it an instance name like : score_txt
then in the timeline put this code
score_txt.text = ""+_global.myscore;
ghpnewyork
06-24-2009, 03:09 PM
Thank you for your help! :-)
I understand what you are talking about, but since I'm quite a beginner, I need a bit more detailed info.
Can you write out the code so I can take a look at it?
Thank you again.
ghpnewyork
06-24-2009, 05:35 PM
To bluemagica:
Thank you for your code.
But I'm having a problem. I get ann error saying that the access for global variable is undefined.
What mistake did I make?
neilmmm
06-24-2009, 10:53 PM
you don't need to use global - you just need to have your score txt span your frames
have a look at this
var score:Number=0;
my_mc.onRelease=function () {
score++;
score_txt.text=score;
this._parent.nextFrame();
}
score_txt.text=score;
stop();
ghpnewyork
06-25-2009, 03:44 PM
Oh my... Thank you so much!
neilmmm
06-25-2009, 11:16 PM
yw
bluemagica
06-26-2009, 01:20 PM
Sorry for replying late, but your actual problem is you are mixing as2 and as3..... though you said you are coding in as2, but in the file you sent me, you are using as3 (eventListeners).
Also your way of using listeners, frames, and buttons, is really inefficient! Try using an array, to store quetions, and show this dynamically to the user.
i think i will write a tutorial on this soon!
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.