views:

205

answers:

3

What are the advantages and disadvantages of having just one main loop and having a main class control individual objects on your stage, in oppose to having each individual object have its own loop and giving it the responsibilities of doing what ever it has to do on its own. What are consider best practices.

A: 

I don't know that "loop" is necessarily the right term here.

If you're talking about having a single DocumentClass that controls everything versus having a lot of classes (ie most of your major visual elements have their own classes with their own internal logic) then it is to some degree a matter of personal preference. However, generally speaking it's probably a good idea to separate your concerns as much as possible.

If you have a main document with, let's say, two characters - the player and an enemy monster - then you don't want to write all of your code on the document. Instead, you want the player and the monster to each have their own internal code. That way it's easy to later add another monster, for instance - does that make sense?

Rather than writing out all of the complex logic for each monster twice, you simply create a Monster class and then you know that it'll always react the same way, even if you put a million on the stage. And you only have to write the code once.

Does that make sense? It's kind of a vague question, but yeah - if you want to take a "programmer's" approach it's always best to write the least amount of code possible. In the case of Flash architecture, that frequently means letting your code live where it can be most efficiently reused.

I hope that helpS!

Myk
I see what your saying. but I wouldnt put all the logic on to the main class. I would put all of each objects behaviors in its own class. But I was thinking it would be easier to call their behaviors from one centralized loop. This way when you stop the game or pause it or reset it. Everything stops at once. I find it very difficult to keep track of loops when they are all encapsulated inside of individual display objects.
numerical25
I gotcha - well, one method you can use is to write a LoopManager class and implement it as a Singleton - meaning that there is only ever one instance of the LoopManager, and all of your various objects have a reference to it. Then when a loop starts you can register it with the LoopManager. If you need to stop the loops, do something like LoopManager.pauseAll() and you're set.That's only one approach - depending on what you're doing it may be far from the best way. Google "AS3 Singleton Pattern" if you're not familiar with it.
Myk
That makes sense. It is definitely smart. I will have to look into that. Never heard of someone making a singleton for a loop. but it seems smart.
numerical25
A: 

I tend to copy the model used in XNA framework for C#. That is I have a class that has one main update loop. It then loops through all the entities and calls their update loop.

Allan
A: 

Using a single main loop definitely makes it easier for you to add pause/unpause functionality. In general, non-Flash game engines have a single main game loop. Individual objects have update() and draw() functions which are called from the main loop.

Here's a good explanation of the concept for Flash: Pause Your Game in Flash AS3.

Selene