Be a Supporter!

[AS3] Preloader

  • 509 Views
  • 25 Replies
New Topic Respond to this Topic
Knight52
Knight52
  • Member since: Jan. 8, 2012
  • Offline.
Forum Stats
Member
Level 07
Game Developer
[AS3] Preloader 2013-12-10 11:22:05 Reply

I use FlashDevelop 4. I started project with preloader. I set the preloader.as (below code) as the document class. It should display bytes loaded, and it displays just when completely loaded. There was nothing but white screen before that. I don't know where did I miss. Do I need that [factoryClass="blabla"] somewhere? on the other classes? how do I tell the compiler to load the other classes AFTER this one?

Below is preloader class, set as 'document class', as it currently is.

package 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	
	/**
	 * ...
	 * @author Knight
	 */
	public class Preloader extends MovieClip 
	{
		private var progressText:TextField;
		public function Preloader() 
		{
			progressText = new TextField();
			if (stage) {
				stage.scaleMode = StageScaleMode.NO_SCALE;
				stage.align = StageAlign.TOP_LEFT;
			}
			addEventListener(Event.ENTER_FRAME, checkFrame);
			progressText.width = 800;
			progressText.x = 0;
			progressText.y = 300;
			progressText.defaultTextFormat = new TextFormat("Tahoma", 40, 0xffffff, true, false, false, null);
			addChild(progressText);
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
			
			// TODO show loader
		}
		
		private function ioError(e:IOErrorEvent):void 
		{
			trace(e.text);
		}
		
		private function progress(e:ProgressEvent):void 
		{
			// TODO update loader
			progressText.text = e.bytesLoaded + "/" + e.bytesTotal;
		}
		
		private function checkFrame(e:Event):void 
		{
			if (currentFrame == totalFrames) 
			{
				stop();
				loadingFinished();
			}
		}
		
		private function loadingFinished():void 
		{
			removeEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
			
			// TODO hide loader
			
			startup();
		}
		
		private function startup():void 
		{
			removeChild(progressText);
			Main;
			var main:Class = getDefinitionByName("Main") as Class;
			addChild(new main());
		}
		
	}
	
}
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to [AS3] Preloader 2013-12-10 12:34:32 Reply

At 12/10/13 11:22 AM, Knight52 wrote: private function startup():void
{
removeChild(progressText);

Main; // where does this line come from?

var main:Class = getDefinitionByName("Main") as Class;
addChild(new main());
}

see comment

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to [AS3] Preloader 2013-12-10 13:57:42 Reply

At 12/10/13 11:22 AM, Knight52 wrote: I use FlashDevelop 4. I started project with preloader. I set the preloader.as (below code) as the document class. It should display bytes loaded, and it displays just when completely loaded. There was nothing but white screen before that. I don't know where did I miss. Do I need that [factoryClass="blabla"] somewhere? on the other classes? how do I tell the compiler to load the other classes AFTER this one?

Yes, the factoryclass is the most important part. Make a new preloader project to see where it goes. And when you typed out Main, you nullified the entire point of the preloader.

Knight52
Knight52
  • Member since: Jan. 8, 2012
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to [AS3] Preloader 2013-12-10 21:04:40 Reply

At 12/10/13 01:57 PM, MSGhero wrote:
Yes, the factoryclass is the most important part. Make a new preloader project to see where it goes. And when you typed out Main, you nullified the entire point of the preloader.

How do I refer to Main class then? getDefinitionByName needs the class to be referred somewhere else first!

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to [AS3] Preloader 2013-12-10 21:09:12 Reply

At 12/10/13 09:04 PM, Knight52 wrote: How do I refer to Main class then? getDefinitionByName needs the class to be referred somewhere else first!

If your preloader has a reference to any class, it loads that class at the start before any preloading begins. Which is the opposite of what a preloader should do for your main class. The string you pass into gDBN() and the class you get from it make up a weak reference to your main class. Since you pulled this new class out of nowhere, it has to load it in, aka preload.

