as3 symbol variables not initialized yet - actionscript-3

I'm initializing symbols in my timeline, and trying to access the variables within those symbols, but they return 0 or undefined even though I set the variables in the symbol's timeline. For some reason the variables haven't been set yet, though the main timeline can see that they exist. How do I make the program wait until the variables have been set?

Best practice to work with classes, not coding in timeline and frames of MovieClip.
I assume you have MovieClip from designer and you want inject some logic to the specific frame. There are many options.
Events
You can trigger event in the specific frame, and you work in normal way (with classes and class members).
//Frame code
import flash.events.Event;
this.dispatchEvent(new Event("IntroDidFinish", true, true));
stop();
//Somewhere in class
myContainer.addEventListener("IntroDidFinish", onIntroFinish, false, 0, true);
function onIntroFinish(e: Event):void{
//Do your stuff
}
Events help you decouple logic from the design(predefined complex MovieClip, etc.)
Waiting for initialisation
As MovieClip reaches some frame, you should wait extra time for initialisation. Thats why 99.9% of AS3 developers don't like MovieClip as holder for any critical data or logic. It means if you call myMovieClip.goToAndStop(8); you can't get myMovieClip.someValue declared in 8 frame after goTo operation. If you still want to go with such approach, easiest solution for you will be Event.ENTER_FRAME, after goTo subscribe for ENTER_FRAME event, for only one update, and do your work ;)

Related

On the use of frames and functions in Flash AS3

My question is, in a flash game I use different frames for levels. And I am confused on how functions work for this. My first frame works fine but I need help with using functions on other frames and keeping functions specific to one frame. Any help is appreciated, just a quick simple explanation
You cannot associate a function with a specific frame.
When you execute code on a frame, what actually happens is the MovieClip containing that frame will call a function called addFrameScript(), passing to it a representation of the code you write on the frame.
This means:
Until you visit a frame that defines a function, you cannot call said function.
Once you have visited a frame that defines a function, that function is attached to the parent MovieClip, and you are able to call the function at any point in the timeline that is earlier or later.
If you try to define a new function with the same name on a different frame, you will get a 1021: Duplicate function definition error.
Instead of making a new function for every frame or level, you should make a single function that is able to handle different information that is representative of a level, e.g.
function loadLevel(level:int):void
{
// Do stuff with the value of level.
// For example, this function might look at a data source that maps
// the level numbers to some level data representing tile placement.
}
This could be defined on the first frame, then on each subsequent frame:
loadLevel(1); // Frame 2
loadLevel(2); // Frame 3
// ...etc
All of this of course is not ideal and could be done better avoiding the timeline and instead using to OOP paradigm that AS3 provides.
I have found using multiple levels within the same scene causes no end to headaches for me.
I'm no expert & i'm sure its possible to do so in many cases and I have seen some great games created using 1 frame 1 scene & all code in an external .as.
However I find it much easier myself to just use 1 frame per level and put each level in a different Scene.

AS3 Event architecture

I'm having difficulty with the last piece in the puzzle on AS3 events.
I understand target classes inherit from EventDispatch or implement IEventDispatch and can register (amongst other methods) event listeners.
However what do the target classes register with? If an Event happens, how does AS3 know to pass the Event to the target classes?
Regards,
shwell.
Read this article about event phases and it will make more sense:
http://livedocs.adobe.com/flex/3/html/help.html?content=events_02.html
Hope this helps. Have a great day.
You can look at how starling event works
starling even dispatcher
When a displayObject bubbles an event, it will check if the parent of the displayObject exist and add the parent to bubbleList if exist util the ancestor of displayObject is null.
The following code is in starling eventDispatcher
var element:DisplayObject = this as DisplayObject;
var chain:Vector.<EventDispatcher> = new <EventDispatcher>[element];
while ((element = element.parent) != null)
chain[int(length++)] = element;
In AS3, EventDispatcher is an implementation of the observer design pattern. This class implement the addEventLister, removeEventListener, dispatchEvent' andhasEventListener` methods. Internally, it also maintains a dictionary or similar data structure that contains the events which are currently being listened for, and a list of methods which have to be called when the event is dispatched. Something like this -
{"event1": [method7, method5, method3], "event2": [method3, method2], "event3": [method1]};
When addEventListener is called on an object, it creates a new key for the event in question and adds the method reference to its associated value list.
When dispatchEvent is called on the class, it fetches all the methods associated with the event and calls the methods attached with it. Each method is called with an instance of the Event class or its subclasses.
Removing an event listener obviously does the opposite of what adding does.
I guess you're missing of addEventListener() mechanics. This thing has a global side effect on event engine, registering a callback function along with caller this value to provide correct context of a fired event, with possible update of event.localX and event.localY properties by calling globalToLocal() either statically or dynamically, as the event bubbles up and down.
If you are, like me, confused about how does Flash player determine the target of an event - there is an internal "focus" pointer that determines which component of the SWF has keyboard focus, and that one is used to target keyboard events. For mouse events, most likely Flash engine calls getObjectsUnderPoint() to query for topmost IEventDispatcher compatible objects (not all of the DisplayObjects can process events), and that one is sent a mouse event, with the previous event's target to receive a say MouseEvent.ROLL_OUT or MouseEvent.MOUSE_OUT if the target has been changed. For other events, most likely the entire display list can react.
For objects in the display list, the following excerpt from Adobe is the answer "When Adobe® Flash® Player dispatches an Event object, that Event object makes a roundtrip journey from the root of the display list to the target node, checking each node for registered listeners.".
For non display objects, AS3 run time maintains a dictionary of all AS3 events containing bound variables. The bound variables are a reference to the event listeners.

