ForumsProgramming ForumAS3 Memory and Classes

12 5972
manny6574
offline
manny6574
922 posts
Nomad

Hello, some of you may remember me as I used to come here quite often.

I remember this forum had a lot of good developers, and I am back into AS3 after a year of having quit.

Anyway, long story cut short: I'm making a game that is the top of my ability, which seems to have improved on its own. I have an Enemy class and 16 types of enemies, and another massive load is being thought up and drawn by my friend. The enemies have different behaviours. I have two options:

1. Use a switch statement in the Enemy class to test for the type of enemy, and execute the corresponding function. That would mean the Enemy class has about 80 functions in the end - 40 pseudo-constructors and 40 handler for different ENTER_FRAME events

2. Make separate classes for each of the types, that extend the Enemy class. Contain all code in here.

WHICH OPTION IS BEST IN TERMS OF MEMORY EFFICIENCY?

  • 12 Replies
strouthas
offline
strouthas
24 posts
Nomad

Depends on your constructors and what code you execute in there, but in general separate classes tend to require less memory.

Darkroot
offline
Darkroot
2,763 posts
Peasant

Number two, always go with the method that is the most decomposed and OO as you can. It mostly always leads to it being more efficient and well designed. But I doubt either of them would tax a system in this case, it doesn't look very complex.

Maybe make enemy an abstract class and have abstract methods for the enemies but it also sounds you need to make enemy a "grandparent" class.

manny6574
offline
manny6574
922 posts
Nomad

So you mean have the Enemy class and a few sub-classes of it and then the specific enemy classes under that?

Secretmapper
offline
Secretmapper
1,747 posts
Nomad

Use Design Patterns here. You're using AS3, Polymorphism is an advantage you have.

Secretmapper
offline
Secretmapper
1,747 posts
Nomad

So you mean have the Enemy class and a few sub-classes of it and then the specific enemy classes under that?


Never do this. NEVER,EVER have some sort of deep hierarchy like this.
Secretmapper
offline
Secretmapper
1,747 posts
Nomad

So you mean have the Enemy class and a few sub-classes of it and then the specific enemy classes under that?


Never ever do this. You don't want deep hierarchy.

Use Polymorphism. You're using AS3, and that is an advantage.
manny6574
offline
manny6574
922 posts
Nomad

Polymotphism uses an interface right? Because I came up with that I idea earlier today:

interface Enemy, which is implemented by all the enemy type classes:

Example

public class RobotThingie extends Movieclip implements Enemy{};
public class SomethingElse extends Movieclip implements Enemy{};

this would also eliminate problems in creating the enemies.

Secretmapper
offline
Secretmapper
1,747 posts
Nomad

Why doesn't AG update... that's why I have posts containing the same message :/

Anyway, yeah it would.

manny6574
offline
manny6574
922 posts
Nomad

Yeah, that's what I did. And it's working so far. BTW, I hope to have the game on AG so keep your eyes peeled

Secretmapper
offline
Secretmapper
1,747 posts
Nomad

Well gl hf I guess

Annihalation
offline
Annihalation
479 posts
Nomad

Unrelated, but ETA on your game? Also, I have to agree with Darkroot, keep it as object oriented as you possibly can, It will help in the long run lol. Good luck on your developing

manny6574
offline
manny6574
922 posts
Nomad

Planned date is the week before the endo fo summer holidays, so something like 20 August - 1 september 2012.

far off because I am the only coder and have to work in between other things towards end of year.

Showing 1-12 of 12