Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 2 of 2

Thread: dynamic loading question

  1. #1

    dynamic loading question

    I am using dynamic loading for sprites (Sirisian's TextureManager) ...

    I load when I start the game so I think I will need some loading screen and force people to wait.. but I am wondering if it's better to load one large file or many files.

    ie, does the number of files matter when cumulative(?) image size will stay the same?

    thanks.

  2. #2
    Quote Originally Posted by misterooga View Post
    I am using dynamic loading for sprites (Sirisian's TextureManager) ...

    I load when I start the game so I think I will need some loading screen and force people to wait.. but I am wondering if it's better to load one large file or many files.

    ie, does the number of files matter when cumulative(?) image size will stay the same?

    thanks.
    The main reason why I posted this question was this: I was using--probably outdated version of--sirisian's texture manager and every other time, the loader will go into infinite loop. This happens when I try to load up front.

    In case anyone is interested, I post the code with a part I added to fix the issue.

    Code:
    package engine.texture {
    	import flash.display.BitmapData;
    	public class Texture {
    		public var loaded:Boolean;
    		public var ID:String;
    		public var bitmap:BitmapData;
    		public function Texture(ID_:String) {
    			loaded = false;
    			ID = ID_;
    		}
    	}
    }
    
    package engine.texture {
    	import flash.display.*;
    	import flash.events.*;
    	import flash.net.URLRequest;
    	public class TextureManager {
    		//All the textures.
    		public var textures:Array;
    		//Array for textures waiting to be loaded.
    		public var textureLoadQueue:Array;
    		public var readyToLoadImage:Boolean;
    		public var URLpath:String;
    		public var textureTemp:Texture;
    		
    		// JK added
    		public var numItems:Number = 0;
    		public var numItemsLoaded:Number = 0;
    		public var loadDelayCounter:Number = 0;
    		
    		public function TextureManager(URLpath_:String) {
    			URLpath = URLpath_;
    			textures = new Array();
    			textureLoadQueue = new Array();
    			readyToLoadImage = true;
    			
    			
    			// Loading graphics
    			
    			numItems = 3; // hardcoded value
    			//this.Load("Game_Ending");
    			//this.Load("CharacterSheet1");			
    			//this.Load("CharacterSheet2");			
    			
    			
    		}
    		public function Load(imageID_:String):void {
    			//Allocate a spot to identify if the texture is loaded
    			textures[imageID_] = new Texture(imageID_);
    			//Add to the load queue to download the image file
    			textureLoadQueue.push(textures[imageID_]);
    		}
    		
    		public function allLoaded():Boolean { return (numItems == numItemsLoaded); }
    		
    		public function LoadQueue():void {
    			if (textureLoadQueue.length>0) {
    				if (readyToLoadImage) {
    					loadDelayCounter = 0;
    					textureTemp = textureLoadQueue.shift();
    					trace("Loading: "+textureTemp.ID);
    					readyToLoadImage = false;
    					var loader:Loader = new Loader();
    					var urlReq:URLRequest = new URLRequest(URLpath+textureTemp.ID+".png");
    					loader.contentLoaderInfo.addEventListener(Event.INIT,completedLoading);
    					loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorLoading);
    					loader.load(urlReq);
    				} else {
    					loadDelayCounter++;
    					trace("Texture Loading...");
    					
    					// JK _ if it keeps repeating for 5 times or more, try force reloading 
    					if (loadDelayCounter > 5 ) {
    						//loader.contentLoaderInfo.removeEventListener(Event.INIT,completedLoading);
    						//loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, errorLoading);
    						textureLoadQueue.unshift(textureTemp);
    						readyToLoadImage = true;						
    					}
    				}
    			}
    		}
    		public function completedLoading(eventObj:Object):void{
    			trace("Done Loading: " + textureTemp.ID);
    			var loader:Loader = eventObj.target.loader;
                var info:LoaderInfo = loader.contentLoaderInfo;
    			textureTemp.bitmap = Bitmap(loader.content).bitmapData;	
    			textureTemp.loaded = true;
    			readyToLoadImage = true;
    			numItemsLoaded ++;
    		}
    		public function errorLoading(eventObj:Object):void{
    			trace("Error Loading Image: " + eventObj);
    			//An error occured. By default the next image will load: NOTE: if needed you may want to uncomment the lines that follow given what you want to do:
    			//textureLoadQueue.unshift(textureTemp);//This will try to load the image again.
    			//textureLoadQueue.push(textureTemp);//This will try to load the image again after the rest of the images.
    			readyToLoadImage = true;
    		}
    		//Does not check if the image is actually loaded or exists, just returns the texture if found
    		public function GetTexture(ID_:String):Texture {
    			return textures[ID_];
    		}
    		
    	}
    }
    All I added to fix is the little part in LoadQueue(). I also added allLoaded() check function.

    Note that this works if and only if you are loading all the files up front; we did it this way for our game since we didn't have too much graphics AND we got lazy and didn't want to bother loading at various other places.. heh.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012