API Docs for: 1.1.0
Show:

Kiwi.Files.Loader Class

Defined in: src\file\Loader.ts:10
Module: Files
Parent Module: Kiwi

Used for the loading of files and game assets. This usually happens when a State is at the 'loading' stage (executing the 'preload' method).

Constructor

Kiwi.Files.Loader

(
  • game
)
Kiwi.Files.Loader

Parameters:

  • game Kiwi.Game

    The game that this loader belongs to.

Returns:

Kiwi.Files.Loader:

This Object

Methods

addAudio

(
  • key
  • url
  • [storeAsGlobal=true]
  • [onlyIfSupported=true]
)
public

Creates a new File to store a audio piece. This method firstly checks to see if the AUDIO file being loaded is supported or not by the browser/device before adding it to the loading queue. You can override this behaviour and tell the audio data to load even if not supported by setting the 'onlyIfSupported' boolean to false. Also you can now pass an array of filepaths, and the first audio filetype that is supported will be loaded.

Parameters:

  • key String

    The key for the audio file.

  • url String

    The url of the audio to load. You can pass an array of URLs, in which case the first supported audio filetype in the array will be loaded.

  • [storeAsGlobal=true] Boolean optional

    If the file should be stored globally.

  • [onlyIfSupported=true] Boolean optional

    If the audio file should only be loaded if Kiwi detects that the audio file could be played. Set this to fa

addBinaryFile

(
  • key
  • url
  • [storeAsGlobal=true]
)
public

Creates a new File for a Binary file and adds it to the loading queue.

Parameters:

  • key String

    The key for the file.

  • url String

    The url to the Binary file.

  • [storeAsGlobal=true] Boolean optional

    If the file should be stored globally.

addImage

(
  • key
  • url
  • [width]
  • [height]
  • [offsetX]
  • [offsetY]
  • [storeAsGlobal=true]
)
public

Creates a new file for an image and adds a the file to loading queue.

Parameters:

  • key String

    The key for the file.

  • url String

    The url of the image to load.

  • [width] Number optional

    The width of the cell on the image to use once the image is loaded.

  • [height] Number optional

    The height of the cell on the image to use once the image is loaded.

  • [offsetX] Number optional

    An offset on the x axis of the cell.

  • [offsetY] Number optional

    An offset of the y axis of the cell.

  • [storeAsGlobal=true] Boolean optional

    If the image should be stored globally or not.

addJSON

(
  • key
  • url
  • [storeAsGlobal=true]
)
public

Creates a new File to store JSON and adds it to the loading queue.

Parameters:

  • key String

    The key for the file.

  • url String

    The url to the json file.

  • [storeAsGlobal=true] Boolean optional

    If the file should be stored globally.

addSpriteSheet

(
  • key
  • url
  • frameWidth
  • frameHeight
  • [numCells]
  • [rows]
  • [cols]
  • [sheetOffsetX]
  • [sheetOffsetY]
  • [cellOffsetX]
  • [cellOffsetY]
  • [storeAsGlobal=true]
)
public

Creates a new file for a spritesheet and adds the file to the loading queue.

Parameters:

  • key String

    The key for the file.

  • url String

    The url of the image to load.

  • frameWidth Number

    The width of a single cell in the spritesheet.

  • frameHeight Number

    The height of a single cell in the spritesheet.

  • [numCells] Number optional

    The number of cells that are in this spritesheet.

  • [rows] Number optional

    The number of cells that are in a row.

  • [cols] Number optional

    The number of cells that are in a column.

  • [sheetOffsetX] Number optional

    The offset of the whole spritesheet on the x axis.

  • [sheetOffsetY] Number optional

    The offset of the whole spritesheet on the y axis.

  • [cellOffsetX] Number optional

    The spacing between each cell on the x axis.

  • [cellOffsetY] Number optional

    The spacing between each cell on the y axis.

  • [storeAsGlobal=true] Boolean optional

addTextFile

(
  • key
  • url
  • [storeAsGlobal=true]
)
public

Creates a new File to store a text file and adds it to the loading queue.

Parameters:

  • key String

    The key for the file.

  • url String

    The url to the text file.

  • [storeAsGlobal=true] Boolean optional

    If the file should be stored globally.

addTextureAtlas

(
  • key
  • imageUrl
  • jsonID
  • jsonURL
  • [storeAsGlobal=true]
)
public

