Be a Supporter!

As3 Oop...help Me Understand....

  • 132 Views
  • 3 Replies
New Topic Respond to this Topic
Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 22
Game Developer
As3 Oop...help Me Understand.... 2013-09-18 21:49:27 Reply

Still trying to grasp OOP and how different classes communicate with each other..... Maybe I'm just thinking too hard about it. Or maybe I'm just overlooking something simple that once I grasp it a light will turn on in my head and I will finally get it...

Say I have a Player class that controls the characters movements and health. Then say I also have an Enemy class that controls the characters movements, or AI, and it's health.

Which class would I run hit detection in??? Like if I do it in the Player class then it doesn't recognize enemy unless I do something like: var enemy:Enemy enemy = new Enemy ....right? But even then how do I get it to reduce the health in the enemy class?

Furthermore, if I have to initialize the enemy in the Player class wouldn't that make the Player class full of variable being initialized for every type of thing that requires hit detection with the Player??? That just doesn't seem like it belongs there.

I could create like a Game Manager class where I could initialize all variables in the game that require hit detection. Is that what I should do? Seems like it would make one really long class.... Plus that still doesn't solve the issue of how to affect the health state in the Player and Enemy class....

As you can tell I'm seriously having trouble understanding getting classes to communicate with each other.....

I know there are some amazing AS3 coders on these forums so please help me out!

Thanks in advance guys.


BBS Signature
MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Online!
Forum Stats
Supporter
Level 16
Game Developer
Response to As3 Oop...help Me Understand.... 2013-09-18 21:54:44 Reply

At 9/18/13 09:49 PM, Hero101 wrote: I could create like a Game Manager class where I could initialize all variables in the game that require hit detection. Is that what I should do?

Yes.

Plus that still doesn't solve the issue of how to affect the health state in the Player and Enemy class....

Give the player and enemy public health variables or helper methods and then just change their health.

// update method
if player collides with enemy
   player.hp -= 10;
   player.play(hurtAnimation);
   enemy.die();
   points -= 5;
Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 22
Game Developer
Response to As3 Oop...help Me Understand.... 2013-09-18 23:19:34 Reply

// update method
if player collides with enemy
player.hp -= 10;
player.play(hurtAnimation);
enemy.die();
points -= 5;

Thanks MSGhero. I am going to have to make a little demo game and test this out. Hopefully I can figure it out!

As always I appreciate your help :)


BBS Signature
Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to As3 Oop...help Me Understand.... 2013-09-19 12:08:04 Reply

Basically you want a "manager" class to take care of things such as collision detection and the like. To prevent these classes from turning into 600+ lines monstrosities you can use helper classes.

For example your gameManager class could have a CollisionDectection class. Your CollisionDectection class might have a CollisionResolver helperclass etc.

Still for bigger games this might not be enough. Once you advance a bit further in OOP you'll discover ways to create a class for each collision algorithm, and for each collision match-up. Creating potential for seamless scaling is important here.

Composition is the most important OOP concept to understand if you really want to get into using helper classes.