AS3 preloader sorrows, unable to load symbols from library - actionscript-3

I created an AS3 preloader, and placed the code for that on frame one.
I then made a symbol, and placed it in the library. It was set to NOT export on frame 1, and the fla's settings had all classes exported on frame two. There were no references to the object until frame two.
Then, flash crashed whenever I compiled without the "Export in frame one" box checked.
To fix this, a friend suggested I start my game logic on frame 3, so it will have properly loaded frame 2. That seemed to work fine, the class was instantiating properly.
Then, it turned out that it was not loading the movieclip, only instantiating the class. Again, this could be fixed by exporting in frame 1, but I really cannot afford to do that.
The same friend suggested I place an instance of the symbol on the stage on frame 3, and perform game logic on frame 4. They said this would initialize the movieclip properly.
However, this was not the case. How can I load the entire symbol, graphics and all, without exporting to frame 1? This single symbol will contain probably 10-20 MB of graphics, so it needs to be preloaded.
Thanks for the help!
EDIT: To make a long story short, all I need is some way to load a movieclip so it can be used and visible and everything.
EDIT: Is there any way to force-load a movieclip via AS3?

Hard to figure out from descriptions.
If you make a new .fla file, paste your large(10-20MB) clip on frame 2,
set your export frame as 2, then try to preload from frame 1 and access the large clip's content in frame 2, do you get the same error ?
say you have this in frame 1:
stop();
this.loaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onComplete(event:Event):void{
gotoAndStop(2);
}
and in frame 2:
trace(myLargeClip);//where myLargeClip would be your 10-20MB clip
It should be ok, otherwise, in case tracing your large clip returns null, you might want to try to invalidate the stage:
on frame 2:
stage.addEventListener(Event.RENDER,onRender);
stage.invalidate();
function onRender(event:Event):void{
trace(myLargeClip);
}
Basically what I'm suggesting is:
Isolate the problem. See if your large clip is causing problems in a similar, but simplified scenario and why, then once you got a fix use it in your main fla.
Try the stage invalidation, although, since I don't fully understand your setup, it's just a wild guess.
HTH,
George

Related

AS3 movie instance turning null

I'm banging my head on what seems like a simple as3 problem. I have a flash chart that contains a series of buttons that go to different parts of the timeline on Roll_over.
so for example - the "Market maneuvers" button looks like this
marketManeuversButton.addEventListener(MouseEvent.ROLL_OVER, marketManeuversButtonReaction)
and the function it calls looks like this
function marketManeuversButtonReaction (event:MouseEvent):void{ gotoAndStop('18'); }
The problem is, when I mouseover that button (and many others), it goes to frame '18' and then throws this error:
Error #1009 Cannot access a property or method of a null object
reference
here is my flash file
Any help would be appreciated. Thanks.
When you change frame, flash recreated all objects in frame, and you loose all your data.
Yes, it's simple AS3 problem, just don't use a scene frames at all. Program all in classes, don't use any frames to code any logic except stop(), gotoAndStop(), gotoAndPlay().
In your problem, put all scene in movieclip, exclude control buttons from it to another movieclip and control scene movie clip with control movieclip >____<. It pegleg. Next time just do it right, don't use scene frames.

Flash / pure AS3 preloader is seen after game is 100% loaded

first of all I want to tell that I know there are hundreds of similar questions, but most of them are for projects/games which are built for "on stage development".
I'm using pure as3 approach to develop games. I don't use*main timeline and my project has only 1 frame*.
On my main class's constructor, I only add preloader to the stage.
But still my preloader is seen only when the game is completely loaded.
My question is simple: "Why?" and "How can I fix this?"
-When I try to "uncheck" export as3 on frame 1, flash gives error.(because some of my classes are imported on the main class.)
-Even if I create second frame and put every contents of the game except preloader, the problem is still unsolved.
Thank you very much for spending your time to read this and help me.
-Ozan
To make the preloader, you either need two SWFs or at least two frames. Even FlashDevelop has an ability to use two frames, for this the directive [frame factoryClass="ClassName"] is used in main AS file before public class Main. This is the core limitation of Flash player, that is, it first loads the first frame, and only then it can display that frame. Since you've said you have only one frame, then your game has to be loaded 100% (1/1 frames) before you will see a thing.
There's more - if you are referencing a certain class on frame 1 via its class name, the class with all its dependencies is embedded in the same frame (the first), so you're screwed again. To bypass this, execute stepos 10 through 14 from this instruction to set the frame for classes to be 2 (replace 10 for 2 while executing the instruction), clear "export in the first frame" for all classes except your preloader, and add an instance of game's main class to keyframe 2. Then in preloader code perform proper cleanup and do gotoAndStop(2).

how to set actionscript code to execute only once