Knight52
Knight52
  • Member since: Jan. 8, 2012
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to [AS3] Preloader 2013-12-10 21:25:04 Reply

At 12/10/13 09:09 PM, MSGhero wrote:
If your preloader has a reference to any class, it loads that class at the start before any preloading begins. Which is the opposite of what a preloader should do for your main class. The string you pass into gDBN() and the class you get from it make up a weak reference to your main class. Since you pulled this new class out of nowhere, it has to load it in, aka preload.

so how should I refer to the main class? I can't refer the name in preloader, but it won't include the class in swf at all if I don't.

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to [AS3] Preloader 2013-12-10 21:32:58 Reply

At 12/10/13 09:25 PM, Knight52 wrote: so how should I refer to the main class? I can't refer the name in preloader, but it won't include the class in swf at all if I don't.

You already have the code for it:

var main:Class = getDefinitionByName("Main") as Class;
addChild(new main());

When in doubt, create a new preloader project and see what's there.

Knight52
Knight52
  • Member since: Jan. 8, 2012
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to [AS3] Preloader 2013-12-10 22:29:19 Reply

At 12/10/13 09:32 PM, MSGhero wrote: var main:Class = getDefinitionByName("Main") as Class;
addChild(new main());

I'm pretty sure that gave me runtime error, but I'll try again.

kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to [AS3] Preloader 2013-12-11 04:56:10 Reply

At 12/10/13 10:29 PM, Knight52 wrote:
At 12/10/13 09:32 PM, MSGhero wrote: var main:Class = getDefinitionByName("Main") as Class;
addChild(new main());
I'm pretty sure that gave me runtime error, but I'll try again.

Document class.
Set it.


BBS Signature
Knight52
Knight52
  • Member since: Jan. 8, 2012
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to [AS3] Preloader 2013-12-11 07:24:52 Reply

Alright, now my Main looks like this

