As therevills said, pre-cache the image. I've once written a Bmax class for this, here a conversion to monkey (untested):
'------------------------------------------------------------------------------
' Used so images are not loaded twice
' How to use: myImage:Image = CachedImage.Load( path )
'------------------------------------------------------------------------------
Class CachedImage
Private
'------------------------------------------------------------------------------
' This Map contains all images already loaded
' key: path:String | value: image:Image
'------------------------------------------------------------------------------
Global RessourceMap:StringMap<Image> = New StringMap<Image>
Public
'------------------------------------------------------------------------------
' Lookup in the RessourceMap if the Image is already loaded
' If loaded => Return the already loaded Image
' If Not loaded => Return the newly loaded Image
'------------------------------------------------------------------------------
Function Load:Image( path:Object, flags:Int = -1 )
If RessourceMap.Contains( path )
Return Image( RessourceMap.Get( path ) )
Else
Local image:Image = LoadImage( path, flags )
RessourceMap.Set( path, image )
Return image
End
End
'------------------------------------------------------------------------------
' Remove the image with the speciefied path
'------------------------------------------------------------------------------
Function Remove:Void( path:Object )
If RessourceMap.Get( path )
RessourceMap.Remove( path )
Return
End
End
'------------------------------------------------------------------------------
' Clear the RessourceMap of all loaded Images
' Info: You still have to manually clear all other references to Images
' in your own Classes
'------------------------------------------------------------------------------
Function Reset:Void()
RessourceMap.Clear()
End
End
The idea is that every image you load has a reference in the RessourceMap. Once loaded it won't load it again but only pass the reference to the new image.
Use it like this:
myImage:Image = CachedImage.Load( path )
|