Creating new module

If your project has a lots of different classes or you want to share some classes in multiple projects, it might be a good idea to separate them into own modules. Your project can load as many modules as you want.

Creating new module is really simple. First switch to modules tab.

Then click on the small plus button in the bottom of the sidebar.

Then name your new module, for example player. Then click OK.

Now you should see the basic syntax of Panda 2 module:

game.module(
    'game.player'
)
.body(function() {



});

Now let's create new class inside our new module. Click back to the Classes tab.

Then click on the empty line number 6 and then the small plus button again.

Name the new class Player and hit Save. Your module should now look like this:

game.module(
    'game.player'
)
.body(function() {

game.createClass('Player', {
    init: function() {
        
    }
});

});

If you switch back to the Modules tab, you should see your new game.player module there.

Using your module

To use your new module, we need to require it from another module. Click back to the game.main module. Require your module with require function in the top of your module.

game.module(
    'game.main'
)
.require(
    'game.player'
)
.body(function() {

game.addAsset('panda.png');

game.createScene('Main', {
    init: function() {
        var sprite = new game.Sprite('panda.png');
        sprite.addTo(this.stage);
    }
});

});

Now hit Save and you should be able to use your Player class from the game.player module inside your game.main module.

game.module(
    'game.main'
)
.require(
    'game.player'
)
.body(function() {

game.addAsset('panda.png');

game.createScene('Main', {
    init: function() {
        var sprite = new game.Sprite('panda.png');
        sprite.addTo(this.stage);
        
        var player = new game.Player();
    }
});

});