Kiwi.Files.Loader Class
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
Parameters:
-
game
Kiwi.GameThe game that this loader belongs to.
Returns:
This Object
Item Index
Methods
- _attemptToAddAudio
- _calculateNextFileSize
- _detailsReceived
- _fileQueueUpdate
- _parallelFileComplete
- _queueFileComplete
- _sortFile
- _startLoading
- _startLoadingAllParallel
- _startLoadingParallel
- _startLoadingQueue
- _updateFileListInformation
- addAudio
- addBinaryFile
- addFileToQueue
- addImage
- addJSON
- addSpriteSheet
- addTextFile
- addTextureAtlas
- addXML
- boot
- calculateBytes
- calculateQueuedSize
- clearQueue
- complete deprecated
- destroy
- getBytesLoaded deprecated
- getPercentLoaded deprecated
- init deprecated
- loadFile
- objType
- onQueueComplete
- onQueueProgress
- removeFileFromQueue
- start
- startLoad deprecated
Methods
_attemptToAddAudio
-
params
-
[onlyIfSupported=true]
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:
-
params
Object-
key
StringThe key for the audio file.
-
url
StringThe url of the audio to load.
-
[state=true]
Kiwi.State optionalThe state this file should be for.
-
[fileStore]
Kiwi.Files.FileStore optional
-
-
[onlyIfSupported=true]
Boolean optionalIf the audio file should only be loaded if Kiwi detects that the audio file could be played.
Returns:
The file which was created.
_calculateNextFileSize
()
private
Checks to see if all the file sizes have been retrieved. If so completes the "calculateQueuedSize" call. Otherwise requests the next file's details.
_detailsReceived
()
private
Executed when by '_calculateNextFileSize' when the files information has been retrieved. Adds its calculated size to the _bytesTotal and executes the 'nextFileSize' method.
_fileQueueUpdate
-
file
-
[forceProgressCheck=false]
Executed by files when they have successfully been loaded. This method checks to see if the files are in the file queue, and dispatches the appropriate events.
Parameters:
-
file
Kiwi.Files.FileThe file which has been recently loaded.
-
[forceProgressCheck=false]
Boolean optionalIf the progress of file loading should be checked, regardless of the file being in the queue or not.
_parallelFileComplete
-
file
Executed when a file in the 'loadingParallel' lsit has been successfully loaded. Removes the file from the list and get the fileQueue to check its progress.
Parameters:
-
file
Kiwi.Files.File
_queueFileComplete
-
file
Executed when a file in the 'loadingQueue' has been successfully loaded. Removes the file from the loadingQueue and executes the '_startLoadingQueue' to start loading the next file.
Parameters:
-
file
Kiwi.Files.File
_sortFile
-
file
Sorts a file and places it into either the 'loadingParallel' or 'loadingQueue' depending on the method of loading it is using.
Parameters:
-
file
Kiwi.Files.File
_startLoading
()
private
Loops through the file queue and starts the loading process.
_startLoadingAllParallel
()
private
Starts loading all files which can be loaded in parallel.
_startLoadingParallel
-
params
Starts loading a file which can be loaded in parallel.
Parameters:
-
params
Kiwi.Files.Filefile
_startLoadingQueue
()
Boolean
private
Starts the loading process in the loadingQueue.
Returns:
Whether the first file is loading
_updateFileListInformation
()
private
Called each time a file has processed whilst loading, or has just completed loading.
Calculates the new number of bytes loaded and the percentage of loading done by looping through all of the files.
addAudio
-
key
-
url
-
[storeAsGlobal=true]
-
[onlyIfSupported=true]
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
StringThe key for the audio file.
-
url
StringThe 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 optionalIf the file should be stored globally.
-
[onlyIfSupported=true]
Boolean optionalIf the audio file should only be loaded if Kiwi detects that the audio file could be played.
Returns:
The file which was created.
addBinaryFile
-
key
-
url
-
[storeAsGlobal=true]
Creates a new File for a Binary file and adds it to the loading queue.
Parameters:
-
key
StringThe key for the file.
-
url
StringThe url to the Binary file.
-
[storeAsGlobal=true]
Boolean optionalIf the file should be stored globally.
Returns:
The file which was created.
addFileToQueue
-
file
Adds a file to the queue of files to be loaded. Files cannot be added whilst the queue is currently loading, the file to add is currently loading, or has been loaded before.
Parameters:
-
file
Kiwi.Files.FileThe file to add.
Returns:
If the file was added to the queue or not.
addImage
-
key
-
url
-
[width]
-
[height]
-
[offsetX]
-
[offsetY]
-
[storeAsGlobal=true]
Creates a new file for an image and adds a the file to loading queue.
Parameters:
-
key
StringThe key for the file.
-
url
StringThe url of the image to load.
-
[width]
Number optionalThe width of the cell on the image to use once the image is loaded.
-
[height]
Number optionalThe height of the cell on the image to use once the image is loaded.
-
[offsetX]
Number optionalAn offset on the x axis of the cell.
-
[offsetY]
Number optionalAn offset of the y axis of the cell.
-
[storeAsGlobal=true]
Boolean optionalIf the image should be stored globally or not.
Returns:
The file which was created.
addJSON
-
key
-
url
-
[storeAsGlobal=true]
Creates a new File to store JSON and adds it to the loading queue.
Parameters:
-
key
StringThe key for the file.
-
url
StringThe url to the json file.
-
[storeAsGlobal=true]
Boolean optionalIf the file should be stored globally.
Returns:
The file which was created.
addSpriteSheet
-
key
-
url
-
frameWidth
-
frameHeight
-
[numCells]
-
[rows]
-
[cols]
-
[sheetOffsetX]
-
[sheetOffsetY]
-
[cellOffsetX]
-
[cellOffsetY]
-
[storeAsGlobal=true]
Creates a new file for a spritesheet and adds the file to the loading queue.
Parameters:
-
key
StringThe key for the file.
-
url
StringThe url of the image to load.
-
frameWidth
NumberThe width of a single cell in the spritesheet.
-
frameHeight
NumberThe height of a single cell in the spritesheet.
-
[numCells]
Number optionalThe number of cells that are in this spritesheet.
-
[rows]
Number optionalThe number of cells that are in a row.
-
[cols]
Number optionalThe number of cells that are in a column.
-
[sheetOffsetX]
Number optionalThe offset of the whole spritesheet on the x axis.
-
[sheetOffsetY]
Number optionalThe offset of the whole spritesheet on the y axis.
-
[cellOffsetX]
Number optionalThe spacing between each cell on the x axis.
-
[cellOffsetY]
Number optionalThe spacing between each cell on the y axis.
-
[storeAsGlobal=true]
Boolean optional
Returns:
The file which was created.
addTextFile
-
key
-
url
-
[storeAsGlobal=true]
Creates a new File to store a text file and adds it to the loading queue.
Parameters:
-
key
StringThe key for the file.
-
url
StringThe url to the text file.
-
[storeAsGlobal=true]
Boolean optionalIf the file should be stored globally.
Returns:
The file which was created.
addTextureAtlas
-
key
-
imageUrl
-
jsonID
-
jsonURL
-
[storeAsGlobal=true]
Creates new file's for loading a texture atlas and adds those files to the loading queue.
Parameters:
-
key
StringThe key for the image file.
-
imageUrl
StringThe url of the image to load.
-
jsonID
StringA key for the JSON file.
-
jsonURL
StringThe url of the json file to load.
-
[storeAsGlobal=true]
Boolean optionalIf hte files should be stored globally or not.
Returns:
The file which was created.
addXML
-
key
-
url
-
[storeAsGlobal=true]
Creates a new File to store XML and adds it to the loading queue.
Parameters:
-
key
StringThe key for the file.
-
url
StringThe url to the xml file.
-
[storeAsGlobal=true]
Boolean optionalIf the file should be stored globally.
Returns:
The file which was created.
boot
()
public
The boot method is executed when the DOM has successfully loaded and we can now start the game.
calculateBytes
-
[value]
Quick way of getting / setting the private variable 'calculateBytes' To be made into a public variable once removed.
Parameters:
-
[value]
Boolean optional
Returns:
calculateQueuedSize
-
callback
-
[context=null]
Loops through the file queue and gets file information (filesize, ETag, filetype) for each.
To get accurate information about the bytesLoaded, bytesTotal, and the percentLoaded set the 'calculateBytes' property to true, as the loader will automatically execute this method before hand.
Can only be executed when the file queue is not currently loading.
Parameters:
-
callback
Any -
[context=null]
Any optional
clearQueue
()
public
Clears the file queue of all files.
complete
()
Boolean
deprecated
public
Returns a boolean indicating if everything in the loading que has been loaded or not.
Returns:
destroy
()
public
Flags this loader for garbage collection. Only use this method if you are SURE you will no longer need it. Otherwise it is best to leave it alone.
getBytesLoaded
()
Number
deprecated
public
Returns the total number of bytes that have been loaded so far from files in the file queue.
Returns:
getPercentLoaded
()
Number
deprecated
public
Returns a percentage of the amount that has been loaded so far.
Returns:
init
-
[progress=null]
-
[complete=null]
-
[calculateBytes=false]
Initialise the properities that are needed on this loader. Recommended you use the 'onQueueProgress' / 'onQueueComplete' signals instead.
Parameters:
-
[progress=null]
Any optionalProgress callback method.
-
[complete=null]
Any optionalComplete callback method.
-
[calculateBytes=false]
Boolean optional
loadFile
()
public
Starts the process of loading a file outside of the regular queue loading process. Callbacks for load completion need to be added onto the file via 'onComplete' Signal.
objType
()
String
public
The type of object this is.
Returns:
"Loader"
onQueueComplete
()
public
A Signal which dispatches callbacks when all files in the 'loadingList' have been loaded. When adding callbacks make sure to 'remove' them (or to use the 'addOnce' method) otherwise will fire when other sections use the loader.
onQueueProgress
()
public
A Signal which dispatches callbacks each time a file in the 'loadingList' have been loaded. Callbacks dispatched are passed the following arguments in order.
- percent - The percentage of files loaded. A number from 0 - 100
- bytesLoaded - The number of bytes loaded
- file - The latest file that was loaded. First call will be null.
When adding callbacks make sure to 'remove' them (or to use the 'addOnce' method) otherwise will fire when other sections use the loader.
removeFileFromQueue
-
file
Removes a file from the file queue. Files cannot be removed whilst the queue is loading.
Parameters:
-
file
Kiwi.Files.FileThe file to remove.
Returns:
If the file was added to the queue or not.
start
-
[calculateBytes]
Starts loading all the files which are in the file queue.
To accurately use the bytesLoaded or bytesTotal properties you will need to set the 'calculateBytes' boolean to true. This may increase load times, as each file in the queue will firstly make XHR HEAD requests for information.
When 'calculateBytes' is true the percentLoaded will be the bytesLoaded / bytesTotal
.
Otherwise it is based on the filesLoaded / numberOfFilesToLoad
.
Parameters:
-
[calculateBytes]
Boolean optionalSetter for the 'calculateBytes' property.
startLoad
()
deprecated
public
Loops through all of the files that need to be loaded and start the load event on them.
Properties
_bytesLoaded
Number
private
The number of bytes loaded of files in the file queue.
_bytesTotal
Number
private
Total file size (in bytes) of all files in the queue to be loaded.
_calculateBytes
Boolean
private
If the number of bytes for each file should be calculated before the queue starts loading. If true each file in the queue makes a XHR HEAD request first to get the total values.
_completeFiles
Number
private
The number of files in the file queue that have been updated.
Default: 0
_currentFileIndex
Number
private
The index of the current file in the filelist thats size is being retrieved.
_fileQueueLoading
Boolean
private
A flag indicating if the files inside the file queue are loading or not.
Default: false
_loadingList
Array
private
List of files that are to be loaded. These files will be placed in the '_loadingQueue' or '_loadingParallel' lists when the queue is told to 'start' loading.
_loadingParallel
Array
private
A list of files that can be loaded in parallel to one another. This list of files are currently being loaded.
_loadingQueue
Array
private
List of files that cannot load in parallel to one another and so need to wait for previous files to load first. Generally files loaded via XHR.
bytesLoaded
Number
public
READ ONLY: Returns the total number of bytes for the files in the file queue.
If you are using this make sure you set the 'calculateBytes' property to true OR execute the 'calculateQueuedSize' method. Otherwise files that are loaded via tags will not be accurate!
Default: 0
bytesTotal
Number
public
READ ONLY: Returns the total number of bytes for the files in the file queue. Only contains a value if you use the 'calculateBytes' and are loading files OR if you use the 'calculateQueuedSize' method.
Default: 0
enableParallelLoading
Boolean
public
When enabled, files which can be loaded in parallel (those which are loaded via tags) will be loaded at the same time.
The default behaviour is to have the files loading in a queued fashion instead of one after another.
Default: false
fileQueueLoading
Boolean
public
READ ONLY: A flag indicating if the files inside the file queue are loading or not.
Default: false
onQueueSizeCalculate
Any
private
Callback for when the total number of bytes of the files in the file list has been calculated.
onSizeContext
Any
private
Context that the onSizeCallback should be executed in.
percentLoaded
Number
public
When 'calculateBytes' is true the percentLoaded will be the bytesLoaded / bytesTotal
.
Otherwise it is based on the filesLoaded / numberOfFilesToLoad
.