Phaser学习笔记

资源加载

Phaser.Loader

Loader控制器用于加载外部的内容如:图片,精灵图,以及数据文件。


pack()


    game.load.pack('level1', 'assets/asset-pack1.json', null, this);
    game.load.image('test', 'assets/sprites/ilkke.png');
    game.load.text('html', 'http://phaser.io');
    game.load.tilemap('mario', 'assets/tilemaps/maps/super_mario.json', null, Phaser.Tilemap.TILED_JSON);
    game.load.audio('sfx', [ 'assets/audio/SoundEffects/squit.mp3', 'assets/audio/SoundEffects/squit.ogg' ]);
    //  As with all load operations the first parameter is a unique key, which must be unique between all image files.
    //  Next is the bitmap font file itself, in this case desyrel.png
    //  Finally is the path to the XML file that goes with the font.
     //  Note that the XML file should be saved with UTF-8 encoding or some browsers (such as Firefox) won't load it.

    //  There are various tools that can create Bitmap Fonts and the XML file needed.
    //  On Windows you can use the free app BMFont: http://www.angelcode.com/products/bmfont/
    //  On OS X we recommend Glyph Designer: http://www.71squared.com/en/glyphdesigner
    game.load.bitmapFont('desyrel', 'assets/fonts/desyrel.png', 'assets/fonts/desyrel.xml');

    game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71);


     //  Phaser can load texture atlas files that use the Starling XML file format.

    //  As with all load operations the first parameter is a unique key, which must be unique between all image files.

    //  Next is the texture atlas itself, in this case seacreatures.png
    //  Finally is the path to the XML file that goes with the atlas.
    game.load.atlasXML('seacreatures', 'assets/sprites/seacreatures.png', 'assets/sprites/seacreatures.xml');


     //  Phaser can load Texture Atlas files that use either JSON Hash or JSON Array format.

    //  As with all load operations the first parameter is a unique key, which must be unique between all image files.

    //  Next is the texture atlas itself, in this case seacreatures.png

    //  Finally is the path to the JSON file that goes with the atlas.
    game.load.atlas('seacreatures', 'assets/sprites/seacreatures_json.png', 'assets/sprites/seacreatures_json.json');

    //  Note that the JSON file should be saved with UTF-8 encoding or some browsers (such as Firefox) won't load it.


//  Tilemaps are split into two parts: The actual map data (usually stored in a CSV or JSON file) 
    //  and the tileset/s used to render the map.

    //  Here we'll load the tilemap data. The first parameter is a unique key for the map data.

    //  The second is a URL to the JSON file the map data is stored in. This is actually optional, you can pass the JSON object as the 3rd
    //  parameter if you already have it loaded (maybe via a 3rd party source or pre-generated). In which case pass 'null' as the URL and
    //  the JSON object as the 3rd parameter.

    //  The final one tells Phaser the foramt of the map data, in this case it's a JSON file exported from the Tiled map editor.
    //  This could be Phaser.Tilemap.CSV too.

    game.load.tilemap('mario', 'assets/tilemaps/maps/super_mario.json', null, Phaser.Tilemap.TILED_JSON);

监听事件

 //    You can listen for each of these events from Phaser.Loader
    game.load.onLoadStart.add(loadStart, this);
    game.load.onFileComplete.add(fileComplete, this);
    game.load.onLoadComplete.add(loadComplete, this);