Forums

ForumsProgramming Forum

AS3 Memory and Classes

Posted May 15, '12 at 5:08pm

manny6574

manny6574

862 posts

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?

 

Posted May 15, '12 at 7:02pm

strouthas

strouthas

24 posts

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

 

Posted May 15, '12 at 8:39pm

Darkroot

Darkroot

2,549 posts

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.

 

Posted May 16, '12 at 2:53am

manny6574

manny6574

862 posts

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

 

Posted May 16, '12 at 11:58am

Secretmapper

Secretmapper

740 posts

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

 

Posted May 16, '12 at 12:04pm

Secretmapper

Secretmapper

740 posts

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.

 

Posted May 16, '12 at 1:29pm

Secretmapper

Secretmapper

740 posts

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.

 

Posted May 16, '12 at 1:33pm

manny6574

manny6574

862 posts

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.

 

Posted May 17, '12 at 9:21am

Secretmapper

Secretmapper

740 posts

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

Anyway, yeah it would.

 

Posted May 17, '12 at 3:53pm

manny6574

manny6574

862 posts

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

 
Reply to AS3 Memory and Classes

You must be logged in to post a reply!