Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.23 / 5.00 3,881 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.93 / 5.00 4,634 ViewsStill 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.
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; // 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 :)
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.