Reputation: 29
I have a class designed to display a text pop up in game and I'm going to use this for two different situations:
For the "-1" I want the text to appear and then I want it to fade off screen therefore I'm reducing the color values and alpha to zero and shifting it's position slightly and then later deleting the object/text popup.
For the "+25" I want to move this text popup over to an icon of gold coins and then delete.
My question is, since I'm using this class for two different events, both of which would have different logic to execute, how best would I go about differentiating between what text pop up I'm displaying and therefore what .
I thought of maybe having a switch/if statement using a string.
E.g
string for -1 would be "enemyLived"
string for "+25" would be "goldPickup"
and I would check
if (this.identity == "enemyLived")
do logic
else (this.identity =="goldPickup")
do logic
But I was curious if a more experienced coder had a suggestion for a "better" designed check.
Upvotes: 1
Views: 100
Reputation: 4860
Sounds like a classic case for OOD and use of inheritance to solve this problem.
Create an interface that describes method for general action, e.g. PopupTransition()
, then have two classes, one for negative points, the other positive. Have each one provide their own implementation on how transition occurs (fading or moving or whatever). When event occurs, instantiate the respective type, and pass it to your general handler, who doesn't care what event it is, and will just call PopupTransition
at the right moment. You can easily extend it if you suddenly want to support other transitions..
Upvotes: 6