AS3 Get currentFrame of current MovieClip - actionscript-3

How can I get the frame number of the current MovieClip. I am trying this.currentFrame but it's coming up as undefined.
To clarify, I'm not looking to find out currentFrame of root or a child MC; instead I am looking to find the currentFrame of the current MovieClip.

Related

Cannot convert root to movieclip

I have this code in a Movieclip that is called onto the stage in the main timeline
if(MovieClip(root).isWithinRange(MovieClip(e.currentTarget), MovieClip(root).hero, 10))
{
if(e.currentTarget.getStatus() == 0)
{
e.currentTarget.unlock();
}
}
And it gives me an error at MovieClip(root). I tried tracing that and it gave me the same error.
Type Coercion failed: cannot convert flash.display::Stage#4e131e9 to flash.display.MovieClip.
Tracing root gives me Object Stage.
So I can't convert the Stage object to a MovieClip, but when I try to skip the cast, it can't use the method because it says its a static type. How do I access this method?
That is because stage is DisplayObject and not MovieClip. So you can convert it into MovieClip:
var stageRef:MovieClip = this.parent as MovieClip;
if object was placen on stage - stage.addChild(yourObject);
or you can acces it via DisplayObject (make sure to import flash.display.DisplayObject;)
DisplayOject(stage)["nameOfUrObject"];
The true answer here is that Stage IS NOT MovieClip. Period.
You cannot do what you wanted to do and I clearly can't understand where this isWithinRange method came from? If you want answers, ask specific questions. I already answered this one, unfortunately it won't help you much :)
p.s.
After reading all comments - here's a link that may help you: AS3 - Call function in root timeline from class
Keeping everything on timeline is a very very bad approach (sorry to say it).

as3 symbol variables not initialized yet

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

referencing a movieclip symbol on stage

I am sure this is a very basic question but I just could not find out how it is done (all tutorials I read have this step skipped).
I am working on Adobe Flash CS4, AS3. I have a movieclip symbol on stage, and I have assigned it with an instance name "myClip".
Then, on frame 10, I want to change its position, say,
myClip.x = 10;
but I only got an error message "Cannot access a property or method of a null object reference." What have I missed?
Thanks a lot!
on timeline myClip layer on frame 10 right click >> insertframe..

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 Require Scope Guidance Please ?

Thanks for taking the time to read... here is my question/scenario, its a quick one:
I have:
Stage -> SWF Loader Root -> SWF Loader -> MovieClip -> Nested MC
From within "Nested MC": I can only access "SWF loaders root" time line, I can't seem to get access to the stage's functions...
Within "Nested MC" I used:
this.parent <- shows "MovieClip"
this.parent.parent <- shows "SWF Loader"
this.parent.parent.parent <- shows "SWF Loader Root"
this.parent.parent.parent.parent <- SHOWS NULL!!!!
Im trying to call on a function which resides on the main time line.
Is there any way to access the main timeline?
Any suggestions will be greatly appreciated.
Am I missing something trivial? Im learning
Sam
You probably want to dispatch an event from your nested MC then listen for the event from the main timeline. Sounds like you're a few layers deep in the display, so make sure you set "bubbles" to true.
From nested MC:
dispatchEvent(new Event("your_custom_event_name", true));
Then on the main timeline:
addEventListener("your_custom_event_name", customEventHandler);
function customEventHandler(e:Event):void {
mainTimelineFunction();
}
function mainTimelineFunction():void {
trace("success");
}