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 Yojimbo

I created a main menu, but it's a separate game class from the actual game class. For development purposes, I'm just keeping it as a simple level select menu, but right now I have it so when you select a level, it will set a variable that's a scope above both classes, and the new game will load the level based on that variable.

I want to remove that variable and be able to pass an initial level to load to the actual game as it's initializing. Without changing Impact's code, is there a way I can do this that's already built in?

Example:

ig.module(
        'game.main'
    )
    .requires(
        //Derp stuff derp
    )
    .defines(function () {

        var levelToLoad = null;
        // These are two actions that are defined 
        // for the buttons that are both on the main menu
        // as well as the in-game menu for loading other levels.
        var actBtnCityScape = function(){
            levelToLoad = LevelFirstdraft;
            if(this.highlighted) {
                ig.system.setGame(MyGame);
            }
        };
        var actBtnAnimEx = function() {
            levelToLoad = LevelAnimationexamples;
            if(this.highlighted) {
                ig.system.setGame(MyGame);
            }
        };

        var MainMenu = ig.Game.extend({

            update: function() {
                // Update all entities and backgroundMaps
                this.parent();
                // Update the GUI Master
                updateMainMenu.bind(this)();
                ig.guimaster.update(); 

                /* My 'GUI Master' is what handles which level is being 
                selected and passes that to the 'levelToLoad' variable above.  
                All the other drawing and init jazz is in there */

            }
        });
        var MyGame = ig.Box2DGame.extend({
        //... stuff ...
            init: function () {
                Box2D.SCALE = 0.05;
                // Initial Level/Screen Load
                this.loadLevel(levelToLoad);
        //... Other stuff ...

Could I instantiate the game object, give it a new property of `levelToLoad` and pass it along?

Edit: Hmm... Well, I modified the prototype for the game:

MyGame.prototype._levelToLoad = LevelFirstdraft;

And that's how I'm handling it at the moment. I still feel like it's inappropriate to rely on modifying the prototype, though. I guess it doesn't matter too much, since it's only for development right now.

1 decade ago by Joncom

I was about to suggest modifying the prototype, but I see you are already doing so. There's nothing wrong with that.

1 decade ago by vincentpiel

Most easy way is to have your namespace, in which you put all your shared stuff :

    window.superJumper = { } ;

    superJumper.level    = 0 ;
    superJumper.money = 200 ; 
    superJumper.life       = 3 ;


So you can read or write any value from anywhere in your code

Rq : you might first initialize the values with stored values (in localStorage or coockie, or using file API, or from a server).
Page 1 of 1
« first « previous next › last »