Smeestad
Smeestad

Reputation: 37

AS3 - Score counter

I am making a simple math game with four frames. In the first frame you can choose the math operation you want to practice. In the second frame you are presented with a randomly generated math question. If you enter the correct answer, the application outputs "correct answer", and skips back to frame one. You get one point for every correct answer. What is the simplest way of keeping the score, and output it on the screen?

Upvotes: 0

Views: 162

Answers (2)

wezzy
wezzy

Reputation: 5935

The first thing is to abandon the frame logic and start reasoning about classes and components. In object oriented programming there are many ways to do what you want. This is an architectural question. One way could be to pass the reference of the main application to the class (or the function) that run the "quiz frame". You should implement a saveScore(score:int):void method so when the quiz is ended your method can call this new saveScore() method to update the score.

This is a strict relationship between your main application code and your quiz frame code. In a simple case like this is should work and it's not a problem but generally speaking is always better lo have components loose coupled (https://en.wikipedia.org/wiki/Loose_coupling) because you never know what you will need in the future and maybe someday you'll have to change your whole architecture and the less your components ad connected and dependent the easier is the change (refactoring).

I usually choose to have a singleton for every app (https://en.wikipedia.org/wiki/Singleton_pattern) or some shared place to save data accessible from every class of my application.

For example you can use a class like this:

class Registry{
    public private static var _score:int;

    public static setScore(score:int):void{
        Registry._score = score;
    }

    public static getScore():int{
        return Registry._score;
    }
}

and everywhere in your program you can call Registry.setScore(10) and Registry.getScore(). This helps your greatly because the components in your program doesn't know each others and they doesn't know which method are exposed they just need to know the Registry.

Upvotes: -1

null
null

Reputation: 5255

To be honest, the answer to pretty much every question that includes "...with ... frames..." which asks for the simplest/best/sane way to do something is "without frames".

Whenever you switch frames, all code on that frame is executed. If you visit a frame again, the code is executed again. If you set your score to 0 on the first frame, whenever you go to that frame again, you reset the score to 0.

The simplest way, as far as the score goes, is to only initialize it once. According to the things mentioned above, this means that you should never visit the frame, that sets score to 0 again.

You can do this in two ways:

  1. split your first frame into two frames: both looking the same, the first one does the initialisation and whenever you want to "go back to frame 1", you actually go to the second frame
  2. you don't use multiple frames: take the content of each of your frames and put it into its own symbol (MovieClip). Instead of switching between frames, you add/remove these MovieClips to the display list.

I recommend 2, because it is the better way of doing it. But 1 is simpler to do.

Upvotes: 2

Related Questions