Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 07-29-2007, 04:25 AM   #1
abeall
Senior Member
 
Join Date: Feb 2006
Posts: 819
Default Event.RENDER fires before all on stage objects are instantiated???

I have an application that has several "view states", and I made the mistake of approaching the project the way it was done in AS2: I put each view on its own frame. It was too late when I discovered that gotoAndStop() no longer is immediate -- a staggering problem on many levels.

So, since I need to add event listeners and such to many(several dozen) interface elements on those frames, I've attempted to use the RENDER event to ensure the objects have all been instantiated on the frame before I access them. In other words, something like this(not actual code, there is a lot of code in my project):
ActionScript Code:
function MyApp extends MovieClip(){   stage.addEventListener(Event.RENDER,onRender); } function changeView(viewState){   gotoAndStop(viewState);   stage.invalidate(); } function onRender(evt){   myMovieClipOnStage.x = 5; }

The problem is that onRender is called before myMovieClipOnStage has been instantiate by Flash. I know because Flash throws a "Cannot access a property or method of a null object reference." error, and trace(myMovieClipOnStage) outputs "null." The problem is also intermittent -- it seems that if I change states rapidly or the player window is large(so I guess render times take longer) is when it happens most often. What should I do?

And although I'm not keen on rebuilding this thing, how should have I approached multiple view states in a Flash AS3 project? There are some shared elements and some not shared.

I've also noticed that if I addEventListeners to on stage objects, even when I navigate off the frame so they no longer exist, the event keeps firing, even though I set them as weakReferences.

Last edited by abeall; 07-29-2007 at 04:33 AM..
abeall is offline   Reply With Quote
Old 07-29-2007, 03:38 PM   #2
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,758
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

This is a big problem that was not given enough attention during development. a) AS3 was catered to Flex (no frames). b) AS3 is still fairly new and see point a).

I have found that the RENDER event (what should be our savior for these related gotoAndPlay issues) is not dependable. Two cases include RENDER firing prematurely as the result of other events, such as CLICK or COMPLETE and the fact that (bug) if you use ref.removeEventListener(Event.RENDER, ...) you will in fact remove all RENDER event listeners. Since Flash CS3 components do this... you're in a bit of a pickle.

Solutions: For basic display object usage, you can work on using the ADDED event instead of render to determine class instantiation - checking specifically in ADDED for the instance(s) you're looking for. If timing is not a super big deal, you can also use the ENTER_FRAME event to wait a frame to where you know you can be sure your instances are created. You can also go Flex-esque and not use frames for states (as inconvenient as that can be sometimes). Then you are in specific control of instantiation and would know when each instance is available and not. So instead of having something happening on different frames in your main timeline, each state would be its own movie clip symbol in the library and you would just add/remove them to the main timeline when needed or navigated to.
: )
__________________
(6)
senocular is offline   Reply With Quote
Old 07-29-2007, 09:53 PM   #3
abeall
Senior Member
 
Join Date: Feb 2006
Posts: 819
Default

Interesting. I hope this gets ironed out a little bit more in the next version. At any rate, the multiple frame route was providing no joy after a lot of tinkering, so I decided to just scrap that approach altogether and now I'm just doing a hide-and-seek routine with all interface elements on one frame -- switching views in my app simply sets the visibility to be appropriate for all display elements involved. That's quite dirty, though, as now my stage in the IDE looks like a mess with all elements visible(trying to organize layers, but still), and all the hidden display elements at runtime are still consuming resources, though invisible. It would be a lot involved to change the structure so elements are added/removed at runtime, and putting everything in a container clip poses some challenges with the current architecture. It's a small enough app I'm willing to say "it works" but I'm definitely going to have to come up with a different workflow in the future. I'll probably make each view state a MovieClip with a base class of "ViewState" and handle it from there... designing is not going to be as fun, but I might be able to put instances on the stage at design time just for reference, perhaps on a guide layer.

