Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 10-26-2009, 02:00 PM   #1
Misterbluedot
Newbie, not n00b!
 
Join Date: Oct 2009
Location: Washington, where the weather changes every 15 minutes.
Posts: 37
Question preloader problems

I'm trying to get a preloader attached to a game and working but I'm not sure whats going wrong here. When I test my game, it starts up and plays fine like it should normally. but I don't see the preloader. The output window also give me this error on every one of my movieclip linked classes that are in a asset holder MC on the second frame: ArgumentError: Error #1063: Argument count mismatch on com.asgamer.basics1::Ship(). Expected 1, got 0.

If I test my movie, then click simulate download, the preloader screen pops up, but the progress bar is fully stretched out from the beginning. After it gets done loading, it switches to the game, but it runs very very slowly. I can still control the player ship, but everything moves at a snails pace.

I'm at my wits end here, I would move on to something else and come back to this later, but a preloader is kind of important if you want to put any flash on the web. Can anyone take a look at my code and .fla and tell me what's going on, and what I need to do to make the preloader work?

I'm attaching a .zip file with all the files I'm working with to this post. The game is the source code from a tutorial over at asgamer.com. I've been using this to practice on so I don't break my personal projects. If anyone can help out, I would really appreciate it!
Attached Files
File Type: zip almost working.zip (252.9 KB, 45 views)
Misterbluedot is offline   Reply With Quote
Old 10-26-2009, 05:01 PM   #2
Misterbluedot
Newbie, not n00b!
 
Join Date: Oct 2009
Location: Washington, where the weather changes every 15 minutes.
Posts: 37
Default

Bump.

Please help me, I've been trying to get this preloader working for a week, but every time I think I'm getting some where I fall further back. I fix one error, and 2 more pop up. I'm new to AS3, so my knowledge of what's causing these errors is limited, and I don't know how to even begin fixing the ones I'm having now.

Argument errors? My code is sending the correct number of arguments, I know that because I can go thru and look at what variables/info I'm sending to each function, and without the preloader it works fine, no errors at all, no slow down, the game just works.

So why is it when I attach the preloader suddenly Ship() is expecting 1 argument (which my engine sends as soon as it needs to) but getting 0? I don't understand why that would happen at all. So far none of the forums I've posted to have had any answer for me at all.

If it's something that should be obvious, just point it out to me, I'm new, it's not obvious to me or I wouldn't post. If I'm not explaining clearly enough, ask me to clarify. I'm really starting to get discouraged here, and this hobby is becoming more like a headache that I want to just walk away from. Please, just a little help?
Misterbluedot is offline   Reply With Quote
Old 10-26-2009, 10:11 PM   #3
nyghtrunner
Senior Member
 
nyghtrunner's Avatar
 
Join Date: Nov 2004
Posts: 637
Default

Ok, so I see a few problems that are easy to clear up.

In the "Engine.as" file. Here's the biggest problem that you have. Since the whole thing is set up to run on the "ENTER_FRAME" event, the biggest thing to keep in mind is that, once you set that up, it will ALWAYS be firing. Even, and perhaps most especially when, the preloader is going. There's a logic problem with the way you have it set up right now.

