purge tiles when they go off-screen. This basically removes the single biggest memory...
authorRichard Fairhurst <richard@systemed.net>
Sun, 6 Mar 2011 19:16:13 +0000 (19:16 +0000)
committerTom Hughes <tom@compton.nu>
Wed, 9 Mar 2011 11:27:22 +0000 (11:27 +0000)
net/systemeD/halcyon/TileSet.as

index 36e4a12ea144a7e3cd04decf643fd16cc795db40..12050f48d614c5bf7ab0ceac5804e7931307d8c7 100644 (file)
@@ -20,12 +20,14 @@ package net.systemeD.halcyon {
                private var requests:Array=[];
                private var tiles:Object={};            // key is "z,x,y"; value "true" if queued, or reference to loader object if requested
                private var waiting:int=0;                      // number of tiles currently being downloaded
+               private var loadcount:int=0;            // number of tiles fully downloaded
                private var baseurl:String;                     // e.g. http://npe.openstreetmap.org/$z/$x/$y.png
                private var scheme:String;                      // 900913 or microsoft
                public var blocks:Array;                        // array of regexes which are verboten
 
                private var map:Map;
-               private const MAXTILEREQUESTS:int= 4;
+               private const MAXTILEREQUESTS:uint= 4;
+               private const MAXTILESLOADED:uint=30;
 
                private var sharpenFilter:BitmapFilter = new ConvolutionFilter(3, 3, 
                        [0, -1, 0,
@@ -182,12 +184,25 @@ package net.systemeD.halcyon {
                        t.addEventListener(TimerEvent.TIMER,function():void { upFade(DisplayObject(event.target.loader)); });
                        t.start();
                        waiting--;
+                       loadcount++;
+                       if (loadcount>MAXTILESLOADED) purgeTiles();
                        return;
                }
                
                protected function upFade(s:DisplayObject):void {
                        s.alpha+=0.1;
                }
+               
+               protected function purgeTiles():void {
+                       for (var tile:String in tiles) {
+                               var coords:Array=tile.split(','); var tz:uint=coords[0]; var tx:uint=coords[1]; var ty:uint=coords[1];
+                               if (tz!=map.scale || tx<tile_l || tx>tile_r || ty<tile_t || ty<tile_b) {
+                                       if (tiles[tile].parent) tiles[tile].parent.removeChild(tiles[tile]);
+                                       delete tiles[tile];
+                                       loadcount--;
+                               }
+                       }
+               }
 
                
                // Assemble tile URL