I wish I could get into Flex. :-/
abeall is offline   Reply With Quote
Old 07-29-2007, 10:04 PM   #4
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

Quote:
Originally Posted by abeall View Post
It was too late when I discovered that gotoAndStop() no longer is immediate -- a staggering problem on many levels.
Interesting. What do you mean by that?
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 07-29-2007, 10:29 PM   #5
abeall
Senior Member
 
Join Date: Feb 2006
Posts: 819
Default

Quote:
Originally Posted by Flash Gordon View Post
Interesting. What do you mean by that?
Imagine a timeline in Flash, where on frame 1 you have a preloader, on frame 2 you have "alpha_mc," and on frame 3 you have "bravo_mc." In AS2 you can do this:
ActionScript Code:
// after preloader is done on frame 1 gotoAndStop(2); alpha_mc._x = 55; gotoAndStop(3); bravo_mc._x = 100; // etc

In AS3, you get this:
ActionScript Code:
// after preloader is done on frame 1 gotoAndStop(2); trace(alpha_mc); // outputs "null"

In other words, after you gotoAndStop(2), alpha_mc is not yet instantiated by Flash, so alpha_mc is null, so you can't modify its x value. You have to set up some sort of asynchronous(uuggh how I loathe asynchronous events that should be synchronous) to try and catch when the on stage elements have been instantiated. In my case I was using RENDER, which to my understanding was supposed to happen at the end of a frame when Flash is about to draw everything. Yet somehow, RENDER was firing before everything on the frame instantiated. Apparently its buggy, as senocular pointed out in several ways.
abeall is offline   Reply With Quote
Old 07-29-2007, 10:37 PM   #6
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

Alrighy, thank you for taking the time to explain what you meant.

Cheers
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 07-29-2007, 10:58 PM   #7
abeall
Senior Member
 
Join Date: Feb 2006
Posts: 819
Default

No problem. And by "a staggering problem" I just meant that the way Flash apps were almost always done(placing different major view states on separate frames and navigating between them at runtime) is no longer a reasonable way to do things.
abeall is offline   Reply With Quote
Old 07-30-2007, 12:51 AM   #8
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,758
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

Quote:
Originally Posted by abeall View Post
No problem. And by "a staggering problem" I just meant that the way Flash apps were almost always done(placing different major view states on separate frames and navigating between them at runtime) is no longer a reasonable way to do things.
I've heard of a few projects that started off as AS3 but went back to AS2 because of this, and this is something I've been continuously struggling with for my Mii Editor. I have a couple of hacked solutions, but nothing is really ironed out yet.
__________________
(6)
senocular is offline   Reply With Quote
Old 07-30-2007, 05:52 AM   #9
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

So this this something adobe plans on addressing or is it just a way of life now?
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 07-30-2007, 05:56 AM   #10
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 2,060
Send a message via AIM to Mazoonist
Default

Help me out here... is it my imagination, or....

It seems like in AS2 you could say:
gotoAndPlay('label");

and even if there was a stop() on that frame, the player would go to that frame and start playing.

Now, however, with AS3, when you say gotoAndPlay("label"), if there's a stop() command on that frame, it'll go there and stop.
__________________
My new website: theflashconnection.com
Mazoonist 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
hide objects not on stage AcidSky ActionScript 1.0 (and below) 2 09-19-2003 02:49 PM
optimizing 3D movies- hundreds of objects tcniedz ActionScript 1.0 (and below) 7 07-24-2003 01:42 PM
enterframe and load and objects, oh my! dice ActionScript 1.0 (and below) 13 07-18-2003 12:07 PM
movieclip objects hitting each other retrospect ActionScript 1.0 (and below) 1 12-19-2002 02:15 AM
Stage placement vs dynamic creation notsofast Simple Stuff (Newbies) 11 05-03-2002 11:41 AM


All times are GMT. The time now is 02:54 AM.


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