Noob Flash Rss

ActionScript 3.0 Memory Management

0

Posted by Aaron | Posted in ActionScript | Posted on 19-07-2007

Memory management code directly from the man at gskinner.com.

import flash.system.System;
import flash.net.navigateToURL;
import flash.net.URLRequest;
...
// check our memory every 1 second:
var checkMemoryIntervalID:uint = setInterval(checkMemoryUsage,1000);
...
var showWarning:Boolean = true;
var warningMemory:uint = 1000*1000*500;
var abortMemory:uint = 1000*1000*625;
...
function checkMemoryUsage() {
   if (System.totalMemory > warningMemory && showWarning) {
      // show an error to the user warning them that we're running out of memory and might quit
      // try to free up memory if possible
      showWarning = false; // so we don't show an error every second
    } else if (System.totalMemory > abortMemory) {
      // save current user data to an LSO for recovery later?
      abort();
    }
}
function abort() {
   // send the user to a page explaining what happpened:
   navigateToURL(new URLRequest("memoryError.html"));
}

You can find other system management tips at gskinner.com

AS3 Dynamic Alpha Gradient Mask

3

Posted by Aaron | Posted in ActionScript | Posted on 16-07-2007

In the process of creating an image gallery in AS3, I ran across this interesting error.

1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:MovieClip.

Incorrect Code:

public class ImageGallery extends Component {
		private var ldrData:URLLoader;
		private var count:uint;
		var holder:MovieClip;
		var myMask:MovieClip;
		public function ImageGallery() {
			count = 0;
			myMask = new MovieClip();
			holder = new MovieClip();
			addChild(holder);
			holder.x = 50;
			holder.y = 0;
			myMask = addChild(new mask_mc());
			holder.cacheAsBitmap = true;
			myMask.cacheAsBitmap = true;
			holder.mask = myMask;
			if (this.stage != null) {
				//Master.SetParameter("ModelId","100")
				this.Initialize();
			}
		}
       ...
}

The fix for this error was simple. When declaring myMask = addChild(new mask_mc()), you can cast your variable using “as MovieClip” to fix the problem.

Corrected Code:

public class ImageGallery extends Component {
		private var ldrData:URLLoader;
		private var count:uint;
		var holder:MovieClip;
		var myMask:MovieClip;
		public function ImageGallery() {
			count = 0;
			myMask = new MovieClip();
			holder = new MovieClip();
			addChild(holder);
			holder.x = 50;
			holder.y = 0;
			myMask = addChild(new mask_mc()) as MovieClip;
			holder.cacheAsBitmap = true;
			myMask.cacheAsBitmap = true;
			holder.mask = myMask;
			if (this.stage != null) {
				//Master.SetParameter("ModelId","100")
				this.Initialize();
			}
              ...
     }