Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Apiheld

I do have Constants in my game. Where do I put them such that I have access to them while the game loads up? They are limited to the game's scope.

Example: As a warmup exercise, I implement Tetris. I'm not sure if I go with 16x16 or 32x32 tiles. Anyways, it's always good to have as few fixed numbers in your code as possible. To be able to scale everything up later, I need a Constant called TILESIZE. However, the size of a block depends on the TILESIZE, like so:

size: { x: ig.game.TILESIZE, y: ig.game.TILESIZE},

So, the TILESIZE needs to be known by EntityBlock as soon as possible.

I read in some thread that you can put global variables in the game scope. However, if I run my game, it complains "Cannot read property 'TILESIZE' of null", meaning, that ig.game is not known to EntityBlock at first.

Two questions:

1. Where to put Constants generally speaking?
2. Is there a better way to give the game a specific TILESIZE except for putting it into a Constant?

1 decade ago by vincentpiel

Here's what i did for this issue :
I have a module, named CONSTANT.js, in which i define a simple js object containing all my constants.
In fact, some of them must be computed, so it looks like (from the top of my head) :

 module ('game.CONSTANTS')
 .defines( function() {
  
    window.CONSTANTS = {
         someWidth : 16,
         someHeight : 16, 
         someArea : -1 ,

         ...
    }
   
   var ct = CONSTANTS;
   ct.someArea = ct.someWidth * ct.someHeight;
   
    ...
 });


now i require this module in the main.js, and then i have access to CONSTANTS within all my game.

1 decade ago by Apiheld

Thank you, that helped. Although I wonder if there's a way to attach Constants to the game's scope and not to the window object. But since it won't be that many anyways, I probably won't care ;)

1 decade ago by Joncom

If you want them in ig.game then add them within your game definition in main.js rather than in a separate module. Or if you want them in a separate module but not in window, you can just define constants like this:
ig.CONSTANTS = ...

instead of like this:
window.CONSTANTS = ...
Page 1 of 1
« first « previous next › last »