Modifying the domain memory of a loaded SWF to control behavior

I have several flash games that cannot be paused from within the games. I would like to write a wrapper swf that allows me to pause them. It is not feasible to recompile the games, so no functionality can be added to them.
What I would probably need to do is control when the loaded swf gets ENTER_FRAME events and redirect calls to flash.utils.getTimer to a function defined in the wrapper. Can this be done by modifying the domainMemory ByteArray of the loaded ApplicationDomain object?
domainMemory is definitely doesn't help you, it's just the API to access to the fast memory.
What you can try to do is to decompile the game swf and find the dispatcher of main EnterFrame event (and after loading game swf you have to find this dispatcher through the display list for example if it doesn't turned out to be the stage or root), that is used as the game tick dispatcher, than you will be able to intercept the default EnterFrame event for this dispatcher. For interception just add your own listener with the higher priority:
//pause the game
dispatcher.addEventListener(Event.ENTER_FRAME, onGameEnterFrame, false, 1);
//resume the game
dispatcher.removeEventListener(Event.ENTER_FRAME, onGameEnterFrame);
protected function onGameEnterFrame(event:Event):void
{
event.stopImmediatePropagation();
}

How to clear object from previous frame in Flash with AS3?

I have an object (MovieClip) on stage at some frame, and at the next frame, even though the object is visually removed, it is still there (it has an internal function that generates something on stage periodically, and the stage objects are being generated). I know I can simply stop the action while leaving the frame, but the object will eat up memory and maybe even CPU time for some background actions (and this is a AIR to iOS project so I care about memory and performance). How can I get rid of the object entirely? I expect everything to be removed when I navigate to another frame if it's a designer-placed object (if the object is not generated by ActionScript) and this is the case, but it only gets visually removed.
Thanks,
Can.
Just null your object reference and it's listeners.
...
myMovieClip.removeListeners();//class function
myMovieClip.parent.removeChild(myMovieClip);
myMovieclip = null;
...

AS3: Major Slowdown

I'm working on a Flash game, and after running my game for a while there is a huge drop in frame rate. There aren't a lot of MovieClips onscreen at once, but MovieClips are being replaced using removeChild and addChild often.
How can one test for problems such as memory leaks? And what are some good AS3 programming standards on this matter?
It seems like you're not preparing your instances of MovieClip for garbage collection. This thread could be extremely helpful to you.
Some of the basic things you want to cover when discarding a MovieClip (or any other Object) properly are:
Remove the object from the DisplayList (if it's a DisplayObject). This is done via what you're doing already, removeChild()
Remove any event listeners that have been applied to the Object. Best thing to do is keep on top of this right from the beginning; by that I mean, when you call addEventListener(), be sure to somewhere in the very near future add a sister removeEventListener() as well.
Remove reference to your Object. This includes, but is not limited to: reference to the Object via being part of an Array/Vector, reference via being stored in a property of another Object, etc.
A suggestion that I can offer is to have in the base class of your objects a method that handles all of this, eg remove() or deconstruct().
Here's an example:
public function deconstruct():void
{
if(parent)
parent.removeChild(this);
removeEventListener(MouseEvent.CLICK, _onClick);
}
And when you extend this class and need other dereferencing features, just build on your deconstruct() method:
override public function deconstruct():void
{
removeEventListener(MouseEvent.MOUSE_OVER, _mouseOver);
var i:int = someArray.indexOf(this);
someArray.splice(i, 1);
super.deconstruct();
}
http://gskinner.com/talks/resource-management/