Okay so I have a movieclip called a_mc, if you click the movieclip, it goes to frame 5, and then on frame 5 there is a button called close_btn where if you click the button, it goes back to frame 1 and it is supposed to make a_mc invisible. Here is the actionscript code for frame 1.
stop();
a_mc.addEventListener(MouseClick.CLICK, aClicked);
function aClicked(event:MouseEvent):void {
gotoAndStop(5);
}
and on frame 5, the actionscript code is
stop();
close_btn.addEventListener(MouseEvent.CLICK, closeCLicked);
function closeClicked(event:MouseEvent):void {
gotoAndStop(1);
a_mc.visible = false;
a_mc.removeEventListener(MouseEvent.CLICK, aClicked);
}
see, the problem is, in frame 5, I make a_mc invisible and remove the event listener and go back to frame 1 and on frame one, it always executes the actionscript code so it again creates the event listener and makes a_mc visible. Any idea on how to stop this from happening?
I tried putting the code from frame 1 into a package and then a class and then a constructer method but it is saying
"Syntax error: package is unexpected"
Could you put all the code that you want to execute once in frame 1? - don't call stop() and let it run to the next frame.
Then put the rest of your code in other key frames and don't use gotoAndStop(1) so frame 1 is only called once?
You can try not removing the event listener on a_mc in frame 5, and then in frame 1 check if the event listener is already present (a_mc.hasEventListener()) as a signal that frame 1 has already been shown. Not exactly a 'bets practices' solution, but it might work.
Unfortunately, depending on the actual conents of those clip, and what happens in other frames, it may be the problem you're having is a consequence of the way movieclip object works in flash. When a frame is changed, flash instantiates new objects on the stage (added in new frame), and removes the ones not needed anymore (depending on the contents, but generally it's true). The 'a_mc' object that you manipulate in frame 5 may not be the same 'a_mc' object that is on the stage when you go back to frame 1. It may have been deleted and reinstantiated in the meantime.
To avoid things like that, it would be a better solution to have controlling code in a class outside of the timeline of the animating clip, or at least to keep the state in a separate object. I work in Flash Builder so I can't help you with the details of such organization in Flash Pro (which I presume you're using), but you could probably have all code on the frame 1 of the main clip, and then put the other movieclips with buttons and stuff as children of the main clip. That way main clip can control the state, and know what to show when.

Internal AS3 preloader & stage issues

I need to create a single SWF with no external files, so I'm trying to add an internal preloader to my Flash project which has [embed] assets. I know [embed] causes problems with preloaders because it puts the assets on frame 1. I have tried the solutions recommended in these posts, where you set the document class to your preloader class:
Preloader for SWF with embed bytearray
How to create Preloader in AS3
I can get it to work, but ONLY if I comment out any lines of code that involve the stage, otherwise I get an "Error #1009: Cannot access a property or method of a null object reference." Those lines are essential though, so does anyone know how to fix those errors with the stage?
You haven't posted your code or your fla, so all I can do is share what works for me.
First, I wouldn't use Embed. Instead, use a swc. I have found that Embed can be unreliable as far as actually getting the entire asset in there (at least when publishing with Flash Builder + Flash Pro, which is my workflow).
Once you have your assets in a swc, try the following steps:
Set your export frame to Frame 10 (or any frame other than 1--I like frame 10 because then you can read the label that says "Preloader")
Put your actual content on frame 11. You can structure this a lot of ways. Since I program to Interfaces, I give whatever is on frame 11 an instance name and then use a setter to determine that my "first thing" has been placed on stage. I can get away with this because my main Document Class just knows the definition of the Interface, not the full implementation of the Class, so the Class does not need to load for the main Document Class to work. You probably aren't truly using the timeline and probably didn't program to interfaces, so you'll probably just set the base class of the symbol that's on frame 11 to the main logic of whatever you're trying to do.
Put your preloader graphics in Frame 1. I'm not sure why your stage references are so important. I, personally, don't use any logic in the preloader. Instead, I use a spinner that spans frames 1-10 (plus the word "Loading...". The spinner just spins while the classes load. The embed frame acts as a temporary "stop" that just holds the timeline back until those classes have been loaded. Once the classes have been loaded, the timeline will act like you called play() on it. So it really can be that simple. If you need it to be more complicated, give one of your preloader graphics an instance name and set up a getter/setter pair for it, then use the setter to trigger your logic that accesses the stage. You are pretty much guaranteed to have a valid stage at that point.
Word of warning: if you did make use of the timelime, you will get strange results if you try to jump to a frame that isn't loaded yet, so make sure to check to see if a given frame is loaded if it's near the end of your main timeline and your main timeline is heavy with assets before calling goToAndPlay() or goToAndStop().
Some references that might help you further:
Preloaders vs as 3 (I'd recommend you read the entire series this is part of. This is an amazing series I wish I'd found 3 years ago)
Solving the Frame 2 Problem Presentation and code
Combining the Timeline with OOP The example code for that is here (long story)

As3 preload/render a single movieclip before addChild to stage

I have some problems. I want to preload a single movieclip instead of preloading the whole project. Is there a way to that.
Concept Game.
Intro Displayed - Intro removed - Title Dislplayed - Title removed "after pressing on screen"
[[[[[[[PRELOAD CUT SCENE]]]]]]] if completed display CUT SCENE.
Thats the plan, but i do not know how.
Yes, there is a way to do that. But you need several SWFs for that, each containing a single movie clip that you want to be separately preloaded. You make a Loader object for each one, you have an URL for each one, then you call Loader.load() at appropriate time (say, after successfully preloading previous one) and assign a Event.COMPLETE listener that would fire once the movieclip is loaded. Don't forget to assign an error event loader as well, otherwise your game would stuck should it fail to preload a cut scene. More in the documentation for Loader class.