The game loop and other issues.

7 posts

Flag Post

Hi folks.
I’m new to Flash, and am trying to get my head around the programmer model. I’d appreciate it if you could look this over and point out my misunderstandings.

Suppose I have a game with animated (movieclip) player and enemies. These need to move around until they’re shot (or whatever). Then they change to a new animation, blow up, die and get removed.

So we need to:
1)Create new objects.
2)Move them around
3)Do some collision detection and response
4)remove dead ones.

And we want all the code in one place. We have some onEnterFrame interrrupt which is called x times a second, and this has housekeeping code in it, fine. This can read player input, see if they’re dead yet, perhaps keep the score display updated and so on.
1) should be fairly easy. New objects are generally created in this loop or or from some object-specific code.
2) Objects need to know what to do and how to move. Perhaps we keep track of the things we’ve made, then each object doesn’t need a separate event?
3) If we have such a list, we can parse it for any collisions we’re interested in, then use hittest, some trig. or whatever. Then we have to swap them out for a ‘death throes’ animation by switching movieclips somehow. (In fact, for every animation, we’d probably need a new movieclip, because we’re not allowed to put code on movieclips.)
4) We then wait for the animation, counting play-cycles, and remove the object when we know the dying animation is done.

Have I missed the useful flash short-cuts?
I’m having difficulty believing that I’m not. Is there some way of programmatically putting code onto movieclips on certain frames? Then we could have many different animations in each movieclip, with gotoandplay() loops set up. For dying, put removeMovieClip() on the last frame of the explosion, and 4) would take care of itself.
And if I have a lot of object AI in a complex per-frame, all-object loop, does it start to become at risk of timing out? If so, how can that be mitigated?

The other thing is… I’m kind of used to dealing with integers. Flash doesn’t seem to have them as a primitive type, and I suspect that will make it hard to do rock-solid integer maths (don’t ask why).
Building things from arrays of Booleans might be wasteful on memory, and slow. What are my best options here? Muck around with strings as arrays of 16 bit quantities? Fudge the same sort of thing using opaque bitmaps?

 
Flag Post

I think you’ve got the idea down enough to progress a good ways. There are many different ways to do this as you get into the more advanced stuff, but for the first steps you’re on the right track.

Regarding integers, AS2 only has numbers but AS3 has integers, unsigned integers and numbers. If you need to manipulate bits you can as well, using the bitwise operators. AS3 also has the ByteArray object. I dont really know what your looking to do that you can only do with integers, but you can always use the Math.round/etc methods. In most cases I dont see how the lack of an int type in AS2 would limit you. Type casting in AS2 is only used for compile time errors anyway, they dont actually compile into the code and have an effect on memory space or performance.

 
Flag Post

Then we could have many different animations in each movieclip, with gotoandplay() loops set up. For dying, put removeMovieClip() on the last frame of the explosion, and 4) would take care of itself.

Yeah, I used a lot of this type of stuff for P.O.D., which was my first really big Project in Flash. For the squid boss, he has many different animation cycles, and I put code on the frame at the end of each cycle which calls a main function to determine which one to skip to next. So, it all looks fluid, but it’s different chunks of animation, in different places, on the same MovieClip.

The same for the explosions and dying animations… they have a remove function at the end which puts them away out of sight (back in the pool).

So, essentially, I’m curious about AS 3 as well, since it’s not allowing us to put code on MovieClips… how are we going to know when a particular animation has ended so we can remove the object, or make other calculations?

 
Flag Post

You dont really need to put code on a clip to know when its done. You can put it inside as well. And in that regard, you can put code inside MovieClip in AS3 just the same. In fact, since its all class based, you dont have to call or save object references to your class (ie my_mc.objRef = this; objRef.myMethod() ). If you have a function in your class called “animationDone”, you can just call animationDone() (or _parent.animationDone(), etc) straight from the MovieClip. This is similiar to associating a class with a MovieClip library item in AS2.

 
Flag Post

Oh… so you mean the difference between putting the code on the Clip and on the Frame…. ooooh. Well, in that case I’m going to like AS 3. Bring it on!

As a side note also, scoping was the biggest issue that I had in learning how to work with OOP in Flash. I’m glad that’s being made easier, too :)

 
Flag Post

Thank you for your replies above.

I eventually stopped prevaricating and wrote a game. Now I have a much better idea of what flash can do, although I’ve not entirely got my head around the whole class thing.
I’m using Flash 8 incidentally, so AS3 isn’t available.
But I am still unsure of how to approach any sort of complex animation on objects, apart from how IndieFlashArcade described. Clearly that is OK for one or two boss characters, but not feasible for 10^6 grunts.

ArcaneCoder, could you explain that to me again please?
I see that movieclips can read their current frame, so are you suggesting something like:

onEnterFrame = function() {
...
//grunt animation
 for (i=0;i<=gruntmax;i++){
 g=eval("grunt"+i);
  switch (g._currentframe){
  case 12:g.gotoandplay(0);break;
  case 24:g.gotoandplay(13);break;
  case 36:g.gotoandplay(25);break;
  case 48:g.gotoandplay(37);break;
  }
 }
...
}

I may have made some silly errors with the formatting, but I hope you see the idea. Is that the sort of thing you were suggesting?
If you had lots of different creature types with equivalent animations, maybe it would be possible to use named frame pointers rather than hardcoded positions.
As you may have noticed, I didn’t understand your points about classes or putting code inside objects at all…

I only partially understand OOP; perhaps classes would let you do the same sort of thing a bit more elegantly. But is this the way of manipulating movieclip animations you were proposing? Obviously it looks at every creature at every frame, which would be quite
inefficient if there were very long animations. I have a feeling I’m missing some way to trigger an event from particular frames.

 
Flag Post

If you’ve done any CompSci, one way I find useful to think about all those MoveClips is to treat each MovieClip timeline as a sorta Finite State Machine (FSM). You then map states to frame-numbers and you’re away…. (animations are transitition states that take many frames).

So you do basically have a switch()/case: block to control the whole FSM thing. Your approach of having a loop that goes over every grunt and controls it externally is correct. If you use the Flash Author interface you could put all the controlling script into the grunt MCs….. this treats MCs more like autonomous agents / threads, but is actually a big performance killer when the number of grunts increases. (Flash has overheads for scheduling the next script to run).

Avoid the eval() thing. Eval is a big performance killer (though that might not be a problem for a simple game). Consider the maintaining your own list of grunt MC references – the Array() class has handy things to help here: Array.push(), Array.splice().
(BTW: Looking at your code above, you would need to track (or test and skip over) holes in the grunt numbering as grunts spawned, unspawned).

Another trick is that you can actually set _quality on a MC by MC basis. Mostly people set it once on the _root and leave it at that. BUT set MCs that move fast set with a lower quality and nobody will notice. Also, if you can guarantee that your game will stick to 100% scaling, some items you can draw with exact px line widths on exact px boundaries using only 90degree lines then those will set fine to lower quality without the difference being noticeable.