Making 2 scripts to not adjust lazer starting position

Hi there,

When i took the challenge in the beginning of the video (make enemies shoot). I made a second script and class for the enemy lazers. So I never had any problems with being blown up by my own lazer or having my enemies blow themselves up.

Could this approach hurt me in the long run? What issues could i face with this?

Hi Malith,

The initial thought here is that you will invariably be repeating some of your functionality. Typically, if you can identify repeated behaviour you are better off breaking it down into a more generic class that anything could use.

In the case of both the enemies and the player, they will both fire something, that something will have a speed and a direction, it will most likely have an associated sprite. Note, the sprite doesn’t need to be the same, but the fact that they will both have a sprite is what we are looking at there.

When you start to see this commonality, it is often time to refactor. If you still need uniqueness for specific classes, you could consider creating a base class which the other classes would inherit from.

The benefit of this approach becomes more obvious when you need to make changes. In the case of a couple of projectiles it’s not perhaps so obvious. If however you consider having a class for different types of car in your game. Lets say there are 50 different types of car. You have created a class for each one. Now you want to add a property which returns the numbers of doors each type of car has. You will be making changes for some time :slight_smile:

If however, those different models of car were to inherit from a car base-class which provided common/generic functionality that all cars share, you could add the new property to the base class and all of the classes deriving from it would also have that property.

Hope this helps :slight_smile:

Privacy & Terms