[Frame (factoryClass = "Preloader")]
	public class Main extends Sprite 
	{
		private static var instance:Main;
		private var credit:MyTextField;
		private var fade:Shape;
		private var main:MainPage;
......

If I set Main as document class, the code doesn't reach Preloader at all. If I set Preloader as document class, it gave me runtime error "Variable Main is not defined." Any idea?

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to [AS3] Preloader 2013-12-11 09:41:29 Reply

At 12/11/13 07:24 AM, Knight52 wrote: If I set Main as document class, the code doesn't reach Preloader at all. If I set Preloader as document class, it gave me runtime error "Variable Main is not defined." Any idea?

What line does this error occur on?

Read my post again (it's the first answer you've got in this thread) and answer the question.

Knight52
Knight52
  • Member since: Jan. 8, 2012
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to [AS3] Preloader 2013-12-11 09:47:18 Reply

At 12/11/13 09:41 AM, milchreis wrote:
At 12/11/13 07:24 AM, Knight52 wrote: If I set Main as document class, the code doesn't reach Preloader at all. If I set Preloader as document class, it gave me runtime error "Variable Main is not defined." Any idea?
What line does this error occur on?

Read my post again (it's the first answer you've got in this thread) and answer the question.

I commented out the 'Main', and I got this problem.

From setting the breaker, it's after executing getDefinitionByName. Weird enough that FlashDevelop didn't highlight the line, or bring up Preloader.as at all.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to [AS3] Preloader 2013-12-11 10:00:35 Reply

At 12/11/13 09:47 AM, Knight52 wrote: From setting the breaker, it's after executing getDefinitionByName.

Be more precise!
Your problem is somewhere else. (in Main class)
To solve it you have to find the line it is on.
This is not solved by looking at your code, use the debugger and step through it one step at a time to find out which line causes the error.

Weird enough that FlashDevelop didn't highlight the line, or bring up Preloader.as at all.

If the code has errors, it's possibly not even executed.
Asking why it doesn't do as expected is like asking why a still birth doesn't breathe.

You probably mistyped the "main" variable somewhere?

Post both full classes!

Knight52
Knight52
  • Member since: Jan. 8, 2012
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to [AS3] Preloader 2013-12-11 10:11:15 Reply

At 12/11/13 10:00 AM, milchreis wrote: Be more precise!
Your problem is somewhere else. (in Main class)
To solve it you have to find the line it is on.
This is not solved by looking at your code, use the debugger and step through it one step at a time to find out which line causes the error.

Alright, my bad. The execution stop after calling the ONLY line that has getDefinitionByName in the whole project. Over there in the preloader.

At 12/11/13 10:00 AM, milchreis wrote:
Weird enough that FlashDevelop didn't highlight the line, or bring up Preloader.as at all.
If the code has errors, it's possibly not even executed.
Asking why it doesn't do as expected is like asking why a still birth doesn't breathe.

You probably mistyped the "main" variable somewhere?

Post both full classes!

I'm sure I didn't. To prove that, I uncommented Main that I commented out earlier because I was suggested not to refer anywhere in preloader class. And it ran fine, minus preloader itself.

Preloader.as Right now. Set as 'document class'

package 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	
	/**
	 * ...
	 * @author Knight
	 */
	public class Preloader extends MovieClip 
	{
		private var progressText:TextField;
		public function Preloader() 
		{
			progressText = new TextField();
			if (stage) {
				stage.scaleMode = StageScaleMode.NO_SCALE;
				stage.align = StageAlign.TOP_LEFT;
			}
			addEventListener(Event.ENTER_FRAME, checkFrame);
			progressText.width = 800;
			progressText.x = 0;
			progressText.y = 300;
			progressText.defaultTextFormat = new TextFormat("Tahoma", 40, 0xffffff, true, false, false, null);
			addChild(progressText);
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
			
			// TODO show loader
		}
		
		private function ioError(e:IOErrorEvent):void 
		{
			trace(e.text);
		}
		
		private function progress(e:ProgressEvent):void 
		{
			// TODO update loader
			progressText.text = e.bytesLoaded + "/" + e.bytesTotal;
		}
		
		private function checkFrame(e:Event):void 
		{
			if (currentFrame == totalFrames) 
			{
				stop();
				loadingFinished();
			}
		}
		
		private function loadingFinished():void 
		{
			removeEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
			
			// TODO hide loader
			
			startup();
		}
		
		private function startup():void 
		{
			removeChild(progressText);
			//Main;
			var main:Class = getDefinitionByName("Main") as Class;
			addChild(new main());
		}
		
	}
	
}

Main.as.

