Targeting event.target instance name and not object - actionscript-3

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.

Related

`root` changes in different places

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.

Flash/AS3: How do I access <Instance Name> object from the Document class?

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

Calling MovieClip(root) from a dynamic MovieClip instance produces error 1034

NINJA EDIT:
For some reason, the same code works now, without any problem at all. I don't know what happened, or why, but I no longer have this problem
Here's the original post:
To put simply, I created a MovieClip, put it with addChild() to stage, and when I tried to call this piece of code:
MovieClip(root).someFunction();
It throws Error #1034: Type Coercion failed: cannot convert flash.display::Stage#4034f71 to flash.display.MovieClip.
I really can't figure out why this piece of code won't work. The object itself works perfectly, as I can call functions within it (that line of code is actually within a function). It's just that piece of code that is problematic
Can someone tell me where I went wrong?
EDIT:
To better illustrate the situation, here's my pieces of code:
in a MovieClip, I have this function:
function bombReset():void
{
bBombIsDropped = false;
tCarpetBombTween.gotoAndStop(0);
this.visible = false;
MovieClip(root).carpetBombAttack(iPosition);
}
And on Scene1(root, the outermost parent) I have this function:
function carpetBombAttack(position:int):void
{
damagePlant(15,vTileOccupant[(position-1)]);
}
If I create a MovieClip instance via addChild and call bombReset in it, Flash will throw an error
If I manually drag the MovieClip onto stage, when I call bombReset, it will work fine
Your error means that the compiler doesn't know how a MovieClip and a Stage can be the same thing. Also, I'm not certain, but I believe the compiler will whine about someFunction not existing on the stage even if you casted the stage (aka root) correctly.
The proper way to solve this is by assigning a document class to your project and make someFunction a public method (class function).
The lay-mans solution (which I sometimes use when I'm being lazy) is the following
Object(this.stage).someFunction();
That works because you are type-casting this.stage in a way that makes the compiler think it's an Object instead of a Stage. Objects can have any number of undocumented properties and functions, thus allowing you to call items on the Object whether they are part of a class definition or not (and even ones that don't exist - which is where you can get yourself into trouble).
The inheritance for Stage is Stage -> DisplayObjectContainer -> InteractiveObject -> ... while MovieClip is MovieClip -> Sprite -> DisplayObjectContainer -> InteractiveObject -> ... (I'd link directly to the docs but the pages keep crashing on me).
While they share common base classes, the Stage and MovieClip classes aren't actually related, so trying to cast one as the other will fail.
As you're doing the MovieClip(root) type cast and not the root as MovieClip cast, that's why you're getting the error you're getting.
Either cast it directly to the object that has the someFunction() defined, use the solution defined by Jackson, or if you absolutely know it's there, you can also do root["someFunction"]()

AS3: SetChildIndex not working

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).

Getting a null object reference error for a button instance

I am trying to use this script to jump to a certain point on my timeline:
feature1_btn.addEventListener(MouseEvent.CLICK, feature1);
function feature1(event:MouseEvent):void {
gotoAndPlay(620);
}
I have the instance of my button labeled as "feature1_btn". Why am I getting this error?
You're likely getting this error because you're attempting to add the listener to the instance before the instance has been created and/or initialized. If this code is in your timeline you need to make sure that feature1_btn exists on the stage at that point in the timeline.