Creates new file's for loading a texture atlas and adds those files to the loading queue.

Parameters:

  • key String

    The key for the image file.

  • imageUrl String

    The url of the image to load.

  • jsonID String

    A key for the JSON file.

  • jsonURL String

    The url of the json file to load.

  • [storeAsGlobal=true] Boolean optional

    If hte files should be stored globally or not.

addToBytesTotal

(
  • file
)
private

Adds the number of bytes that a File is to the total number of bytes loaded.

Parameters:

addXML

(
  • key
  • url
  • [storeAsGlobal=true]
)
public

Creates a new File to store XML and adds it to the loading queue.

Parameters:

  • key String

    The key for the file.

  • url String

    The url to the xml file.

  • [storeAsGlobal=true] Boolean optional

    If the file should be stored globally.

attemptToAddAudio

(
  • key
  • url
  • [storeAsGlobal=true]
  • [onlyIfSupported=true]
)
private

This method firstly checks to see if the AUDIO file being loaded is supported or not by the browser/device before adding it to the loading queue. Returns a boolean if the audio file was successfully added or not to the file directory.

Parameters:

  • key String

    The key for the audio file.

  • url String

    The url of the audio to load.

  • [storeAsGlobal=true] Boolean optional

    If the file should be stored globally.

  • [onlyIfSupported=true] Boolean optional

    If the audio file should only be loaded if Kiwi detects that the audio file could be played. Set this to fa

boot

() public

The boot method is executed when the DOM has successfully loaded and we can now start the game.

calculateBytes

(
  • [value]
)
Boolean public

If true (and xhr/blob is available) the loader will get the bytes total of each file in the queue to give a much more accurate progress report during load If false the loader will use the file number as the progress value, i.e. if there are 4 files in the queue progress will get called 4 times (25, 50, 75, 100)

Parameters:

  • [value] Boolean optional

Returns:

Boolean:

complete

() Boolean public

Returns a boolean indicating if everything in the loading que has been loaded or not.

Returns:

Boolean:

fileLoadComplete

(
  • file
)
private

Executed when a file has been successfully loaded. This method then decides whether loading is complete or we need to load the next file.

Parameters:

fileLoadProgress

(
  • file
)
private

Executed whilst a file is being loaded.

Parameters:

getBytesLoaded

() Number public

Returns the total number of bytes that have been loaded so far.

Returns:

Number:

getNextFileSize

() private

Calculates the size of the new file that is to be loaded.

getPercentLoaded

() Number public

Returns a percentage of the amount that has been loaded so far.

Returns:

Number:

init

(
  • [progress=null]
  • [complete=null]
  • [calculateBytes=false]
)
public

Initialise the properities that are needed on this loader.

Parameters:

  • [progress=null] Any optional

    Progress callback method.

  • [complete=null] Any optional

    Complete callback method.

  • [calculateBytes=false] Boolean optional

nextFile

() private

Starts the loading of the next file in the list.

objType

() String public

The type of object that this is.

Returns:

String:

"Loader"

startLoad

() public

Loops through all of the files that need to be loaded and start the load event on them.

Properties

_bytesCurrent

Number private

Total number of bytes loaded from last completed file

_bytesLoaded

Number private

Total number of bytes loaded so far (out of _bytesTotal)

_bytesTotal

Number private

Total file size (in bytes) of all files to be loaded - only set if calculateBytes is true

_calculateBytes

Boolean private

If a real byte value calculation will be made prior to the load (much smoother progress bar but costs HEAD calls x total file count)

Default: true

_complete

Boolean private

Everything in the queue loaded?

_currentFile

Number private

The most recently loaded file (out of the total)

_fileChunk

Number private

When using the tag loader we don't have a byte total, just a X of files total - this holds the percentage each file from that total is worth

_fileList

Array private

A list of all of the files that need to be loaded. Each item in the array is of the type Kiwi.Files.File.

_fileTotal

Number private

Total number of files to be loaded

_game

Kiwi.Game private

The game that this loader belongs to.

_loadList

Array private

A list of all of the files that have been loaded. Each item in the array is of the type Kiwi.Files.File.

_onCompleteCallback

Function private

A callback that is to be called when the loader has finished loading files.

_onProgressCallback

Function private

A callback that is to be called while the loader is in the process of loading files.

_percentLoaded

Number private

The total % of the current queue that has been loaded