new LoaderPlugin(scene)
Parameters:
Name | Type | Description |
---|---|---|
scene |
Phaser.Scene | The Scene which owns this Loader instance. |
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 18)
Extends
Members
-
baseURL :string
-
If you want to append a URL before the path of any asset you can set this here.
Useful if allowing the asset base url to be configured outside of the game code.
If you set this property directly then it must end with a "/". Alternatively, call
setBaseURL()
and it'll do it for you.Type:
- string
- Since: 3.0.0
- Default Value:
-
- ''
- Source: src/loader/LoaderPlugin.js (Line 152)
-
cacheManager :Phaser.Cache.CacheManager
-
A reference to the global Cache Manager.
Type:
- Since: 3.7.0
- Source: src/loader/LoaderPlugin.js (Line 83)
-
crossOrigin :string
-
The crossOrigin value applied to loaded images. Very often this needs to be set to 'anonymous'.
Type:
- string
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 201)
-
inflight :Phaser.Structs.Set.<Phaser.Loader.File>
-
Files are stored in this Set while they're in the process of being loaded.
Upon a successful load they are moved to the
queue
Set.By the end of the load process this Set will be empty.
Type:
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 247)
-
list :Phaser.Structs.Set.<Phaser.Loader.File>
-
Files are placed in this Set when they're added to the Loader via
addFile
.They are moved to the
inflight
Set when they start loading, and assuming a successful load, to thequeue
Set for further processing.By the end of the load process this Set will be empty.
Type:
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 233)
-
maxParallelDownloads :number
-
The number of concurrent / parallel resources to try and fetch at once.
Old browsers limit 6 requests per domain; modern ones, especially those with HTTP/2 don't limit it at all.
The default is 32 but you can change this in your Game Config, or by changing this property before the Loader starts.
Type:
- number
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 172)
-
path :string
-
The value of
path
, if set, is placed before any relative file path given. For example:this.load.path = "images/sprites/"; this.load.image("ball", "ball.png"); this.load.image("tree", "level1/oaktree.png"); this.load.image("boom", "http://server.com/explode.png");
Would load the
ball
file fromimages/sprites/ball.png
and the tree fromimages/sprites/level1/oaktree.png
but the fileboom
would load from the URL given as it's an absolute URL.Please note that the path is added before the filename but after the baseURL (if set.)
If you set this property directly then it must end with a "/". Alternatively, call
setPath()
and it'll do it for you.Type:
- string
- Since: 3.0.0
- Default Value:
-
- ''
- Source: src/loader/LoaderPlugin.js (Line 127)
-
prefix :string
-
An optional prefix that is automatically prepended to the start of every file key. If prefix was
MENU.
and you load an image with the key 'Background' the resulting key would beMENU.Background
. You can set this directly, or callLoader.setPrefix()
. It will then affect every file added to the Loader from that point on. It does not change any file already in the load queue.Type:
- string
- Since: 3.7.0
- Default Value:
-
- ''
- Source: src/loader/LoaderPlugin.js (Line 114)
-
progress :number
-
The progress of the current load queue, as a float value between 0 and 1. This is updated automatically as files complete loading. Note that it is possible for this value to go down again if you add content to the current load queue during a load.
Type:
- number
- Since: 3.0.0
- Default Value:
-
- 0
- Source: src/loader/LoaderPlugin.js (Line 221)
-
queue :Phaser.Structs.Set.<Phaser.Loader.File>
-
Files are stored in this Set while they're being processed.
If the process is successful they are moved to their final destination, which could be a Cache or the Texture Manager.
At the end of the load process this Set will be empty.
Type:
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 260)
-
scene :Phaser.Scene
-
The Scene which owns this Loader instance.
Type:
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 65)
-
<protected> sceneManager :Phaser.Scenes.SceneManager
-
A reference to the global Scene Manager.
Type:
- Since: 3.16.0
- Source: src/loader/LoaderPlugin.js (Line 101)
-
<readonly> state :number
-
The current state of the Loader.
Type:
- number
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 307)
-
systems :Phaser.Scenes.Systems
-
A reference to the Scene Systems.
Type:
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 74)
-
textureManager :Phaser.Textures.TextureManager
-
A reference to the global Texture Manager.
Type:
- Since: 3.7.0
- Source: src/loader/LoaderPlugin.js (Line 92)
-
totalComplete :number
-
The total number of files that successfully loaded during the most recent load. This value is reset when you call
Loader.start
.Type:
- number
- Since: 3.7.0
- Default Value:
-
- 0
- Source: src/loader/LoaderPlugin.js (Line 296)
-
totalFailed :number
-
The total number of files that failed to load during the most recent load. This value is reset when you call
Loader.start
.Type:
- number
- Since: 3.7.0
- Default Value:
-
- 0
- Source: src/loader/LoaderPlugin.js (Line 285)
-
totalToLoad :number
-
The total number of files to load. It may not always be accurate because you may add to the Loader during the process of loading, especially if you load a Pack File. Therefore this value can change, but in most cases remains static.
Type:
- number
- Since: 3.0.0
- Default Value:
-
- 0
- Source: src/loader/LoaderPlugin.js (Line 210)
-
xhr :Phaser.Types.Loader.XHRSettingsObject
-
xhr specific global settings (can be overridden on a per-file basis)
Type:
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 185)
Methods
-
addFile(file)
-
Adds a file, or array of files, into the load queue.
The file must be an instance of
Phaser.Loader.File
, or a class that extends it. The Loader will check that the key used by the file won't conflict with any other key either in the loader, the inflight queue or the target cache. If allowed it will then add the file into the pending list, read for the load to start. Or, if the load has already started, ready for the next batch of files to be pulled from the list to the inflight queue.You should not normally call this method directly, but rather use one of the Loader methods like
image
oratlas
, however you can call this as long as the file given to it is well formed.Parameters:
Name Type Description file
Phaser.Loader.File | Array.<Phaser.Loader.File> The file, or array of files, to be added to the load queue.
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 475)
Fires:
-
addListener(event, fn [, context])
-
Add a listener for a given event.
Parameters:
Name Type Argument Default Description event
string | symbol The event name.
fn
function The listener function.
context
* <optional>
this The context to invoke the listener with.
- Since: 3.0.0
- Inherited From:
- Source: src/events/EventEmitter.js (Line 111)
Returns:
this
. -
addPack(pack [, packKey])
-
Takes a well formed, fully parsed pack file object and adds its entries into the load queue. Usually you do not call this method directly, but instead use
Loader.pack
and supply a path to a JSON file that holds the pack data. However, if you've got the data prepared you can pass it to this method.You can also provide an optional key. If you do then it will only add the entries from that part of the pack into to the load queue. If not specified it will add all entries it finds. For more details about the pack file format see the
LoaderPlugin.pack
method.Parameters:
Name Type Argument Description pack
any The Pack File data to be parsed and each entry of it to added to the load queue.
packKey
string <optional>
An optional key to use from the pack file data.
- Since: 3.7.0
- Source: src/loader/LoaderPlugin.js (Line 577)
Returns:
true
if any files were added to the queue, otherwisefalse
.- Type
- boolean
-
animation(key [, url] [, dataKey] [, xhrSettings])
-
Adds an Animation JSON Data file, or array of Animation JSON files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.animation('baddieAnims', 'files/BaddieAnims.json'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.If you call this from outside of
preload
then you are responsible for starting the Loader afterwards and monitoring its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.The key must be a unique String. It is used to add the file to the global JSON Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the JSON Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the JSON Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.animation({ key: 'baddieAnims', url: 'files/BaddieAnims.json' });
See the documentation for
Phaser.Types.Loader.FileTypes.JSONFileConfig
for more details.Once the file has finished loading it will automatically be passed to the global Animation Managers
fromJSON
method. This will parse all of the JSON data and create animation data from it. This process happens at the very end of the Loader, once every other file in the load queue has finished. The reason for this is to allow you to load both animation data and the images it relies upon in the same load call.Once the animation data has been parsed you will be able to play animations using that data. Please see the Animation Manager
fromJSON
method for more details about the format and playback.You can also access the raw animation data from its Cache using its key:
this.load.animation('baddieAnims', 'files/BaddieAnims.json'); // and later in your game ... var data = this.cache.json.get('baddieAnims');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasLEVEL1.
and the key wasWaves
the final key will beLEVEL1.Waves
and this is what you would use to retrieve the text from the JSON Cache.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "data" and no URL is given then the Loader will set the URL to be "data.json". It will always add
.json
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.You can also optionally provide a
dataKey
to use. This allows you to extract only a part of the JSON and store it in the Cache, rather than the whole file. For example, if your JSON data had a structure like this:{ "level1": { "baddies": { "aliens": {}, "boss": {} } }, "level2": {}, "level3": {} }
And if you only wanted to create animations from the
boss
data, then you could passlevel1.baddies.boss
as thedataKey
.Note: The ability to load this type of file will only be available if the JSON File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.JSONFileConfig | Array.<Phaser.Types.Loader.FileTypes.JSONFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json".dataKey
string <optional>
When the Animation JSON file loads only this property will be stored in the Cache and used to create animation data.
xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/AnimationJSONFile.js (Line 77)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
aseprite(key [, textureURL] [, atlasURL] [, textureXhrSettings] [, atlasXhrSettings])
-
Aseprite is a powerful animated sprite editor and pixel art tool.
You can find more details at https://www.aseprite.org/
Adds a JSON based Aseprite Animation, or array of animations, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.aseprite('gladiator', 'images/Gladiator.png', 'images/Gladiator.json'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.If you call this from outside of
preload
then you are responsible for starting the Loader afterwards and monitoring its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.To export a compatible JSON file in Aseprite, please do the following:
-
Go to "File - Export Sprite Sheet"
-
On the Layout tab: 2a. Set the "Sheet type" to "Packed" 2b. Set the "Constraints" to "None" 2c. Check the "Merge Duplicates" checkbox
-
On the Sprite tab: 3a. Set "Layers" to "Visible layers" 3b. Set "Frames" to "All frames", unless you only wish to export a sub-set of tags
-
On the Borders tab: 4a. Check the "Trim Sprite" and "Trim Cells" options 4b. Ensure "Border Padding", "Spacing" and "Inner Padding" are all > 0 (1 is usually enough)
-
On the Output tab: 5a. Check "Output File", give your image a name and make sure you choose "png files" as the file type 5b. Check "JSON Data" and give your json file a name 5c. The JSON Data type can be either a Hash or Array, Phaser doesn't mind. 5d. Make sure "Tags" is checked in the Meta options 5e. In the "Item Filename" input box, make sure it says just "{frame}" and nothing more.
-
Click export
This was tested with Aseprite 1.2.25.
This will export a png and json file which you can load using the Aseprite Loader, i.e.:
Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle.
The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Texture Manager. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Texture Manager first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.aseprite({ key: 'gladiator', textureURL: 'images/Gladiator.png', atlasURL: 'images/Gladiator.json' });
See the documentation for
Phaser.Types.Loader.FileTypes.AsepriteFileConfig
for more details.Instead of passing a URL for the JSON data you can also pass in a well formed JSON object instead.
Once loaded, you can call this method from within a Scene with the 'atlas' key:
this.anims.createFromAseprite('paladin');
Any animations defined in the JSON will now be available to use in Phaser and you play them via their Tag name. For example, if you have an animation called 'War Cry' on your Aseprite timeline, you can play it in Phaser using that Tag name:
this.add.sprite(400, 300).play('War Cry');
When calling this method you can optionally provide an array of tag names, and only those animations will be created. For example:
this.anims.createFromAseprite('paladin', [ 'step', 'War Cry', 'Magnum Break' ]);
This will only create the 3 animations defined. Note that the tag names are case-sensitive.
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasMENU.
and the key wasBackground
the final key will beMENU.Background
and this is what you would use to retrieve the image from the Texture Manager.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.png". It will always add
.png
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the Aseprite File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.AsepriteFileConfig | Array.<Phaser.Types.Loader.FileTypes.AsepriteFileConfig> The key to use for this file, or a file configuration object, or array of them.
textureURL
string | Array.<string> <optional>
The absolute or relative URL to load the texture image file from. If undefined or
null
it will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".atlasURL
object | string <optional>
The absolute or relative URL to load the texture atlas json data file from. If undefined or
null
it will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json". Or, a well formed JSON object.textureXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the atlas image file. Used in replacement of the Loaders default XHR Settings.
atlasXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the atlas json file. Used in replacement of the Loaders default XHR Settings.
- Since: 3.50.0
- Source: src/loader/filetypes/AsepriteFile.js (Line 111)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
-
atlas(key [, textureURL] [, atlasURL] [, textureXhrSettings] [, atlasXhrSettings])
-
Adds a JSON based Texture Atlas, or array of atlases, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.atlas('mainmenu', 'images/MainMenu.png', 'images/MainMenu.json'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.If you call this from outside of
preload
then you are responsible for starting the Loader afterwards and monitoring its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.Phaser expects the atlas data to be provided in a JSON file, using either the JSON Hash or JSON Array format. These files are created by software such as Texture Packer, Shoebox and Adobe Flash / Animate. If you are using Texture Packer and have enabled multi-atlas support, then please use the Phaser Multi Atlas loader instead of this one.
Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle.
The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Texture Manager. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Texture Manager first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.atlas({ key: 'mainmenu', textureURL: 'images/MainMenu.png', atlasURL: 'images/MainMenu.json' });
See the documentation for
Phaser.Types.Loader.FileTypes.AtlasJSONFileConfig
for more details.Instead of passing a URL for the atlas JSON data you can also pass in a well formed JSON object instead.
Once the atlas has finished loading you can use frames from it as textures for a Game Object by referencing its key:
this.load.atlas('mainmenu', 'images/MainMenu.png', 'images/MainMenu.json'); // and later in your game ... this.add.image(x, y, 'mainmenu', 'background');
To get a list of all available frames within an atlas please consult your Texture Atlas software.
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasMENU.
and the key wasBackground
the final key will beMENU.Background
and this is what you would use to retrieve the image from the Texture Manager.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.png". It will always add
.png
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Phaser also supports the automatic loading of associated normal maps. If you have a normal map to go with this image, then you can specify it by providing an array as the
url
where the second element is the normal map:this.load.atlas('mainmenu', [ 'images/MainMenu.png', 'images/MainMenu-n.png' ], 'images/MainMenu.json');
Or, if you are using a config object use the
normalMap
property:this.load.atlas({ key: 'mainmenu', textureURL: 'images/MainMenu.png', normalMap: 'images/MainMenu-n.png', atlasURL: 'images/MainMenu.json' });
The normal map file is subject to the same conditions as the image file with regard to the path, baseURL, CORs and XHR Settings. Normal maps are a WebGL only feature.
Note: The ability to load this type of file will only be available if the Atlas JSON File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.AtlasJSONFileConfig | Array.<Phaser.Types.Loader.FileTypes.AtlasJSONFileConfig> The key to use for this file, or a file configuration object, or array of them.
textureURL
string | Array.<string> <optional>
The absolute or relative URL to load the texture image file from. If undefined or
null
it will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".atlasURL
object | string <optional>
The absolute or relative URL to load the texture atlas json data file from. If undefined or
null
it will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json". Or, a well formed JSON object.textureXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the atlas image file. Used in replacement of the Loaders default XHR Settings.
atlasXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the atlas json file. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/AtlasJSONFile.js (Line 109)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
atlasXML(key [, textureURL] [, atlasURL] [, textureXhrSettings] [, atlasXhrSettings])
-
Adds an XML based Texture Atlas, or array of atlases, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.atlasXML('mainmenu', 'images/MainMenu.png', 'images/MainMenu.xml'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.If you call this from outside of
preload
then you are responsible for starting the Loader afterwards and monitoring its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.Phaser expects the atlas data to be provided in an XML file format. These files are created by software such as Shoebox and Adobe Flash / Animate.
Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle.
The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Texture Manager. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Texture Manager first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.atlasXML({ key: 'mainmenu', textureURL: 'images/MainMenu.png', atlasURL: 'images/MainMenu.xml' });
See the documentation for
Phaser.Types.Loader.FileTypes.AtlasXMLFileConfig
for more details.Once the atlas has finished loading you can use frames from it as textures for a Game Object by referencing its key:
this.load.atlasXML('mainmenu', 'images/MainMenu.png', 'images/MainMenu.xml'); // and later in your game ... this.add.image(x, y, 'mainmenu', 'background');
To get a list of all available frames within an atlas please consult your Texture Atlas software.
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasMENU.
and the key wasBackground
the final key will beMENU.Background
and this is what you would use to retrieve the image from the Texture Manager.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.png". It will always add
.png
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Phaser also supports the automatic loading of associated normal maps. If you have a normal map to go with this image, then you can specify it by providing an array as the
url
where the second element is the normal map:this.load.atlasXML('mainmenu', [ 'images/MainMenu.png', 'images/MainMenu-n.png' ], 'images/MainMenu.xml');
Or, if you are using a config object use the
normalMap
property:this.load.atlasXML({ key: 'mainmenu', textureURL: 'images/MainMenu.png', normalMap: 'images/MainMenu-n.png', atlasURL: 'images/MainMenu.xml' });
The normal map file is subject to the same conditions as the image file with regard to the path, baseURL, CORs and XHR Settings. Normal maps are a WebGL only feature.
Note: The ability to load this type of file will only be available if the Atlas XML File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.AtlasXMLFileConfig | Array.<Phaser.Types.Loader.FileTypes.AtlasXMLFileConfig> The key to use for this file, or a file configuration object, or array of them.
textureURL
string | Array.<string> <optional>
The absolute or relative URL to load the texture image file from. If undefined or
null
it will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".atlasURL
string <optional>
The absolute or relative URL to load the texture atlas xml data file from. If undefined or
null
it will be set to<key>.xml
, i.e. ifkey
was "alien" then the URL will be "alien.xml".textureXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the atlas image file. Used in replacement of the Loaders default XHR Settings.
atlasXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the atlas xml file. Used in replacement of the Loaders default XHR Settings.
- Since: 3.7.0
- Source: src/loader/filetypes/AtlasXMLFile.js (Line 107)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
audio(key [, urls] [, config] [, xhrSettings])
-
Adds an Audio or HTML5Audio file, or array of audio files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.audio('title', [ 'music/Title.ogg', 'music/Title.mp3', 'music/Title.m4a' ]); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global Audio Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Audio Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Audio Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.audio({ key: 'title', url: [ 'music/Title.ogg', 'music/Title.mp3', 'music/Title.m4a' ] });
See the documentation for
Phaser.Types.Loader.FileTypes.AudioFileConfig
for more details.The URLs can be relative or absolute. If the URLs are relative the
Loader.baseURL
andLoader.path
values will be prepended to them.Due to different browsers supporting different audio file types you should usually provide your audio files in a variety of formats. ogg, mp3 and m4a are the most common. If you provide an array of URLs then the Loader will determine which one file to load based on browser support.
If audio has been disabled in your game, either via the game config, or lack of support from the device, then no audio will be loaded.
Note: The ability to load this type of file will only be available if the Audio File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.AudioFileConfig | Array.<Phaser.Types.Loader.FileTypes.AudioFileConfig> The key to use for this file, or a file configuration object, or array of them.
urls
string | Array.<string> <optional>
The absolute or relative URL to load the audio files from.
config
any <optional>
An object containing an
instances
property for HTML5Audio. Defaults to 1.xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/AudioFile.js (Line 170)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
audioSprite(key, jsonURL [, audioURL] [, audioConfig] [, audioXhrSettings] [, jsonXhrSettings])
-
Adds a JSON based Audio Sprite, or array of audio sprites, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.audioSprite('kyobi', 'kyobi.json', [ 'kyobi.ogg', 'kyobi.mp3', 'kyobi.m4a' ]); }
Audio Sprites are a combination of audio files and a JSON configuration. The JSON follows the format of that created by https://github.com/tonistiigi/audiosprite
If the JSON file includes a 'resource' object then you can let Phaser parse it and load the audio files automatically based on its content. To do this exclude the audio URLs from the load:
function preload () { this.load.audioSprite('kyobi', 'kyobi.json'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.If you call this from outside of
preload
then you are responsible for starting the Loader afterwards and monitoring its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.The key must be a unique String. It is used to add the file to the global Audio Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Audio Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Audio Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.audioSprite({ key: 'kyobi', jsonURL: 'audio/Kyobi.json', audioURL: [ 'audio/Kyobi.ogg', 'audio/Kyobi.mp3', 'audio/Kyobi.m4a' ] });
See the documentation for
Phaser.Types.Loader.FileTypes.AudioSpriteFileConfig
for more details.Instead of passing a URL for the audio JSON data you can also pass in a well formed JSON object instead.
Once the audio has finished loading you can use it create an Audio Sprite by referencing its key:
this.load.audioSprite('kyobi', 'kyobi.json'); // and later in your game ... var music = this.sound.addAudioSprite('kyobi'); music.play('title');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasMENU.
and the key wasBackground
the final key will beMENU.Background
and this is what you would use to retrieve the image from the Texture Manager.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.Due to different browsers supporting different audio file types you should usually provide your audio files in a variety of formats. ogg, mp3 and m4a are the most common. If you provide an array of URLs then the Loader will determine which one file to load based on browser support.
If audio has been disabled in your game, either via the game config, or lack of support from the device, then no audio will be loaded.
Note: The ability to load this type of file will only be available if the Audio Sprite File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.AudioSpriteFileConfig | Array.<Phaser.Types.Loader.FileTypes.AudioSpriteFileConfig> The key to use for this file, or a file configuration object, or an array of objects.
jsonURL
string The absolute or relative URL to load the json file from. Or a well formed JSON object to use instead.
audioURL
string | Array.<string> <optional>
The absolute or relative URL to load the audio file from. If empty it will be obtained by parsing the JSON file.
audioConfig
any <optional>
The audio configuration options.
audioXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the audio file. Used in replacement of the Loaders default XHR Settings.
jsonXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the json file. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/AudioSpriteFile.js (Line 143)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader.
-
binary(key [, url] [, dataType] [, xhrSettings])
-
Adds a Binary file, or array of Binary files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.binary('doom', 'files/Doom.wad'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global Binary Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Binary Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Binary Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.binary({ key: 'doom', url: 'files/Doom.wad', dataType: Uint8Array });
See the documentation for
Phaser.Types.Loader.FileTypes.BinaryFileConfig
for more details.Once the file has finished loading you can access it from its Cache using its key:
this.load.binary('doom', 'files/Doom.wad'); // and later in your game ... var data = this.cache.binary.get('doom');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasLEVEL1.
and the key wasData
the final key will beLEVEL1.Data
and this is what you would use to retrieve the text from the Binary Cache.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "doom" and no URL is given then the Loader will set the URL to be "doom.bin". It will always add
.bin
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the Binary File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.BinaryFileConfig | Array.<Phaser.Types.Loader.FileTypes.BinaryFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.bin
, i.e. ifkey
was "alien" then the URL will be "alien.bin".dataType
any <optional>
Optional type to cast the binary file to once loaded. For example,
Uint8Array
.xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/BinaryFile.js (Line 89)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
bitmapFont(key [, textureURL] [, fontDataURL] [, textureXhrSettings] [, fontDataXhrSettings])
-
Adds an XML based Bitmap Font, or array of fonts, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.bitmapFont('goldenFont', 'images/GoldFont.png', 'images/GoldFont.xml'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.If you call this from outside of
preload
then you are responsible for starting the Loader afterwards and monitoring its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.Phaser expects the font data to be provided in an XML file format. These files are created by software such as the Angelcode Bitmap Font Generator, Littera or Glyph Designer
Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle.
The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Texture Manager. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Texture Manager first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.bitmapFont({ key: 'goldenFont', textureURL: 'images/GoldFont.png', fontDataURL: 'images/GoldFont.xml' });
See the documentation for
Phaser.Types.Loader.FileTypes.BitmapFontFileConfig
for more details.Once the atlas has finished loading you can use key of it when creating a Bitmap Text Game Object:
this.load.bitmapFont('goldenFont', 'images/GoldFont.png', 'images/GoldFont.xml'); // and later in your game ... this.add.bitmapText(x, y, 'goldenFont', 'Hello World');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasMENU.
and the key wasBackground
the final key will beMENU.Background
and this is what you would use when creating a Bitmap Text object.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.png". It will always add
.png
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Phaser also supports the automatic loading of associated normal maps. If you have a normal map to go with this image, then you can specify it by providing an array as the
url
where the second element is the normal map:this.load.bitmapFont('goldenFont', [ 'images/GoldFont.png', 'images/GoldFont-n.png' ], 'images/GoldFont.xml');
Or, if you are using a config object use the
normalMap
property:this.load.bitmapFont({ key: 'goldenFont', textureURL: 'images/GoldFont.png', normalMap: 'images/GoldFont-n.png', fontDataURL: 'images/GoldFont.xml' });
The normal map file is subject to the same conditions as the image file with regard to the path, baseURL, CORs and XHR Settings. Normal maps are a WebGL only feature.
Note: The ability to load this type of file will only be available if the Bitmap Font File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.BitmapFontFileConfig | Array.<Phaser.Types.Loader.FileTypes.BitmapFontFileConfig> The key to use for this file, or a file configuration object, or array of them.
textureURL
string | Array.<string> <optional>
The absolute or relative URL to load the font image file from. If undefined or
null
it will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".fontDataURL
string <optional>
The absolute or relative URL to load the font xml data file from. If undefined or
null
it will be set to<key>.xml
, i.e. ifkey
was "alien" then the URL will be "alien.xml".textureXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the font image file. Used in replacement of the Loaders default XHR Settings.
fontDataXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the font data xml file. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/BitmapFontFile.js (Line 113)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
css(key [, url] [, xhrSettings])
-
Adds a CSS file, or array of CSS files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.css('headers', 'styles/headers.css'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String and not already in-use by another file in the Loader.
Instead of passing arguments you can pass a configuration object, such as:
this.load.css({ key: 'headers', url: 'styles/headers.css' });
See the documentation for
Phaser.Types.Loader.FileTypes.CSSFileConfig
for more details.Once the file has finished loading it will automatically be converted into a style DOM element via
document.createElement('style')
. It will have itsdefer
property set to false and then the resulting element will be appended todocument.head
. The CSS styles are then applied to the current document.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.css". It will always add
.css
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the CSS File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.CSSFileConfig | Array.<Phaser.Types.Loader.FileTypes.CSSFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.css
, i.e. ifkey
was "alien" then the URL will be "alien.css".xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.17.0
- Source: src/loader/filetypes/CSSFile.js (Line 88)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
emit(event [, args])
-
Calls each of the listeners registered for a given event.
Parameters:
Name Type Argument Description event
string | symbol The event name.
args
* <optional>
<repeatable>
Additional arguments that will be passed to the event handler.
- Since: 3.0.0
- Inherited From:
- Source: src/events/EventEmitter.js (Line 86)
Returns:
true
if the event had listeners, elsefalse
.- Type
- boolean
-
eventNames()
-
Return an array listing the events for which the emitter has registered listeners.
- Since: 3.0.0
- Inherited From:
- Source: src/events/EventEmitter.js (Line 55)
Returns:
- Type
- Array.<(string|symbol)>
-
fileProcessComplete(file)
-
An internal method that is called automatically by the File when it has finished processing.
If the process was successful, and the File isn't part of a MultiFile, its
addToCache
method is called.It this then removed from the queue. If there are no more files to load
loadComplete
is called.Parameters:
Name Type Description file
Phaser.Loader.File The file that has finished processing.
- Since: 3.7.0
- Source: src/loader/LoaderPlugin.js (Line 847)
-
flagForRemoval(file)
-
Adds a File into the pending-deletion queue.
Parameters:
Name Type Description file
Phaser.Loader.File The File to be queued for deletion when the Loader completes.
- Since: 3.7.0
- Source: src/loader/LoaderPlugin.js (Line 936)
-
glsl(key [, url] [, shaderType] [, xhrSettings])
-
Adds a GLSL file, or array of GLSL files, to the current load queue. In Phaser 3 GLSL files are just plain Text files at the current moment in time.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.glsl('plasma', 'shaders/Plasma.glsl'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global Shader Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Shader Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Shader Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.glsl({ key: 'plasma', shaderType: 'fragment', url: 'shaders/Plasma.glsl' });
See the documentation for
Phaser.Types.Loader.FileTypes.GLSLFileConfig
for more details.Once the file has finished loading you can access it from its Cache using its key:
this.load.glsl('plasma', 'shaders/Plasma.glsl'); // and later in your game ... var data = this.cache.shader.get('plasma');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasFX.
and the key wasPlasma
the final key will beFX.Plasma
and this is what you would use to retrieve the text from the Shader Cache.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "plasma" and no URL is given then the Loader will set the URL to be "plasma.glsl". It will always add
.glsl
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the GLSL File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Default Description key
string | Phaser.Types.Loader.FileTypes.GLSLFileConfig | Array.<Phaser.Types.Loader.FileTypes.GLSLFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.glsl
, i.e. ifkey
was "alien" then the URL will be "alien.glsl".shaderType
string <optional>
'fragment' The type of shader. Either
fragment
for a fragment shader, orvertex
for a vertex shader. This is ignored if you load a shader bundle.xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/GLSLFile.js (Line 317)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
html(key [, url] [, xhrSettings])
-
Adds an HTML file, or array of HTML files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.html('story', 'files/LoginForm.html'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global HTML Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the HTML Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the HTML Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.html({ key: 'login', url: 'files/LoginForm.html' });
See the documentation for
Phaser.Types.Loader.FileTypes.HTMLFileConfig
for more details.Once the file has finished loading you can access it from its Cache using its key:
this.load.html('login', 'files/LoginForm.html'); // and later in your game ... var data = this.cache.html.get('login');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasLEVEL1.
and the key wasStory
the final key will beLEVEL1.Story
and this is what you would use to retrieve the html from the HTML Cache.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "story" and no URL is given then the Loader will set the URL to be "story.html". It will always add
.html
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the HTML File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.HTMLFileConfig | Array.<Phaser.Types.Loader.FileTypes.HTMLFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.html
, i.e. ifkey
was "alien" then the URL will be "alien.html".xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.12.0
- Source: src/loader/filetypes/HTMLFile.js (Line 84)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
htmlTexture(key [, url] [, width] [, height] [, xhrSettings])
-
Adds an HTML File, or array of HTML Files, to the current load queue. When the files are loaded they will be rendered to textures and stored in the Texture Manager.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.htmlTexture('instructions', 'content/intro.html', 256, 512); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Texture Manager. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Texture Manager first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.htmlTexture({ key: 'instructions', url: 'content/intro.html', width: 256, height: 512 });
See the documentation for
Phaser.Types.Loader.FileTypes.HTMLTextureFileConfig
for more details.Once the file has finished loading you can use it as a texture for a Game Object by referencing its key:
this.load.htmlTexture('instructions', 'content/intro.html', 256, 512); // and later in your game ... this.add.image(x, y, 'instructions');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasMENU.
and the key wasBackground
the final key will beMENU.Background
and this is what you would use to retrieve the image from the Texture Manager.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.html". It will always add
.html
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.The width and height are the size of the texture to which the HTML will be rendered. It's not possible to determine these automatically, so you will need to provide them, either as arguments or in the file config object. When the HTML file has loaded a new SVG element is created with a size and viewbox set to the width and height given. The SVG file has a body tag added to it, with the HTML file contents included. It then calls
window.Blob
on the SVG, and if successful is added to the Texture Manager, otherwise it fails processing. The overall quality of the rendered HTML depends on your browser, and some of them may not even support the svg / blob process used. Be aware that there are limitations on what HTML can be inside an SVG. You can find out more details in this Mozilla MDN entry.Note: The ability to load this type of file will only be available if the HTMLTextureFile File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Default Description key
string | Phaser.Types.Loader.FileTypes.HTMLTextureFileConfig | Array.<Phaser.Types.Loader.FileTypes.HTMLTextureFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.html
, i.e. ifkey
was "alien" then the URL will be "alien.html".width
number <optional>
512 The width of the texture the HTML will be rendered to.
height
number <optional>
512 The height of the texture the HTML will be rendered to.
xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.12.0
- Source: src/loader/filetypes/HTMLTextureFile.js (Line 153)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
image(key [, url] [, xhrSettings])
-
Adds an Image, or array of Images, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.image('logo', 'images/phaserLogo.png'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle. If you try to load an animated gif only the first frame will be rendered. Browsers do not natively support playback of animated gifs to Canvas elements.
The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Texture Manager. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Texture Manager first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.image({ key: 'logo', url: 'images/AtariLogo.png' });
See the documentation for
Phaser.Types.Loader.FileTypes.ImageFileConfig
for more details.Once the file has finished loading you can use it as a texture for a Game Object by referencing its key:
this.load.image('logo', 'images/AtariLogo.png'); // and later in your game ... this.add.image(x, y, 'logo');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasMENU.
and the key wasBackground
the final key will beMENU.Background
and this is what you would use to retrieve the image from the Texture Manager.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.png". It will always add
.png
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Phaser also supports the automatic loading of associated normal maps. If you have a normal map to go with this image, then you can specify it by providing an array as the
url
where the second element is the normal map:this.load.image('logo', [ 'images/AtariLogo.png', 'images/AtariLogo-n.png' ]);
Or, if you are using a config object use the
normalMap
property:this.load.image({ key: 'logo', url: 'images/AtariLogo.png', normalMap: 'images/AtariLogo-n.png' });
The normal map file is subject to the same conditions as the image file with regard to the path, baseURL, CORs and XHR Settings. Normal maps are a WebGL only feature.
Note: The ability to load this type of file will only be available if the Image File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.ImageFileConfig | Array.<Phaser.Types.Loader.FileTypes.ImageFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string | Array.<string> <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/ImageFile.js (Line 159)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
isLoading()
-
Is the Loader actively loading, or processing loaded files?
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 654)
Returns:
true
if the Loader is busy loading or processing, otherwisefalse
.- Type
- boolean
-
isReady()
-
Is the Loader ready to start a new load?
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 667)
Returns:
true
if the Loader is ready to start a new load, otherwisefalse
.- Type
- boolean
-
json(key [, url] [, dataKey] [, xhrSettings])
-
Adds a JSON file, or array of JSON files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.json('wavedata', 'files/AlienWaveData.json'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global JSON Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the JSON Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the JSON Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.json({ key: 'wavedata', url: 'files/AlienWaveData.json' });
See the documentation for
Phaser.Types.Loader.FileTypes.JSONFileConfig
for more details.Once the file has finished loading you can access it from its Cache using its key:
this.load.json('wavedata', 'files/AlienWaveData.json'); // and later in your game ... var data = this.cache.json.get('wavedata');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasLEVEL1.
and the key wasWaves
the final key will beLEVEL1.Waves
and this is what you would use to retrieve the text from the JSON Cache.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "data" and no URL is given then the Loader will set the URL to be "data.json". It will always add
.json
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.You can also optionally provide a
dataKey
to use. This allows you to extract only a part of the JSON and store it in the Cache, rather than the whole file. For example, if your JSON data had a structure like this:{ "level1": { "baddies": { "aliens": {}, "boss": {} } }, "level2": {}, "level3": {} }
And you only wanted to store the
boss
data in the Cache, then you could passlevel1.baddies.boss
as thedataKey
.Note: The ability to load this type of file will only be available if the JSON File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.JSONFileConfig | Array.<Phaser.Types.Loader.FileTypes.JSONFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
object | string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json". Or, can be a fully formed JSON Object.dataKey
string <optional>
When the JSON file loads only this property will be stored in the Cache.
xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/JSONFile.js (Line 120)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
keyExists(file)
-
Checks the key and type of the given file to see if it will conflict with anything already in a Cache, the Texture Manager, or the list or inflight queues.
Parameters:
Name Type Description file
Phaser.Loader.File The file to check the key of.
- Since: 3.7.0
- Source: src/loader/LoaderPlugin.js (Line 520)
Returns:
true
if adding this file will cause a cache or queue conflict, otherwisefalse
.- Type
- boolean
-
listenerCount(event)
-
Return the number of listeners listening to a given event.
Parameters:
Name Type Description event
string | symbol The event name.
- Since: 3.0.0
- Inherited From:
- Source: src/events/EventEmitter.js (Line 75)
Returns:
The number of listeners.
- Type
- number
-
listeners(event)
-
Return the listeners registered for a given event.
Parameters:
Name Type Description event
string | symbol The event name.
- Since: 3.0.0
- Inherited From:
- Source: src/events/EventEmitter.js (Line 64)
Returns:
The registered listeners.
- Type
- Array.<function()>
-
loadComplete()
-
Called at the end when the load queue is exhausted and all files have either loaded or errored. By this point every loaded file will now be in its associated cache and ready for use.
Also clears down the Sets, puts progress to 1 and clears the deletion queue.
- Since: 3.7.0
- Source: src/loader/LoaderPlugin.js (Line 903)
Fires:
-
multiatlas(key [, atlasURL] [, path] [, baseURL] [, atlasXhrSettings])
-
Adds a Multi Texture Atlas, or array of multi atlases, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.multiatlas('level1', 'images/Level1.json'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.If you call this from outside of
preload
then you are responsible for starting the Loader afterwards and monitoring its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.Phaser expects the atlas data to be provided in a JSON file as exported from the application Texture Packer, version 4.6.3 or above, where you have made sure to use the Phaser 3 Export option.
The way it works internally is that you provide a URL to the JSON file. Phaser then loads this JSON, parses it and extracts which texture files it also needs to load to complete the process. If the JSON also defines normal maps, Phaser will load those as well.
The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Texture Manager. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Texture Manager first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.multiatlas({ key: 'level1', atlasURL: 'images/Level1.json' });
See the documentation for
Phaser.Types.Loader.FileTypes.MultiAtlasFileConfig
for more details.Instead of passing a URL for the atlas JSON data you can also pass in a well formed JSON object instead.
Once the atlas has finished loading you can use frames from it as textures for a Game Object by referencing its key:
this.load.multiatlas('level1', 'images/Level1.json'); // and later in your game ... this.add.image(x, y, 'level1', 'background');
To get a list of all available frames within an atlas please consult your Texture Atlas software.
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasMENU.
and the key wasBackground
the final key will beMENU.Background
and this is what you would use to retrieve the image from the Texture Manager.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.png". It will always add
.png
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the Multi Atlas File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.MultiAtlasFileConfig | Array.<Phaser.Types.Loader.FileTypes.MultiAtlasFileConfig> The key to use for this file, or a file configuration object, or array of them.
atlasURL
string <optional>
The absolute or relative URL to load the texture atlas json data file from. If undefined or
null
it will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json".path
string <optional>
Optional path to use when loading the textures defined in the atlas data.
baseURL
string <optional>
Optional Base URL to use when loading the textures defined in the atlas data.
atlasXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the atlas json file. Used in replacement of the Loaders default XHR Settings.
- Since: 3.7.0
- Source: src/loader/filetypes/MultiAtlasFile.js (Line 217)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
nextFile(file, success)
-
An internal method called automatically by the XHRLoader belong to a File.
This method will remove the given file from the inflight Set and update the load progress. If the file was successful its
onProcess
method is called, otherwise it is added to the delete queue.Parameters:
Name Type Description file
Phaser.Loader.File The File that just finished loading, or errored during load.
success
boolean true
if the file loaded successfully, otherwisefalse
.- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 799)
Fires:
-
obj(key [, objURL] [, matURL] [, flipUV] [, xhrSettings])
-
Adds a Wavefront OBJ file, or array of OBJ files, to the current load queue.
Note: You should ensure your 3D package has triangulated the OBJ file prior to export.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.obj('ufo', 'files/spaceship.obj'); }
You can optionally also load a Wavefront Material file as well, by providing the 3rd parameter:
function preload () { this.load.obj('ufo', 'files/spaceship.obj', 'files/spaceship.mtl'); }
If given, the material will be parsed and stored along with the obj data in the cache.
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global OBJ Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the OBJ Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the OBJ Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.obj({ key: 'ufo', url: 'files/spaceship.obj', matURL: 'files/spaceship.mtl', flipUV: true });
See the documentation for
Phaser.Types.Loader.FileTypes.OBJFileConfig
for more details.Once the file has finished loading you can access it from its Cache using its key:
this.load.obj('ufo', 'files/spaceship.obj'); // and later in your game ... var data = this.cache.obj.get('ufo');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasLEVEL1.
and the key wasStory
the final key will beLEVEL1.Story
and this is what you would use to retrieve the obj from the OBJ Cache.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "story" and no URL is given then the Loader will set the URL to be "story.obj". It will always add
.obj
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the OBJ File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.OBJFileConfig | Array.<Phaser.Types.Loader.FileTypes.OBJFileConfig> The key to use for this file, or a file configuration object, or array of them.
objURL
string <optional>
The absolute or relative URL to load the obj file from. If undefined or
null
it will be set to<key>.obj
, i.e. ifkey
was "alien" then the URL will be "alien.obj".matURL
string <optional>
Optional absolute or relative URL to load the obj material file from.
flipUV
boolean <optional>
Flip the UV coordinates stored in the model data?
xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.50.0
- Source: src/loader/filetypes/OBJFile.js (Line 140)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
off(event [, fn] [, context] [, once])
-
Remove the listeners of a given event.
Parameters:
Name Type Argument Description event
string | symbol The event name.
fn
function <optional>
Only remove the listeners that match this function.
context
* <optional>
Only remove the listeners that have this context.
once
boolean <optional>
Only remove one-time listeners.
- Since: 3.0.0
- Inherited From:
- Source: src/events/EventEmitter.js (Line 151)
Returns:
this
. -
on(event, fn [, context])
-
Add a listener for a given event.
Parameters:
Name Type Argument Default Description event
string | symbol The event name.
fn
function The listener function.
context
* <optional>
this The context to invoke the listener with.
- Since: 3.0.0
- Inherited From:
- Source: src/events/EventEmitter.js (Line 98)
Returns:
this
. -
once(event, fn [, context])
-
Add a one-time listener for a given event.
Parameters:
Name Type Argument Default Description event
string | symbol The event name.
fn
function The listener function.
context
* <optional>
this The context to invoke the listener with.
- Since: 3.0.0
- Inherited From:
- Source: src/events/EventEmitter.js (Line 124)
Returns:
this
. -
pack(key [, url] [, dataKey] [, xhrSettings])
-
Adds a JSON File Pack, or array of packs, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.pack('level1', 'data/Level1Files.json'); }
A File Pack is a JSON file (or object) that contains details about other files that should be added into the Loader. Here is a small example:
{ "test1": { "files": [ { "type": "image", "key": "taikodrummaster", "url": "assets/pics/taikodrummaster.jpg" }, { "type": "image", "key": "sukasuka-chtholly", "url": "assets/pics/sukasuka-chtholly.png" } ] }, "meta": { "generated": "1401380327373", "app": "Phaser 3 Asset Packer", "url": "https://phaser.io", "version": "1.0", "copyright": "Photon Storm Ltd. 2018" } }
The pack can be split into sections. In the example above you'll see a section called
test1. You can tell the
load.pack` method to parse only a particular section of a pack. The pack is stored in the JSON Cache, so you can pass it to the Loader to process additional sections as needed in your game, or you can just load them all at once without specifying anything.The pack file can contain an entry for any type of file that Phaser can load. The object structures exactly match that of the file type configs, and all properties available within the file type configs can be used in the pack file too.
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.If you call this from outside of
preload
then you are responsible for starting the Loader afterwards and monitoring its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.The key must be a unique String. It is used to add the file to the global JSON Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the JSON Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the JSON Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.pack({ key: 'level1', url: 'data/Level1Files.json' });
See the documentation for
Phaser.Types.Loader.FileTypes.PackFileConfig
for more details.If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasLEVEL1.
and the key wasWaves
the final key will beLEVEL1.Waves
and this is what you would use to retrieve the text from the JSON Cache.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "data" and no URL is given then the Loader will set the URL to be "data.json". It will always add
.json
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.You can also optionally provide a
dataKey
to use. This allows you to extract only a part of the JSON and store it in the Cache, rather than the whole file. For example, if your JSON data had a structure like this:{ "level1": { "baddies": { "aliens": {}, "boss": {} } }, "level2": {}, "level3": {} }
And you only wanted to store the
boss
data in the Cache, then you could passlevel1.baddies.boss
as thedataKey
.Note: The ability to load this type of file will only be available if the Pack File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.PackFileConfig | Array.<Phaser.Types.Loader.FileTypes.PackFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json".dataKey
string <optional>
When the JSON file loads only this property will be stored in the Cache.
xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.7.0
- Source: src/loader/filetypes/PackFile.js (Line 72)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
plugin(key [, url] [, start] [, mapping] [, xhrSettings])
-
Adds a Plugin Script file, or array of plugin files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.plugin('modplayer', 'plugins/ModPlayer.js'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String and not already in-use by another file in the Loader.
Instead of passing arguments you can pass a configuration object, such as:
this.load.plugin({ key: 'modplayer', url: 'plugins/ModPlayer.js' });
See the documentation for
Phaser.Types.Loader.FileTypes.PluginFileConfig
for more details.Once the file has finished loading it will automatically be converted into a script element via
document.createElement('script')
. It will have its language set to JavaScript,defer
set to false and then the resulting element will be appended todocument.head
. Any code then in the script will be executed. It will then be passed to the Phaser PluginCache.register method.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.js". It will always add
.js
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the Plugin File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.PluginFileConfig | Array.<Phaser.Types.Loader.FileTypes.PluginFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string | function <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.js
, i.e. ifkey
was "alien" then the URL will be "alien.js". Or, a plugin function.start
boolean <optional>
Automatically start the plugin after loading?
mapping
string <optional>
If this plugin is to be injected into the Scene, this is the property key used.
xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/PluginFile.js (Line 129)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
removeAllListeners( [event])
-
Remove all listeners, or those of the specified event.
Parameters:
Name Type Argument Description event
string | symbol <optional>
The event name.
- Since: 3.0.0
- Inherited From:
- Source: src/events/EventEmitter.js (Line 165)
Returns:
this
. -
removeListener(event [, fn] [, context] [, once])
-
Remove the listeners of a given event.
Parameters:
Name Type Argument Description event
string | symbol The event name.
fn
function <optional>
Only remove the listeners that match this function.
context
* <optional>
Only remove the listeners that have this context.
once
boolean <optional>
Only remove one-time listeners.
- Since: 3.0.0
- Inherited From:
- Source: src/events/EventEmitter.js (Line 137)
Returns:
this
. -
reset()
-
Resets the Loader.
This will clear all lists and reset the base URL, path and prefix.
Warning: If the Loader is currently downloading files, or has files in its queue, they will be aborted.
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 1001)
-
save(data [, filename] [, filetype])
-
Causes the browser to save the given data as a file to its default Downloads folder.
Creates a DOM level anchor link, assigns it as being a
download
anchor, sets the href to be an ObjectURL based on the given data, and then invokes a click event.Parameters:
Name Type Argument Default Description data
* The data to be saved. Will be passed through URL.createObjectURL.
filename
string <optional>
file.json The filename to save the file as.
filetype
string <optional>
application/json The file type to use when saving the file. Defaults to JSON.
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 967)
Returns:
This Loader plugin.
-
saveJSON(data [, filename])
-
Converts the given JSON data into a file that the browser then prompts you to download so you can save it locally.
The data must be well formed JSON and ready-parsed, not a JavaScript object.
Parameters:
Name Type Argument Default Description data
* The JSON data, ready parsed.
filename
string <optional>
file.json The name to save the JSON file as.
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 949)
Returns:
This Loader plugin.
-
sceneFile(key [, url] [, xhrSettings])
-
Adds an external Scene file, or array of Scene files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.sceneFile('Level1', 'src/Level1.js'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global Scene Manager upon a successful load.
For a Scene File it's vitally important that the key matches the class name in the JavaScript file.
For example here is the source file:
class ExternalScene extends Phaser.Scene { constructor () { super('myScene'); } }
Because the class is called
ExternalScene
that is the exact same key you must use when loading it:function preload () { this.load.sceneFile('ExternalScene', 'src/yourScene.js'); }
The key that is used within the Scene Manager can either be set to the same, or you can override it in the Scene constructor, as we've done in the example above, where the Scene key was changed to
myScene
.The key should be unique both in terms of files being loaded and Scenes already present in the Scene Manager. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Scene Manager first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.sceneFile({ key: 'Level1', url: 'src/Level1.js' });
See the documentation for
Phaser.Types.Loader.FileTypes.SceneFileConfig
for more details.Once the file has finished loading it will be added to the Scene Manager.
this.load.sceneFile('Level1', 'src/Level1.js'); // and later in your game ... this.scene.start('Level1');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasWORLD1.
and the key wasStory
the final key will beWORLD1.Story
and this is what you would use to retrieve the text from the Scene Manager.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "story" and no URL is given then the Loader will set the URL to be "story.js". It will always add
.js
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the Scene File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.SceneFileConfig | Array.<Phaser.Types.Loader.FileTypes.SceneFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.js
, i.e. ifkey
was "alien" then the URL will be "alien.js".xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.16.0
- Source: src/loader/filetypes/SceneFile.js (Line 101)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
scenePlugin(key [, url] [, systemKey] [, sceneKey] [, xhrSettings])
-
Adds a Scene Plugin Script file, or array of plugin files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.scenePlugin('ModPlayer', 'plugins/ModPlayer.js', 'modPlayer', 'mods'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String and not already in-use by another file in the Loader.
Instead of passing arguments you can pass a configuration object, such as:
this.load.scenePlugin({ key: 'modplayer', url: 'plugins/ModPlayer.js' });
See the documentation for
Phaser.Types.Loader.FileTypes.ScenePluginFileConfig
for more details.Once the file has finished loading it will automatically be converted into a script element via
document.createElement('script')
. It will have its language set to JavaScript,defer
set to false and then the resulting element will be appended todocument.head
. Any code then in the script will be executed. It will then be passed to the Phaser PluginCache.register method.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.js". It will always add
.js
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the Script File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.ScenePluginFileConfig | Array.<Phaser.Types.Loader.FileTypes.ScenePluginFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string | function <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.js
, i.e. ifkey
was "alien" then the URL will be "alien.js". Or, set to a plugin function.systemKey
string <optional>
If this plugin is to be added to Scene.Systems, this is the property key for it.
sceneKey
string <optional>
If this plugin is to be added to the Scene, this is the property key for it.
xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.8.0
- Source: src/loader/filetypes/ScenePluginFile.js (Line 123)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
script(key [, url] [, xhrSettings])
-
Adds a Script file, or array of Script files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.script('aliens', 'lib/aliens.js'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String and not already in-use by another file in the Loader.
Instead of passing arguments you can pass a configuration object, such as:
this.load.script({ key: 'aliens', url: 'lib/aliens.js' });
See the documentation for
Phaser.Types.Loader.FileTypes.ScriptFileConfig
for more details.Once the file has finished loading it will automatically be converted into a script element via
document.createElement('script')
. It will have its language set to JavaScript,defer
set to false and then the resulting element will be appended todocument.head
. Any code then in the script will be executed.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.js". It will always add
.js
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the Script File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.ScriptFileConfig | Array.<Phaser.Types.Loader.FileTypes.ScriptFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.js
, i.e. ifkey
was "alien" then the URL will be "alien.js".xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/ScriptFile.js (Line 90)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
scripts(key [, url] [, extension] [, xhrSettings])
-
Adds an array of Script files to the current load queue.
The difference between this and the
ScriptFile
file type is that you give an array of scripts to this method, and the scripts are then processed exactly in that order. This allows you to load a bunch of scripts that may have dependencies on each other without worrying about the async nature of traditional script loading.You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.scripts('PostProcess', [ 'libs/shaders/CopyShader.js', 'libs/postprocessing/EffectComposer.js', 'libs/postprocessing/RenderPass.js', 'libs/postprocessing/MaskPass.js', 'libs/postprocessing/ShaderPass.js', 'libs/postprocessing/AfterimagePass.js' ]); }
In the code above the script files will all be loaded in parallel but only processed (i.e. invoked) in the exact order given in the array.
The files are not loaded right away. They are added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the files are queued it means you cannot use the files immediately after calling this method, but must wait for the files to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String and not already in-use by another file in the Loader.
Instead of passing arguments you can pass a configuration object, such as:
this.load.scripts({ key: 'PostProcess', url: [ 'libs/shaders/CopyShader.js', 'libs/postprocessing/EffectComposer.js', 'libs/postprocessing/RenderPass.js', 'libs/postprocessing/MaskPass.js', 'libs/postprocessing/ShaderPass.js', 'libs/postprocessing/AfterimagePass.js' ] });
See the documentation for
Phaser.Types.Loader.FileTypes.MultiScriptFileConfig
for more details.Once all the files have finished loading they will automatically be converted into a script element via
document.createElement('script')
. They will have their language set to JavaScript,defer
set to false and then the resulting element will be appended todocument.head
. Any code then in the script will be executed. This is done in the exact order the files are specified in the url array.The URLs can be relative or absolute. If the URLs are relative the
Loader.baseURL
andLoader.path
values will be prepended to them.Note: The ability to load this type of file will only be available if the MultiScript File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Default Description key
string | Phaser.Types.Loader.FileTypes.MultiScriptFileConfig | Array.<Phaser.Types.Loader.FileTypes.MultiScriptFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
Array.<string> <optional>
An array of absolute or relative URLs to load the script files from. They are processed in the order given in the array.
extension
string <optional>
'js' The default file extension to use if no url is provided.
xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
Extra XHR Settings specifically for these files.
- Since: 3.17.0
- Source: src/loader/filetypes/MultiScriptFile.js (Line 109)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
setBaseURL( [url])
-
If you want to append a URL before the path of any asset you can set this here.
Useful if allowing the asset base url to be configured outside of the game code.
Once a base URL is set it will affect every file loaded by the Loader from that point on. It does not change any file already being loaded. To reset it, call this method with no arguments.
Parameters:
Name Type Argument Description url
string <optional>
The URL to use. Leave empty to reset.
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 358)
Returns:
This Loader object.
-
setCORS( [crossOrigin])
-
Sets the Cross Origin Resource Sharing value used when loading files.
Files can override this value on a per-file basis by specifying an alternative
crossOrigin
value in their file config.Once CORs is set it will then affect every file loaded by the Loader from that point on, as long as they don't have their own CORs setting. To reset it, call this method with no arguments.
For more details about CORs see https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
Parameters:
Name Type Argument Description crossOrigin
string <optional>
The value to use for the
crossOrigin
property in the load request.- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 451)
Returns:
This Loader object.
-
setPath( [path])
-
The value of
path
, if set, is placed before any relative file path given. For example:this.load.setPath("images/sprites/"); this.load.image("ball", "ball.png"); this.load.image("tree", "level1/oaktree.png"); this.load.image("boom", "http://server.com/explode.png");
Would load the
ball
file fromimages/sprites/ball.png
and the tree fromimages/sprites/level1/oaktree.png
but the fileboom
would load from the URL given as it's an absolute URL.Please note that the path is added before the filename but after the baseURL (if set.)
Once a path is set it will then affect every file added to the Loader from that point on. It does not change any file already in the load queue. To reset it, call this method with no arguments.
Parameters:
Name Type Argument Description path
string <optional>
The path to use. Leave empty to reset.
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 387)
Returns:
This Loader object.
-
setPrefix( [prefix])
-
An optional prefix that is automatically prepended to the start of every file key.
If prefix was
MENU.
and you load an image with the key 'Background' the resulting key would beMENU.Background
.Once a prefix is set it will then affect every file added to the Loader from that point on. It does not change any file already in the load queue. To reset it, call this method with no arguments.
Parameters:
Name Type Argument Description prefix
string <optional>
The prefix to use. Leave empty to reset.
- Since: 3.7.0
- Source: src/loader/LoaderPlugin.js (Line 427)
Returns:
This Loader object.
-
spine(key, jsonURL, atlasURL [, preMultipliedAlpha] [, textureXhrSettings] [, atlasXhrSettings])
-
Adds a Spine Skeleton and Atlas file, or array of files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.spine('spineBoy', 'boy.json', 'boy.atlas', true); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.If you call this from outside of
preload
then you are responsible for starting the Loader afterwards and monitoring its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.Phaser expects the Spine data to be exported from the Spine application in a JSON format, not binary. The associated atlas files are scanned for any texture files present in them, which are then loaded. If you have exported your Spine data with preMultipliedAlpha set, then you should enable this in the arguments, or you may see black outlines around skeleton textures.
The key must be a unique String. It is used to add the file to the global Spine cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Spine cache. Loading a file using a key that is already taken will result in a warning.
Instead of passing arguments you can pass a configuration object, such as:
this.load.spine({ key: 'mainmenu', jsonURL: 'boy.json', atlasURL: 'boy.atlas', preMultipliedAlpha: true });
If you need to load multiple Spine atlas files, provide them as an array:
function preload () { this.load.spine('demos', 'demos.json', [ 'atlas1.atlas', 'atlas2.atlas' ], true); }
See the documentation for
Phaser.Types.Loader.FileTypes.SpineFileConfig
for more details.If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasMENU.
and the key wasBackground
the final key will beMENU.Background
and this is what you would use to retrieve the data from the Spine plugin.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.json". It will always add
.json
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the Spine Plugin has been built or loaded into Phaser.
Parameters:
Name Type Argument Default Description key
string | Phaser.Types.Loader.FileTypes.JSONFileConfig | Array.<Phaser.Types.Loader.FileTypes.JSONFileConfig> The key to use for this file, or a file configuration object, or array of them.
jsonURL
string The absolute or relative URL to load the Spine json file from. If undefined or
null
it will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json".atlasURL
string | Array.<string> The absolute or relative URL to load the Spine atlas file from. If undefined or
null
it will be set to<key>.atlas
, i.e. ifkey
was "alien" then the URL will be "alien.atlas".preMultipliedAlpha
boolean <optional>
false Do the texture files include pre-multiplied alpha or not?
textureXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the Spine json file. Used in replacement of the Loaders default XHR Settings.
atlasXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the Spine atlas file. Used in replacement of the Loaders default XHR Settings.
- Since: 3.19.0
- Source: plugins/spine/src/SpinePlugin.js (Line 565)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
spritesheet(key [, url] [, frameConfig] [, xhrSettings])
-
Adds a Sprite Sheet Image, or array of Sprite Sheet Images, to the current load queue.
The term 'Sprite Sheet' in Phaser means a fixed-size sheet. Where every frame in the sheet is the exact same size, and you reference those frames using numbers, not frame names. This is not the same thing as a Texture Atlas, where the frames are packed in a way where they take up the least amount of space, and are referenced by their names, not numbers. Some articles and software use the term 'Sprite Sheet' to mean Texture Atlas, so please be aware of what sort of file you're actually trying to load.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.spritesheet('bot', 'images/robot.png', { frameWidth: 32, frameHeight: 38 }); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle. If you try to load an animated gif only the first frame will be rendered. Browsers do not natively support playback of animated gifs to Canvas elements.
The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Texture Manager. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Texture Manager first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.spritesheet({ key: 'bot', url: 'images/robot.png', frameConfig: { frameWidth: 32, frameHeight: 38, startFrame: 0, endFrame: 8 } });
See the documentation for
Phaser.Types.Loader.FileTypes.SpriteSheetFileConfig
for more details.Once the file has finished loading you can use it as a texture for a Game Object by referencing its key:
this.load.spritesheet('bot', 'images/robot.png', { frameWidth: 32, frameHeight: 38 }); // and later in your game ... this.add.image(x, y, 'bot', 0);
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasPLAYER.
and the key wasRunning
the final key will bePLAYER.Running
and this is what you would use to retrieve the image from the Texture Manager.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.png". It will always add
.png
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Phaser also supports the automatic loading of associated normal maps. If you have a normal map to go with this image, then you can specify it by providing an array as the
url
where the second element is the normal map:this.load.spritesheet('logo', [ 'images/AtariLogo.png', 'images/AtariLogo-n.png' ], { frameWidth: 256, frameHeight: 80 });
Or, if you are using a config object use the
normalMap
property:this.load.spritesheet({ key: 'logo', url: 'images/AtariLogo.png', normalMap: 'images/AtariLogo-n.png', frameConfig: { frameWidth: 256, frameHeight: 80 } });
The normal map file is subject to the same conditions as the image file with regard to the path, baseURL, CORs and XHR Settings. Normal maps are a WebGL only feature.
Note: The ability to load this type of file will only be available if the Sprite Sheet File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.SpriteSheetFileConfig | Array.<Phaser.Types.Loader.FileTypes.SpriteSheetFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".frameConfig
Phaser.Types.Loader.FileTypes.ImageFrameConfig <optional>
The frame configuration object. At a minimum it should have a
frameWidth
property.xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/SpriteSheetFile.js (Line 59)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
start()
-
Starts the Loader running. This will reset the progress and totals and then emit a
start
event. If there is nothing in the queue the Loader will immediately complete, otherwise it will start loading the first batch of files.The Loader is started automatically if the queue is populated within your Scenes
preload
method.However, outside of this, you need to call this method to start it.
If the Loader is already running this method will simply return.
- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 680)
Fires:
-
svg(key [, url] [, svgConfig] [, xhrSettings])
-
Adds an SVG File, or array of SVG Files, to the current load queue. When the files are loaded they will be rendered to bitmap textures and stored in the Texture Manager.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.svg('morty', 'images/Morty.svg'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Texture Manager. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Texture Manager first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.svg({ key: 'morty', url: 'images/Morty.svg' });
See the documentation for
Phaser.Types.Loader.FileTypes.SVGFileConfig
for more details.Once the file has finished loading you can use it as a texture for a Game Object by referencing its key:
this.load.svg('morty', 'images/Morty.svg'); // and later in your game ... this.add.image(x, y, 'morty');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasMENU.
and the key wasBackground
the final key will beMENU.Background
and this is what you would use to retrieve the image from the Texture Manager.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.html". It will always add
.html
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.You can optionally pass an SVG Resize Configuration object when you load an SVG file. By default the SVG will be rendered to a texture at the same size defined in the SVG file attributes. However, this isn't always desirable. You may wish to resize the SVG (either down or up) to improve texture clarity, or reduce texture memory consumption. You can either specify an exact width and height to resize the SVG to:
function preload () { this.load.svg('morty', 'images/Morty.svg', { width: 300, height: 600 }); }
Or when using a configuration object:
this.load.svg({ key: 'morty', url: 'images/Morty.svg', svgConfig: { width: 300, height: 600 } });
Alternatively, you can just provide a scale factor instead:
function preload () { this.load.svg('morty', 'images/Morty.svg', { scale: 2.5 }); }
Or when using a configuration object:
this.load.svg({ key: 'morty', url: 'images/Morty.svg', svgConfig: { scale: 2.5 } });
If scale, width and height values are all given, the scale has priority and the width and height values are ignored.
Note: The ability to load this type of file will only be available if the SVG File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.SVGFileConfig | Array.<Phaser.Types.Loader.FileTypes.SVGFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.svg
, i.e. ifkey
was "alien" then the URL will be "alien.svg".svgConfig
Phaser.Types.Loader.FileTypes.SVGSizeConfig <optional>
The svg size configuration object.
xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/SVGFile.js (Line 197)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
text(key [, url] [, xhrSettings])
-
Adds a Text file, or array of Text files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.text('story', 'files/IntroStory.txt'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global Text Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Text Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Text Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.text({ key: 'story', url: 'files/IntroStory.txt' });
See the documentation for
Phaser.Types.Loader.FileTypes.TextFileConfig
for more details.Once the file has finished loading you can access it from its Cache using its key:
this.load.text('story', 'files/IntroStory.txt'); // and later in your game ... var data = this.cache.text.get('story');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasLEVEL1.
and the key wasStory
the final key will beLEVEL1.Story
and this is what you would use to retrieve the text from the Text Cache.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "story" and no URL is given then the Loader will set the URL to be "story.txt". It will always add
.txt
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the Text File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.TextFileConfig | Array.<Phaser.Types.Loader.FileTypes.TextFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.txt
, i.e. ifkey
was "alien" then the URL will be "alien.txt".xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/TextFile.js (Line 88)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
tilemapCSV(key [, url] [, xhrSettings])
-
Adds a CSV Tilemap file, or array of CSV files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.tilemapCSV('level1', 'maps/Level1.csv'); }
Tilemap CSV data can be created in a text editor, or a 3rd party app that exports as CSV.
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global Tilemap Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Tilemap Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Text Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.tilemapCSV({ key: 'level1', url: 'maps/Level1.csv' });
See the documentation for
Phaser.Types.Loader.FileTypes.TilemapCSVFileConfig
for more details.Once the file has finished loading you can access it from its Cache using its key:
this.load.tilemapCSV('level1', 'maps/Level1.csv'); // and later in your game ... var map = this.make.tilemap({ key: 'level1' });
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasLEVEL1.
and the key wasStory
the final key will beLEVEL1.Story
and this is what you would use to retrieve the text from the Tilemap Cache.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "level" and no URL is given then the Loader will set the URL to be "level.csv". It will always add
.csv
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the Tilemap CSV File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.TilemapCSVFileConfig | Array.<Phaser.Types.Loader.FileTypes.TilemapCSVFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.csv
, i.e. ifkey
was "alien" then the URL will be "alien.csv".xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/TilemapCSVFile.js (Line 102)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
tilemapImpact(key [, url] [, xhrSettings])
-
Adds an Impact.js Tilemap file, or array of map files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.tilemapImpact('level1', 'maps/Level1.json'); }
Impact Tilemap data is created the Impact.js Map Editor called Weltmeister.
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global Tilemap Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Tilemap Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Text Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.tilemapImpact({ key: 'level1', url: 'maps/Level1.json' });
See the documentation for
Phaser.Types.Loader.FileTypes.TilemapImpactFileConfig
for more details.Once the file has finished loading you can access it from its Cache using its key:
this.load.tilemapImpact('level1', 'maps/Level1.json'); // and later in your game ... var map = this.make.tilemap({ key: 'level1' });
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasLEVEL1.
and the key wasStory
the final key will beLEVEL1.Story
and this is what you would use to retrieve the text from the Tilemap Cache.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "level" and no URL is given then the Loader will set the URL to be "level.json". It will always add
.json
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the Tilemap Impact File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.TilemapImpactFileConfig | Array.<Phaser.Types.Loader.FileTypes.TilemapImpactFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json".xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.7.0
- Source: src/loader/filetypes/TilemapImpactFile.js (Line 63)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
tilemapTiledJSON(key [, url] [, xhrSettings])
-
Adds a Tiled JSON Tilemap file, or array of map files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.tilemapTiledJSON('level1', 'maps/Level1.json'); }
The Tilemap data is created using the Tiled Map Editor and selecting JSON as the export format.
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global Tilemap Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Tilemap Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Text Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.tilemapTiledJSON({ key: 'level1', url: 'maps/Level1.json' });
See the documentation for
Phaser.Types.Loader.FileTypes.TilemapJSONFileConfig
for more details.Once the file has finished loading you can access it from its Cache using its key:
this.load.tilemapTiledJSON('level1', 'maps/Level1.json'); // and later in your game ... var map = this.make.tilemap({ key: 'level1' });
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasLEVEL1.
and the key wasStory
the final key will beLEVEL1.Story
and this is what you would use to retrieve the text from the Tilemap Cache.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "level" and no URL is given then the Loader will set the URL to be "level.json". It will always add
.json
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the Tilemap JSON File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.TilemapJSONFileConfig | Array.<Phaser.Types.Loader.FileTypes.TilemapJSONFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
object | string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json". Or, a well formed JSON object.xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/TilemapJSONFile.js (Line 63)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
unityAtlas(key [, textureURL] [, atlasURL] [, textureXhrSettings] [, atlasXhrSettings])
-
Adds a Unity YAML based Texture Atlas, or array of atlases, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.unityAtlas('mainmenu', 'images/MainMenu.png', 'images/MainMenu.txt'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.If you call this from outside of
preload
then you are responsible for starting the Loader afterwards and monitoring its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.Phaser expects the atlas data to be provided in a YAML formatted text file as exported from Unity.
Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle.
The key must be a unique String. It is used to add the file to the global Texture Manager upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Texture Manager. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Texture Manager first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.unityAtlas({ key: 'mainmenu', textureURL: 'images/MainMenu.png', atlasURL: 'images/MainMenu.txt' });
See the documentation for
Phaser.Types.Loader.FileTypes.UnityAtlasFileConfig
for more details.Once the atlas has finished loading you can use frames from it as textures for a Game Object by referencing its key:
this.load.unityAtlas('mainmenu', 'images/MainMenu.png', 'images/MainMenu.json'); // and later in your game ... this.add.image(x, y, 'mainmenu', 'background');
To get a list of all available frames within an atlas please consult your Texture Atlas software.
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasMENU.
and the key wasBackground
the final key will beMENU.Background
and this is what you would use to retrieve the image from the Texture Manager.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.png". It will always add
.png
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Phaser also supports the automatic loading of associated normal maps. If you have a normal map to go with this image, then you can specify it by providing an array as the
url
where the second element is the normal map:this.load.unityAtlas('mainmenu', [ 'images/MainMenu.png', 'images/MainMenu-n.png' ], 'images/MainMenu.txt');
Or, if you are using a config object use the
normalMap
property:this.load.unityAtlas({ key: 'mainmenu', textureURL: 'images/MainMenu.png', normalMap: 'images/MainMenu-n.png', atlasURL: 'images/MainMenu.txt' });
The normal map file is subject to the same conditions as the image file with regard to the path, baseURL, CORs and XHR Settings. Normal maps are a WebGL only feature.
Note: The ability to load this type of file will only be available if the Unity Atlas File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.UnityAtlasFileConfig | Array.<Phaser.Types.Loader.FileTypes.UnityAtlasFileConfig> The key to use for this file, or a file configuration object, or array of them.
textureURL
string | Array.<string> <optional>
The absolute or relative URL to load the texture image file from. If undefined or
null
it will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".atlasURL
string <optional>
The absolute or relative URL to load the texture atlas data file from. If undefined or
null
it will be set to<key>.txt
, i.e. ifkey
was "alien" then the URL will be "alien.txt".textureXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the atlas image file. Used in replacement of the Loaders default XHR Settings.
atlasXhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object for the atlas data file. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/UnityAtlasFile.js (Line 107)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
update()
-
Called automatically during the load process.
- Since: 3.10.0
- Source: src/loader/LoaderPlugin.js (Line 745)
-
updateProgress()
-
Called automatically during the load process. It updates the
progress
value and then emits a progress event, which you can use to display a loading bar in your game.- Since: 3.0.0
- Source: src/loader/LoaderPlugin.js (Line 729)
Fires:
-
video(key [, urls] [, loadEvent] [, asBlob] [, noAudio] [, xhrSettings])
-
Adds a Video file, or array of video files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.video('intro', [ 'video/level1.mp4', 'video/level1.webm', 'video/level1.mov' ]); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global Video Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Video Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Video Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.video({ key: 'intro', url: [ 'video/level1.mp4', 'video/level1.webm', 'video/level1.mov' ], asBlob: false, noAudio: true });
See the documentation for
Phaser.Types.Loader.FileTypes.VideoFileConfig
for more details.The URLs can be relative or absolute. If the URLs are relative the
Loader.baseURL
andLoader.path
values will be prepended to them.Due to different browsers supporting different video file types you should usually provide your video files in a variety of formats. mp4, mov and webm are the most common. If you provide an array of URLs then the Loader will determine which one file to load based on browser support, starting with the first in the array and progressing to the end.
Unlike most asset-types, videos do not need to be preloaded. You can create a Video Game Object and then call its
loadURL
method, to load a video at run-time, rather than in advance.Note: The ability to load this type of file will only be available if the Video File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Default Description key
string | Phaser.Types.Loader.FileTypes.VideoFileConfig | Array.<Phaser.Types.Loader.FileTypes.VideoFileConfig> The key to use for this file, or a file configuration object, or array of them.
urls
string | Array.<string> <optional>
The absolute or relative URL to load the video files from.
loadEvent
string <optional>
'loadeddata' The load event to listen for when not loading as a blob. Either
loadeddata
,canplay
orcanplaythrough
.asBlob
boolean <optional>
false Load the video as a data blob, or stream it via the Video element?
noAudio
boolean <optional>
false Does the video have an audio track? If not you can enable auto-playing on it.
xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.20.0
- Source: src/loader/filetypes/VideoFile.js (Line 295)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.
-
xml(key [, url] [, xhrSettings])
-
Adds an XML file, or array of XML files, to the current load queue.
You can call this method from within your Scene's
preload
, along with any other files you wish to load:function preload () { this.load.xml('wavedata', 'files/AlienWaveData.xml'); }
The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's
preload
method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene'spreload
method and then when the Scene'screate
method is called you are guaranteed that all of those assets are ready for use and have been loaded.The key must be a unique String. It is used to add the file to the global XML Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the XML Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the XML Cache first, before loading a new one.
Instead of passing arguments you can pass a configuration object, such as:
this.load.xml({ key: 'wavedata', url: 'files/AlienWaveData.xml' });
See the documentation for
Phaser.Types.Loader.FileTypes.XMLFileConfig
for more details.Once the file has finished loading you can access it from its Cache using its key:
this.load.xml('wavedata', 'files/AlienWaveData.xml'); // and later in your game ... var data = this.cache.xml.get('wavedata');
If you have specified a prefix in the loader, via
Loader.setPrefix
then this value will be prepended to this files key. For example, if the prefix wasLEVEL1.
and the key wasWaves
the final key will beLEVEL1.Waves
and this is what you would use to retrieve the text from the XML Cache.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "data" and no URL is given then the Loader will set the URL to be "data.xml". It will always add
.xml
as the extension, although this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.Note: The ability to load this type of file will only be available if the XML File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.
Parameters:
Name Type Argument Description key
string | Phaser.Types.Loader.FileTypes.XMLFileConfig | Array.<Phaser.Types.Loader.FileTypes.XMLFileConfig> The key to use for this file, or a file configuration object, or array of them.
url
string <optional>
The absolute or relative URL to load this file from. If undefined or
null
it will be set to<key>.xml
, i.e. ifkey
was "alien" then the URL will be "alien.xml".xhrSettings
Phaser.Types.Loader.XHRSettingsObject <optional>
An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
- Since: 3.0.0
- Source: src/loader/filetypes/XMLFile.js (Line 94)
Fires:
- Phaser.Loader.LoaderPlugin#event:ADD
Returns:
The Loader instance.