package 
{
	import Base.MyTextField;
	import fl.transitions.easing.None;
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import HardCode.Pages.Loading.LoadingPage;
	import HardCode.Pages.Main.MainPage;
	import Resources.Resource;
	import System.*;
	import System.V.InteractionText;
	import System.V.RenderableObject;
	/**
	 * ...
	 * @author Knight
	 */
	[Frame (factoryClass = "Preloader")]
	public class Main extends Sprite 
	{
		private static var instance:Main;
		private var credit:MyTextField;
		private var fade:Shape;
		private var main:MainPage;
		private var game:GamePage;
		private var load:LoadingPage;
		private var current_page:Pages;
		private var to_change:Pages;
		private var cache:RenderableObject;
		public function Main():void 
		{
			instance = this;
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		public static function ChangeToGamePage():void
		{
			instance.SetPage(instance.game);
		}

		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
			
			// entry point
			Overseer.Initialize();
			ControlCenter.Initialize(stage);
			DataCenter.Initialize();
			
			credit = new MyTextField();
			fade = new Shape();
			main = new MainPage();
			game = new GamePage();
			load = new LoadingPage();
			cache = new RenderableObject();
			
			fade.graphics.beginFill(0xffffff);
			fade.graphics.drawRect(0, 0, 800, 600);
			fade.graphics.endFill();
			addChild(fade);
			//SetPage(main);
			//addChild(new GamePage());
			addChild(credit);
			credit.text = "Created by Knight52";
			credit.x = stage.stageWidth / 2 - credit.width / 2;
			credit.y = 0;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			cache = new RenderableObject();
			addChild(cache);
			load.SetFunction(null, Cache, null);
			SetPage(load);
		}
		private function onRemoveFromStage(e:Event):void
		{
			DataCenter.Flush();
			ControlCenter.Flush();
			Overseer.Flush();
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
		}
		private function ChangePage(page:Pages):void
		{
			var tween:Tween = new Tween(fade, "alpha", None.easeNone, 0, 1, 0.5, true);
			tween.start();
			tween.addEventListener(TweenEvent.MOTION_FINISH, tween_onFinish);
			if (current_page != null)
			{
				current_page.Stop();
			}
			setChildIndex(fade, numChildren - 1);
			to_change = page;
		}
		private function tween_onFinish(e:Event):void
		{
			if (current_page != null)
			{
				removeChild(current_page);
			}
			current_page = to_change;
			addChild(current_page);
			current_page.Start();
			setChildIndex(fade, numChildren - 1);
			var tween:Tween = new Tween(fade, "alpha", None.easeNone, 1, 0, 0.5, true);
			tween.start();
		}
		private function SetPage(page:Pages):void
		{
			if (current_page != null)
			{
				current_page.Stop();
				removeChild(current_page);
			}
			current_page = page;
			addChild(page);
			page.Start();
		}
		private function onEnterFrame(e:Event):void
		{
			if (Resource.IsCached)
			{
				removeChild(cache);
				ChangePage(game);
				removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
		}
		private var cache_count:uint = 0;
		private function Cache(textField:MyTextField):void
		{
			if (textField.text.indexOf("Caching") < 0)
			{
				textField.text = "Caching";
			}
			else if (textField.text.indexOf("...") < 0)
			{
				if (++cache_count == 30)
				{
					cache_count = 0;
					textField.appendText(".");
				}
			}
			else
			{
				Resource.BeginCache(cache);
			}
		}
	}
}
MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to [AS3] Preloader 2013-12-11 10:20:01 Reply

At 12/11/13 10:11 AM, Knight52 wrote: Preloader.as Right now. Set as 'document class'

Preloader isn't your document class. Main is. There's nothing in your preloader to prevent the game from starting instantly (like a play button), so how did you determine that your code "isn't going to the preloader"?

kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to [AS3] Preloader 2013-12-11 10:20:39 Reply

private function startup():void 
		{
			var mainClass:Class = getDefinitionByName("Main") as Class;
			stage.addChild(new mainClass() as DisplayObject);
			stage.removeChild(this);
		}

BBS Signature
Knight52
Knight52
  • Member since: Jan. 8, 2012
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to [AS3] Preloader 2013-12-11 10:33:24 Reply

At 12/11/13 10:20 AM, MSGhero wrote: Preloader isn't your document class. Main is. There's nothing in your preloader to prevent the game from starting instantly (like a play button), so how did you determine that your code "isn't going to the preloader"?

trace() and line breaker (that F9 button).

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to [AS3] Preloader 2013-12-11 10:39:48 Reply

setting Main to be the document class should do the trick

Knight52
Knight52
  • Member since: Jan. 8, 2012
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to [AS3] Preloader 2013-12-11 10:56:19 Reply

At 12/11/13 10:39 AM, milchreis wrote: setting Main to be the document class should do the trick

It doesn't. Preloader wouldn't be loaded at all. I placed trace() and line breaker all over the class, none of them showed up. Yes, I've already pressed 'play' button on the log window.

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to [AS3] Preloader 2013-12-11 11:09:30 Reply

At 12/11/13 10:56 AM, Knight52 wrote: It doesn't. Preloader wouldn't be loaded at all.

The Frame(factoryClass="Preloader") thing makes the Preloader frame 1 of the root timeline and Main as frame 2 (more or less). It's compiler magic, you shouldn't try to rationalize it.

Yes, I've already pressed 'play' button on the log window.

I don't know what that means. What log window? That's probably the problem.

If you put a trace in the startup function in Preloader and it doesn't show up, you're doing something else wrong that you haven't told us about yet.

Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to [AS3] Preloader 2013-12-11 11:25:07 Reply

At 12/11/13 11:09 AM, MSGhero wrote:
Yes, I've already pressed 'play' button on the log window.
I don't know what that means. What log window? That's probably the problem.

In FlashDevelop sometimes my traces just don't show and I have to press play in the log window :(

Knight52
Knight52
  • Member since: Jan. 8, 2012
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to [AS3] Preloader 2013-12-11 11:27:04 Reply

At 12/11/13 11:09 AM, MSGhero wrote:
Yes, I've already pressed 'play' button on the log window.
I don't know what that means. What log window? That's probably the problem.

Below is log window and that play button I'm talking about. My apology for being so horrible at using words. And I'm sure that's not the problem.

At 12/11/13 11:09 AM, MSGhero wrote:
If you put a trace in the startup function in Preloader and it doesn't show up, you're doing something else wrong that you haven't told us about yet.

That's what I'm trying to find out right now. I don't know why it doesn't work. Could it be embedding images? Static classes and functions? Codes from fl.* library that I copied into the project folder?

[AS3] Preloader

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to [AS3] Preloader 2013-12-11 11:37:05 Reply

At 12/11/13 11:27 AM, Knight52 wrote: That's what I'm trying to find out right now. I don't know why it doesn't work. Could it be embedding images? Static classes and functions? Codes from fl.* library that I copied into the project folder?

In your loadingComplete thing, instead of immediately calling startup(), addChild a button with startup as its mouse click handler. If the button shows up, your log thing doesn't work with compiler magic. If your main class shows up, you need to reinstall FD and update your Flex to 4.11 or something.

Knight52
Knight52
  • Member since: Jan. 8, 2012
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to [AS3] Preloader 2013-12-14 12:41:48 Reply

At 12/11/13 11:37 AM, MSGhero wrote:
At 12/11/13 11:27 AM, Knight52 wrote: That's what I'm trying to find out right now. I don't know why it doesn't work. Could it be embedding images? Static classes and functions? Codes from fl.* library that I copied into the project folder?
In your loadingComplete thing, instead of immediately calling startup(), addChild a button with startup as its mouse click handler. If the button shows up, your log thing doesn't work with compiler magic. If your main class shows up, you need to reinstall FD and update your Flex to 4.11 or something.

No luck :(

I put while(1); in the preloader's constructor, and commented out startup() function, It still showed up Main. So, I decided to reinstall the FlashDevelop to 4.5.2, and it downloaded flex 4.6.0 for me too. It still didn't work. I moved another computer, still didn't work. I created a new fresh project, it didn't go into preloader either. What do they mean? Am I missing something incredibly stupid? Example, should I debug this in any other way than testing in flash debugger? Is it something about "AS3 classpath" in AS3Context option?

kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to [AS3] Preloader 2013-12-14 13:25:25 Reply

At 12/14/13 12:41 PM, Knight52 wrote:
I put while(1); in the preloader's constructor, and commented out startup() function, It still showed up Main. So, I decided to reinstall the FlashDevelop to 4.5.2, and it downloaded flex 4.6.0 for me too. It still didn't work. I moved another computer, still didn't work. I created a new fresh project, it didn't go into preloader either. What do they mean? Am I missing something incredibly stupid? Example, should I debug this in any other way than testing in flash debugger? Is it something about "AS3 classpath" in AS3Context option?

Send me a link to a zip file containing your flash develop project file, Main.as file and Preloader.as file.
If acceptable, send me your whole, entire project.


BBS Signature
Knight52
Knight52
  • Member since: Jan. 8, 2012
  • Offline.
Forum Stats
Member
Level 07
Game Developer
Response to [AS3] Preloader 2013-12-15 09:22:49 Reply

With the aid of kkots, I've managed to solve the issue. The thing is I used the wrong SDK. Previously, it's set to use only Air 3.9.0, so I solved by just manually change to Flex 4.6 & Air 3.1. Should've been set automatically when creating the project if it's so required.

For those who google the issue and ended up here. In FlashDevelop 4.5.2, go to Project -> SDK, and change the using SDK to the one with Flex in it.