In ActionScript 3, I've read that the root variable references an instance of the Document Class.
Within my document class constructor, a trace(this == root) returns true. Later in the constructor, a constructor of another class is called. This constructor, however, states that root is null. Finally, tracing from an event listener gives me the result that root is [object Stage].
My goal is to have a single instance of a Document Class (in MainGame.as) and to be able to refer to that as (root as MainGame) throughout my ActionScript program. How can I do this?
If it matters, all of my code is in the default package.
Thanks!
The root propety of a DisplayObject becomes a reference to the Document Class once the DisplayObject is added to the display list. You can continue to use root but be aware that only objects on the display list will work.
You can read more about root here:
The root property of the Stage object is the Stage object itself. The root property is set to null for any display object that has not been added to the display list, unless it has been added to a display object container that is off the display list but that is a child of the top-most display object in a loaded SWF file.
Related
I have a drag and drop SWF I created and am now trying to implement a new function where I need to pass the instance and x/y coordinates to a different function outside of the startDrag and stopDrag functions. I'm trying to target the instance name by setting up a variable and storing the event.target in in the startDrag function.
However, the issue is that when I run a trace on that variable, it doesn't return the instance name. For example, I have a movieclip with an instance name of myClip1. Yet, when I run a trace on the variable, the console reports it as [object myClip1_1].
How can I instead pass the actual instance to the variable so I can use with the external function?
Update: I figured out why I was getting "object myClip1_1" as the output: somehow AS Linkage in the library got turned on. I've disabled that, but now, when I run a trace on event.target and event.currentTarget, I just get "[output MovieClip]" and not the instance name. What gives?
just add .name to event.target:
event.target.name
EDIT:
You shouldn't need the instance name to connect to the object that is being dragged. just say in the startDrag function:
currentDragObject = event.target.
currentDragObject is defined at the global scope:
var currentDragObject:MovieClip;
In this manner, currentDragObject will always be reset to the object being dragged.
I have a function in a child that is supposed to start the timer in the parent class. The code to do so is:
MovieClip(this.parent).Investment1Timer.start();
But when it tries to do that, I get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
I can access the parent's functions and variables just fine, but I can't start any of its timers.
It's likely a scope issue, and Investment1Timer in the parent is private.
It should be :
public var Investment1Timer:Timer;
If you don't specify public or private, it will default to private and will not be accessible outside of that class code.
You should also verify that this.parent is not null and is actually a MovieClip or inherits from MovieClip.
I find the best way to investigate this kind of issue is by running in debug mode as you can then just check the variables involved to pinpoint what is null. Debugger is your friend!
I should also note that it's possible that you have declared Investment1Timer, but you did not create the instance, therefore it is still null ?
In Flash CS4, I can give an object in my timeline an instance name via the properties pannel. I figured this variable would be available to me in my Document class, but when I try to access it, it's null.
How can I get access to these instance from within my Document class? Also, do the objects have to be in the timeline at frame 1 in order to be available to my Document class's constructor?
The constructor is always the first thing called on an object, it is used to get the memory necessary to store all the properties for that object type. After an object is constructed then (assuming it's a display object) at some point it's added to the stage. If an instance of one of your objects is added to the stage on a frame then the main document class will have to know when that object was added to the stage to appropriately access it. You could create the instance of the object within your document class constructor then when added to stage fires on your main class you could add the instance.
Just attempted this wasn't able to reproduce the OPs initial behavior, I'm able to access the instance that is added on frame 1, posted some examples here:
http://www.shaunhusain.com/TestHandleOnInstance/TestHandleOnInstance.as
http://www.shaunhusain.com/TestHandleOnInstance/TestHandleOnInstance.swf
http://www.shaunhusain.com/TestHandleOnInstance/TestHandleOnInstance.fla
I'm working with the QuickBox2D library, and I'm trying to move one object to the bottom of the stage.
But it's not working, QuickBox2D doesn't seem to recognize it.
Call to a possibly undefined method setChildIndex through a reference with static type com.actionsnippet.qbox:QuickBox2D.
Object:
var gameBall:QuickObject;
ChildIndex
sim.setChildIndex(gameBall, 0);
The QuickBox2D class inherits from EventDispatcher, not DisplayObjectContainer where setChildIndex is defined. I don't see a way to handle z-indicies in the QuickBox2D system, which actually makes sense to me because objects shouldn't overlap in a physical system.
Let's have a look at setChildIndex()
It's a method defined by DisplayObjectContainer.
Its first argument must be of type DisplayObject.
Currently, you're trying to:
Call setChildIndex() from an instance of QuickBox2D (not a DisplayObjectContainer).
Pass another instance of QuickBox2D as the first agument (which needs to be a DisplayObject).
Objetcs created inside a function are automatically marked for garbage collection if not referenced anywhere else?
Let´s say, I have a class called SubClass. in the constructor I create some displayObjects.
Then I instatiate SubClass somewhere. When I remove this SubClass instance, will the objects inside be marked for garbage collection?
thanks in advance!
cheers
Bruno
Yes, unless you have references to SubClass's members anywhere outside SubClass, or you keep an active reference to the outside of your class from within SubClass (or any objects within it).
A typical example of the latter is if SubClass subscribes to a Stage event; if the listener is not weak (5th argument of addEventListener) you will keep an active link between the stage and your SubClass instance, and even if you remove the object and null it, it will not be collected.
For simple listeners you can set that 5th argument to true, so the reference is weak and will be broken by the garbage collector. For more complex situations (or for example NetStreams, Loaders, Timeline audio, etc...), you need to create a way for the class to unlink itself for any outside objects and stop any process that could prevent collection, like a public destroy() method that closes requests, stopping media, removeListeners, etc...
But then again, for simple situations when you only have isolated childs, and no references to the outside of your class, simply removing your instance and nulling its reference should be enough for the garbage collection.