Sign in to follow this  
enpu

Creating custom loader tutorial

Recommended Posts

Necro bump. :) @enpu

I don't know if this is a bug or not, but the first addAsset logo is actually necessary for this to work. If you don't call this first (i.e. call addAsset at least once) then all the other addAssets don't work. Well, I don't know if they don't work or not, but I can't use them in-game.

I didn't do any deep debugging into why this is, but this is a curious FYI. I guess the first call sets up some kind of state (global state??) which is used in subsequent calls, so if the first addAsset is tucked inside the scope of a local method, all the assets are loaded within that scope. 

 

game.module(
    'game.main'
)
.body(function() {
    
// Load loader assets
game.addAsset('logo.png'); // <--This one.

game.createClass('PreLoader', 'Loader', {
    init: function() {
    },
    
    onComplete: function() {
        // Load game assets
        game.addAsset('background.jpg');
        game.addAsset('player.png');
        game.addAsset('enemy.png');

        // Set default loader to MyLoader
        game.System.loader = 'MyLoader';
        // Load start scene with the default loader
        game.system.loadScene(game.System.startScene);
    }
});

game.createClass('MyLoader', 'Loader', {
    init: function() {
        this.logo = new game.Sprite('logo.png');
        this.logo.center(this.stage);
        this.logo.addTo(this.stage);
        this.logo.alpha = 0;
    },
    
    onProgress: function() {
        this.logo.alpha = this.percent / 100;
    }
});

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

});

 

Edit: Otherwise, great example!

Share this post


Link to post
Share on other sites

That's.. I didn't think of that. Fair enough. :D

It's an interesting FYI. I'm trying to think of a scenario where I'd want to do a Pre-loading-type action that wouldn't involve loading assets. (Maybe some kind of websocket/network set up? )

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.