AS2 preloader troubles

Subscribe to AS2 preloader troubles 16 posts

Sign in to reply


 
avatar for MCOBigBen MCOBigBen 256 posts
Flag Post

Sorry if my mistake is a basic one, they usually are…

I’m trying to make a preloader in a separate scene from my game. I’ve drawn a loading bar, assigned it as “preloader”, and then created this preloader.as:

class preloader extends MovieClip
{

	function onEnterFrame()
	{
		amount = this.getBytesLoaded() / this.getBytesTotal();
		preloader._xscale = amount;
		
		if (amount == 1)
		{
			this.gotoAndStop(2);
		}
		
	}
}

The end result is the loading scene and the game flipping back and forth rapidly, and the debug error “The property being referenced does not have the static attribute.”

What am I doing wrong here?

 
avatar for Darty Darty 129 posts
Flag Post

the debug error you are getting is because of this line

    preloader._xscale = amount;

Change it to

    this._xscale = amount;
 
avatar for MCOBigBen MCOBigBen 256 posts
Flag Post

Thanks! Fixing that (and declaring var amount) gets it to run without any errror messages, but the resulting animation just thrashes back and forth between my preloader scene and the game scene. How do I make it stay on the preloader scene until it’s loaded, and then switch?

 
avatar for Darty Darty 129 posts
Flag Post

to sort that you need to add

    stop();

before the preloader class

 
avatar for Moonkey Moonkey 1007 posts
Flag Post

and since this class is controlling your preloader, this.gotoAndStop(2); will make your preloader go to frame 2, not your main timeline.

One way to solve that would be changing it to _root.gotoAndStop(2);

 
avatar for MCOBigBen MCOBigBen 256 posts
Flag Post

Drat, I think I’ve mucked things up. gotoAndStop takes me to a frame (right?), when I’ve actually put the game in a different scene, and not a different frame.

Adding stop(); to the preloader class throws an error. I added stop(); to the Actions panel in the frame, and it doesn’t thrash back and forth, it now starts at the preloader, but the load bar never advances.

 
avatar for Moonkey Moonkey 1007 posts
Flag Post

Drat, I think I’ve mucked things up. gotoAndStop takes me to a frame (right?), when I’ve actually put the game in a different scene, and not a different frame.

You can use gotoAndStop("Scene 2", 1); to go to frame 1 of scene 2. Although gotoAndStop(2) would probably work anyway if Scene 1 is only 1 frame, since all the scenes are essentially just one big timeline.
Its a good idea to use framelabels for stuff like this. Then you can use gotoAndStop(“frame label”) and it’ll work regardless of what scene its in.

but the load bar never advances.

In as2, _xscale is a percentage. It goes up to 100 rather than being a decimal, so your code will only ever fill it to 1% at most.

 
avatar for MCOBigBen MCOBigBen 256 posts
Flag Post

Ahhh, it’s a percentage in 0-100 form. I was expecting 0-1.0. Thanks, I’ll try that when I get home.

I really appreciate the help Moonkey and Darty!

 
avatar for MCOBigBen MCOBigBen 256 posts
Flag Post

Baby steps, baby steps….

I added a multiplication by 100 to the calculation of the loading, and now my bar jumps to a bright full green…..and nothing else happens. I tried having the load amount displayed as a dynamic text firled to see if it’s not going all the way to 100, and I’m getting “The class or interface ‘preloaderNumber’ could not be loaded.” I thought I set it up the same way I set up the score dynamic text, but something’s obviously not right.

Can anyone see what I’m doing wrong?

class preloader extends MovieClip
{
	

	var amount;
	
	function onEnterFrame()
	{
		amount = ((this.getBytesLoaded() / this.getBytesTotal()) * 100);
		
		this._xscale = amount;
		
		_root.preloaderNumber.text = amount;
		
		if (amount == 100)
		{
			_root.gotoAndPlay("Scene 1");
		}
		
	}
}
 
avatar for UnknownGuardian UnknownGuardian 6220 posts
Flag Post

You never defined preloaderNumber. Its probably somewhere else. If its located in the preloader’s movieclip, then I believe you would say this.preloaderNumber.text = amount;

 
avatar for MCOBigBen MCOBigBen 256 posts
Flag Post

You’re right, I didn’t define it. I was actually throwing the text up as a troublshooting step, but I found Flash’s debug list of variables, so I can see that “amount” is indeed 100.

But _root.gotoAndPlay(“Scene 1”); still isn’t advancing to my actual game, I just get the pre-loader screen forever.

Anybody have an idea of what I’m doing wrong?

 
avatar for Jabor Jabor 11382 posts
Flag Post

Is “Scene 1” actually a frame label of the part of your game past your preloader?

 
avatar for MCOBigBen MCOBigBen 256 posts
Flag Post

Scene 1 is the name of the scene which contains the game.

[edit]
OK, I’ve created a frame label for the first (and only) frame of the scene the game is in. I’m now calling the label when the load amount reaches 100%

The result is that now the game thrashes between the preloader and the game again, flickering back and forth.

Previously, Darty recommended adding stop(); “before” the preloader class. I’m a little shaky on how the before and after thing works, and what controls the order. I added the stop(); to the Actions pane of the first (and only) frame of the preloader scene. Is this appropriate, or is this what’s messing me up?

 
avatar for Darty Darty 129 posts
Flag Post

you need to add

    stop();

at the beginning of every frame. This tells the swf to stop running the main timeline(the 1 containing both your preloader and game frames) until the current frame tells it otherwise(via a gotoAndStop() or gotoAndPlay())

so for example you have added stop(); at the beginning of the preloader frame. and only call the gotoAndPlay() function if amount == 100.

This is essentially saying you dont want to leave the preloader frame until everything is loaded. You also now have a game frame(which you want to stay in until the game is completed) so you will again need to add a stop(); at the beginning of that frame otherwise the main timeline will start running again(which is what is causing the thrashing).

 
avatar for MCOBigBen MCOBigBen 256 posts
Flag Post

That did it! Awesome, thank you so much!

 
avatar for Stef1987 Stef1987 60 posts
Flag Post

I’m also making a preloader, but I’ve done it a bit differently:
the preloader is a seperate class (instead of using the timeline and stuff, I create the preloader object and when it’s loaded, it calls the loaded() function of the game).

But now it only shows the preloader for 1 frame, once it’s completely loaded (and then the next frame the game starts ofcourse).
Before this, I just see the document’s background color (instead of the preloader).
(It’s like it waits for everything to be loaded before it shows anything at all)

Any ideas why this is ?

EDIT: Created separate topic for this question: here

Sign in to reply