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 aliken9

I was wondering if there is any preferred convention for working with Impact.

For example, examples I've seen most typically declares and instantiates all its variables as well as functions definitions within ig.Entity.extend({}) like so:

.defines(function() {
	EntityButton = ig.Entity.extend({

		font1: new ig.Font('...'),
		font2: new ig.Font('...'),	
		x: 0,
		y: 0,

		init: function(x, y, settings) {
			...
		},
		
		process: function() {
			...
		},

		...

I've also seen code where variables and function are declared/defined before the ig.Entity.extend({}), for example:

.defines(function(){
    var spaceStr = "\nPress SPACE to continue!";
    var str = "Hello there!";

    function arrayShuffle(theArray) {
       ...
    }

    //initialize all questions
    function initQuizzes(){
        ...
    };

    EntityQuizBox = ig.Entity.extend({
        quizText: new ig.Font('...'),

        size:{x:48, y:48},

        init: function( x, y, settings ){
            ...
        },

        update: function(){
            ...
        },

The rationale for the latter style being that there are some functions that should be "private" to the entity and not modifiable by external code.

From my experience there does not seem to be an issue with using either of these styles, but I was wondering if there is a convention for doing either the first or the second method, and what is the rationale behind that?

1 decade ago by Joncom

So far as I can tell, the entity in your second example still has access to initQuizzes, arrayShuffle, spaceStr, and str. What's private about doing it this way?

Personally I use the first method, only because that's how I learned, and I see no benefit yet to doing it differently.

1 decade ago by aliken9

"Private" in the sense that when entities gets spawned from outside code using ig.game.spawnEntity(), their initQuizzes, arrayShuffle, spaceStr functions (hopefully) cannot be overwritten. In the first example, I can override the process function when defining settings.

I think of the latter examples as private methods in java, they are visible only within the object and generally cannot be called/modified from other objects/contexts. Perhaps the transition from a strongly typed to a loosely typed environment made me really unsettled...
Page 1 of 1
« first « previous next › last »