Modules/components for re-usable code

What is the best way that I can go about creating separate files for code that I keep re-typing for each level such as player movement, power-up behavior, and background animations/movement? If I could just create a single file for player movement and then reference or call a single function that invokes all of the player movement and controls that would be awesome.

I’ve tried creating new classes and referencing functions in them from file to file but it doesn’t seem to like that at all. I’ve also tried using export/import for modules but I guess I just don’t know enough about how that should work. I’m running an HTTP server with xampp so I should be able to utilize this.

My code is just getting really heavy and I’d like to get it clean before I get heavy into more level creation.

Hi Benstrocity,

I used this tutorial as a basis to learn about using import and export. it gave me an understanding on how I could split my code up into reusable modules such as a player class.

The code is here

Maybe this could help? :slight_smile:

I appreciate that, it will definitely come in handy. I’m feeling pretty dumb though because I think I need even one step down. I can’t figure out how to get my code to recognize imports/exports. Anytime I utilize those it breaks and essentially says I’m not allowed to export defaults of the class I’m working on.

Do I need some special setup in order to access the export/import features?

Using the structure from the tutorial, I created an extremely basic template using import and export that could help. If not do you have any code that we could take a look at? :slight_smile:


1 Like

You sir just helped me to understand how modules work in javascript. You have no idea how excited this makes me. I feel like the flood gates of opportunity just opened for me. Thank you very much for your assistance! It’s all working now and I’m able to break my code up into separate files to be more re-usable!

1 Like