You have it set up so that the EnterFrame event changes the called method based on a switch (gameState). Realize that when you do it that way, it's going to call the "doLoadScreen()" function EVERY FRAME until the preloader completes. Here's the problem with how you have it. You set up no real way to tell if the preloader is already on the screen, and so that function just continues to add a new preloader and a set of event listeners every dispatch. You see the problem with that, right? the "preloader" variable will only ever be set to the last instance that was created for removal. So... When the "loadAssets" and the "flipTheSwitch" functions fire, you remove the last instance of preloader and the listeners that are associated with that instance only. What this results in is you having lord only knows how many instances/listeners running (at a simulated 56k dl, I got 690 instances of "ThePreloader"... That's a SURE way to eat up the memory, and thus, the game runs at a snails pace...)

Here's a quick fix for that.

ActionScript Code:
private var firstRun:Boolean = true; private var num:Number = 0; ... public function doLoadScreen():void {     trace(num)     if (firstRun) {         preloader = new ThePreloader(474, this.loaderInfo);         //trace(this);         this.addChild(preloader);         preloader.addEventListener("loadComplete", loadAssets);         preloader.addEventListener("preloaderFinished", flipTheSwitch);         firstRun = false;     }     num++; }

As you can see, I set up a boolean var called "firstRun", and set it to true. Then, I placed a simple conditional in the "doLoadScreen" function telling it, "If you haven't run, add the preloader, if you have, do nothing", essentially.

This leads to the "argument errors", which ARE happening, and here's why.

To make the preloader more efficient/accessible, you placed an instance of each class on the stage on frame 2. That's perfectly fine, but instances placed like that are not placed dynamically with a constructor that you can pass things to. They ARE instances of the class, and the class constructor wants the arguments. Since they're just chillin' on the stage, you can't give the constructor those arguments... So... What to do?

Well, the solution is actually VERY simple. In your "loadAssets" function, you have it set up to:
ActionScript Code:
this.play();
which guarantees that those instances, even off the screen, will be on the stage at runtime, causing the "problem" (It's not really a problem, just looks bad on the compiler). The way around this is to change that line to:
ActionScript Code:
this.gotoAndStop(3); //since frame 3 is where the game is actually "running"

This will skip the problem frame (2) without passing go or collecting $200. You jump from preloader to engine in one fell swoop. You place them on frame 2 so that you don't have to "export in first frame" any of those classes, thus, increasing the value of the preloader. It's certainly good practice to do that, just realize that you don't want/need those guys on the stage at any given time. Only in the movie so that the preloader catches them, and you still have access to them via AS3. Make sense?

Anyway, that should clear up your problem.

Cheers!
nyghtrunner is offline   Reply With Quote
Old 10-26-2009, 11:10 PM   #4
Misterbluedot
Newbie, not n00b!
 
Join Date: Oct 2009
Location: Washington, where the weather changes every 15 minutes.
Posts: 37
Default

Quote:
Originally Posted by nyghtrunner View Post
Ok, so I see a few problems that are easy to clear up.

In the "Engine.as" file. Here's the biggest problem that you have. Since the whole thing is set up to run on the "ENTER_FRAME" event, the biggest thing to keep in mind is that, once you set that up, it will ALWAYS be firing. Even, and perhaps most especially when, the preloader is going. There's a logic problem with the way you have it set up right now.

You have it set up so that the EnterFrame event changes the called method based on a switch (gameState). Realize that when you do it that way, it's going to call the "doLoadScreen()" function EVERY FRAME until the preloader completes. Here's the problem with how you have it. You set up no real way to tell if the preloader is already on the screen, and so that function just continues to add a new preloader and a set of event listeners every dispatch. You see the problem with that, right? the "preloader" variable will only ever be set to the last instance that was created for removal. So... When the "loadAssets" and the "flipTheSwitch" functions fire, you remove the last instance of preloader and the listeners that are associated with that instance only. What this results in is you having lord only knows how many instances/listeners running (at a simulated 56k dl, I got 690 instances of "ThePreloader"... That's a SURE way to eat up the memory, and thus, the game runs at a snails pace...)

Here's a quick fix for that.

ActionScript Code:
private var firstRun:Boolean = true; private var num:Number = 0; ... public function doLoadScreen():void {     trace(num)     if (firstRun) {         preloader = new ThePreloader(474, this.loaderInfo);         //trace(this);         this.addChild(preloader);         preloader.addEventListener("loadComplete", loadAssets);         preloader.addEventListener("preloaderFinished", flipTheSwitch);         firstRun = false;     }     num++; }

As you can see, I set up a boolean var called "firstRun", and set it to true. Then, I placed a simple conditional in the "doLoadScreen" function telling it, "If you haven't run, add the preloader, if you have, do nothing", essentially.

This leads to the "argument errors", which ARE happening, and here's why.

To make the preloader more efficient/accessible, you placed an instance of each class on the stage on frame 2. That's perfectly fine, but instances placed like that are not placed dynamically with a constructor that you can pass things to. They ARE instances of the class, and the class constructor wants the arguments. Since they're just chillin' on the stage, you can't give the constructor those arguments... So... What to do?

Well, the solution is actually VERY simple. In your "loadAssets" function, you have it set up to:
ActionScript Code:
this.play();
which guarantees that those instances, even off the screen, will be on the stage at runtime, causing the "problem" (It's not really a problem, just looks bad on the compiler). The way around this is to change that line to:
ActionScript Code:
this.gotoAndStop(3); //since frame 3 is where the game is actually "running"

This will skip the problem frame (2) without passing go or collecting $200. You jump from preloader to engine in one fell swoop. You place them on frame 2 so that you don't have to "export in first frame" any of those classes, thus, increasing the value of the preloader. It's certainly good practice to do that, just realize that you don't want/need those guys on the stage at any given time. Only in the movie so that the preloader catches them, and you still have access to them via AS3. Make sense?

Anyway, that should clear up your problem.

Cheers!
YOU are the man! Thank you! It works perfectly now!

Yeah, everything you said made sense. I should of connected ENTER_FRAME with, you know, entering the frame, but I'm still learning about event listeners. It wasn't till a few days ago that I realized they will interfere with removeChild() commands if you don't get rid of them.

I tried the gotoAndStop(); line before, but with out the enter frame fix, so I didn't know if it was helping or hurting and switched it back lol. But that solved the asset holder problem

Thanks a million! If you're ever out near Seattle, I owe you a beer
Misterbluedot is offline   Reply With Quote
Old 10-27-2009, 02:21 PM   #5
nyghtrunner
Senior Member
 
nyghtrunner's Avatar
 
Join Date: Nov 2004
Posts: 637
Default

Quote:
Originally Posted by Misterbluedot View Post
YOU are the man! Thank you! It works perfectly now!

Yeah, everything you said made sense. I should of connected ENTER_FRAME with, you know, entering the frame, but I'm still learning about event listeners. It wasn't till a few days ago that I realized they will interfere with removeChild() commands if you don't get rid of them.

I tried the gotoAndStop(); line before, but with out the enter frame fix, so I didn't know if it was helping or hurting and switched it back lol. But that solved the asset holder problem

Thanks a million! If you're ever out near Seattle, I owe you a beer
Saweet! That alone made it all worth while!
nyghtrunner is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:09 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.