AS3 Require Scope Guidance Please ? - actionscript-3

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

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

in what situation,gotoAndStop is not working

I have 3 frames, I don't show the whole code, it's too huge.and the main code is
gotoAndStop(2);
trace('frame:',currentFrame)
the output should be frame: 2
But in fact it's frame: 1, and objects in frame 2 cannot be loaded and become null
no compiler errors
When I delete some codes after it, the application sometimes operate right and stops at frame 2.
This shouldn't happen as any code after it should not involve the output
although I can solve this when I delete the first frame,but it's quite risky to keep developing.
Any ideas why this happen?
Before keep going,I should mention I actually have compiler errors due to null objects,but it's not the main point.
And I have a class called host,the code gotoAndStop is also in the first place of constructor.
I have put override function in host, and the output is
stopping at frame: 2 Called from: Error
at host/gotoAndStop()
at host()
frame: 1
TypeError: Error #1009: Cannot access a property or method of a null object reference
at host()
Then I tried the method2 Creative Magic says,the result shows
stopping at frame: 2 Called from: Error
at host/gotoAndStop()
at host()
displaying frame: 1
frame: 1
TypeError: Error #1009: Cannot access a property or method of a null object reference
at host()
displaying frame: 2
This quite confuses me what's happening in the frame,I wonder the cause is like you say old version of SDK,thanks for help
Your problem can be either you've written buggy code or you use old Flash SDK.
In the first case you should check if you're calling gotoAndStop(), play() or gotoAndPlay() methods of your MovieClip. To do so you can edit the class of your MC:
In Flash Professional in the library panel right-click on the MC
Select "Edit Class"
In your selected IDE add following function
override public function gotoAndStop(frame:Object, scene:String = null):void
{
trace("stopping at frame:", frame, "Called from:", new Error().getStackTrace());
super(this).gotoAndStop(frame, scene);
}
It will trace when the gotoAndStop() method was called and who called it. It will let you see if the frame was set to frame by something else. I recommend you to override play() and gotoAndPlay() functions as well.
If you're intimidated by this code you could just add a trace() on each frame of your MC, since it's only 3 frames it shouldn't be too much work. Your trace could look something like that:
trace("displaying frame:", this.currentFrame);
The other possible cause could lie within the SDK you're using. I, myself had a strange bug when the loaded MC wouldn't listen to stop(), gotoAndStop() and other functions. The problem was solved by upgrading the SDK: http://www.adobe.com/devnet/flex/flex-sdk-download.html
The explanation on how to replace the old SDK is there as well.

AS3: Cannot access a property or method of a null object reference at $iinit while loading external swf

I have two swfs, that works perfect separately, but when i try to load one to other, it shows me "Cannot access a property or method of a null object reference at Creator$iinit".
First SWF is login screen, second is register screen. I load them in this way:
var requestA:URLRequest = new URLRequest("Creator.swf");
var loader:Loader = new Loader();
loader.load(requestA);
addChild(loader);
I have no idea whats wrong. Please, help.
Most likely you're trying to access something that is not available immediately on load and init - you'll need to add some code in the Creator.swf like:
// Somewhere in the first lines of code
addEventListener(Event.ADDED_TO_STAGE, this.ready);
function ready(e:Event) {
removeEventListener(Event.ADDED_TO_STAGE, this.ready);
// ** Do other initialization stuff here
}
What's happening is that you're trying to access the stage or objects before things are ready to accept them.

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"]()