Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scope when using new

Status
Not open for further replies.

peterworth

Programmer
Aug 18, 2004
80
GB
i'm not sure how scope works in actionscript - am i doing something wrong in the code below?

the loop runs 3 times, so there should be 3 movieclip loader objects and 3 listeners. but in the output, there is only 1 "loaded" trace. i.e. only one of the listener objects has called the onLoadInit function.

any advice appreciated!

Code:
trace("length "+content_xml.firstChild.childNodes.length);
		for (var i = 0; i < content_xml.firstChild.childNodes.length; i++) {
			var lo = new Object();
			var mcl = new MovieClipLoader();
			createEmptyMovieClip("floorplan"+i, this.getNextHighestDepth());
			mcl.addListener(new Object());
			lo.onLoadInit = function() {
				trace("loaded");
				floorplans[i]._width = floorplanWidth;
				floorplans[i]._height = floorplanHeight;
				floorplans[i]._x = offStageRight;
				floorplans[i]._y = floorplanY;
			};
			trace("loading "+content_xml.firstChild.childNodes[i].attributes.image);
			mcl.loadClip(content_xml.firstChild.childNodes[i].attributes.image, eval("floorplan"+i));
		}
 
Try something like this:
Code:
var lo = new Object();
var mcl = new MovieClipLoader();
lo.onLoadInit = function(target) {
	trace(target+" loaded");
	target._width = floorplanWidth;
	target._height = floorplanHeight;
	target._x = offStageRight;
	target._y = floorplanY;
};
mcl.addListener(lo);
for (var i = 0; i<content_xml.firstChild.childNodes.length; i++) {
	var mc = this.createEmptyMovieClip("floorplan"+i, this.getNextHighestDepth());
	mcl.loadClip(content_xml.firstChild.childNodes[i].attributes.image, mc);
}

Kenneth Kawamoto
 
i've got the below now, but the output is still wrong - 3 "loading..." but only 1 "loaded".

Code:
var lo = new Object();
		var mcl = new MovieClipLoader();
		lo.onLoadInit = function(target) {
			trace(target+" loaded");
			target._width = floorplanWidth;
			target._height = floorplanHeight;
			target._x = offStageRight;
			target._y = floorplanY;
		};
		mcl.addListener(lo);
		for (i = 0; i<content_xml.firstChild.childNodes.length; i++) {
			createEmptyMovieClip("floorplan"+i, this.getNextHighestDepth());
			trace("loading...");
			mcl.loadClip(content_xml.firstChild.childNodes[i].attributes.image, eval("floorplan"+i));
		}
 
the correct output (the name of the image files which are to be loaded). and the images load fine and appear on the stage. it's just the listener which isnt working...
 
I don't have your data so I had to change the script a little for testing:
Code:
var lo = new Object();
var mcl = new MovieClipLoader();
lo.onLoadInit = function(target) {
	trace(target+" loaded");
	target._width = floorplanWidth;
	target._height = floorplanHeight;
	target._x = offStageRight;
	target._y = floorplanY;
};
mcl.addListener(lo);
for (i=0; i<3; i++) {
	var mc = this.createEmptyMovieClip("floorplan"+i, this.getNextHighestDepth());
	trace(mc+" loading...");
	mcl.loadClip("dummy.swf", mc);
}

I get this output:
[tt]
_level0.floorplan0 loading...
_level0.floorplan1 loading...
_level0.floorplan2 loading...
_level0.floorplan2 loaded
_level0.floorplan1 loaded
_level0.floorplan0 loaded
[/tt]

Thus the code is fine. Add the following - what do you get for these?
Code:
lo.onLoadProgress = function(target, loadedBytes, totalBytes) {
	trace(target+": "+loadedBytes+" bytes of "+totalBytes+" bytes loaded");
};
lo.onLoadComplete = function(target, httpStatus) {
	trace(target+" load complete - status "+httpStatus);
};

Kenneth Kawamoto
 
hmmm, strange.. i've even tried having a different mcl and lo objects for each image( as in code below), and the output is still the same. with the extra traces, the output is this:

loading floorplan1.jpg
loading floorplan2.jpg
loading floorplan3.jpg
_level0.floorplan2: 65536 bytes of 79705 bytes loaded
_level0.floorplan2: 79705 bytes of 79705 bytes loaded
_level0.floorplan2 load complete - status 0
loaded _level0.floorplan2

Code:
		var loArr = new Array();
		var mclArr = new Array();
		for (i = 0; i<content_xml.firstChild.childNodes.length; i++) {
			loArr[i] = new Object();
			mclArr[i] = new MovieClipLoader();
			loArr[i].onLoadInit = function(target) {
				trace("loaded "+target);
				target._width = floorplanWidth;
				target._height = floorplanHeight;
				target._x = offStageRight;
				target._y = floorplanY;
			};
			loArr[i].onLoadProgress = function(target, loadedBytes, totalBytes) {
				trace(target+": "+loadedBytes+" bytes of "+totalBytes+" bytes loaded");
			};
			loArr[i].onLoadComplete = function(target, httpStatus) {
				trace(target+" load complete - status "+httpStatus);
			};
			mclArr[i].addListener(loArr[i]);
			createEmptyMovieClip("floorplan"+i, this.getNextHighestDepth());
			trace("loading "+content_xml.firstChild.childNodes[i].attributes.image);
			mclArr[i].loadClip(content_xml.firstChild.childNodes[i].attributes.image, eval("floorplan"+i));
		}
 
No need for creating more than one MovieClipLoader/Listener Object (waste of memory).

Anyway I took your version of code and tested with minimal modification. I get this:
[tt]
loading floorplan1.jpg
loading floorplan2.jpg
loading floorplan3.jpg
_level0.floorplan0: 15253 bytes of 15253 bytes loaded
_level0.floorplan0 load complete - status 0
_level0.floorplan1: 15302 bytes of 15302 bytes loaded
_level0.floorplan1 load complete - status 0
_level0.floorplan2: 15188 bytes of 15188 bytes loaded
_level0.floorplan2 load complete - status 0
loaded _level0.floorplan2
loaded _level0.floorplan1
loaded _level0.floorplan0
[/tt]

Can you test with different JPEG (replace floorplan1.jpg and floorplan2.jpg)?

Or Control > Delete ASO Files and Test Movie
(well, I doubt this works!)


Kenneth Kawamoto
 
The problem is you're doing everything within XML.onLoad function.

Do it like this:
Code:
content_xml.onLoad = function(success) {
	if (!success) {
		trace('could not load content');
	} else {
		[b]loadImages();[/b]
	}
};
content_xml.load('test.xml');
[b]function loadImages() {
	// your JPEG loading code here
}[/b]


Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top