
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
ImageViewer Implementation (Take 3)
|
149
Version 3 of our ImageViewer class is nearly complete. However, we have one more
issue to account for: cleaning up after each ImageViewer instance before it’s deleted.
Deleting a Class’s Resources
In programming, the fun’s not over until you’ve put your proverbial toys away. Each
instance of our ImageViewer class creates movie clip instances that persist in the
Flash Player until they are explicitly removed, even if the ImageViewer instance that
created them is, itself, deleted! For example, the following code creates an
ImageViewer instance, loads an image, and then deletes the ImageViewer instance:
var viewer:ImageViewer = new ImageViewer(this, 1, 100, 100,
250, 250, 10, 0xCE9A3C);
viewer.loadImage("picture.jpg");
delete viewer;
After the code executes—despite the statement delete viewer;—the picture.jpg loads
and appears on screen. Why? Because the clips created by the ImageViewer instance
were not removed before the variable
viewer was deleted.
// Display load progress in the on-screen text field.
// Divide bytesLoaded and bytesTotal by 1024 to get kilobytes.
container_mc.loadStatus_txt.text = "LOADING: "
+ Math.floor(bytesLoaded / 1024)
+ "/" + Math.floor(bytesTotal / 1024) + " KB";
}
public function onLoadInit (target:MovieClip):Void {
// ...