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

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

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

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

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/

Flash crash (ends up in a restart loop) when loading an external swf

Im working with FlashDevelop and have two main projects, all pure AS3 projects.
When trying to load my second project from my main project I get all kinds of errors.
The Main class of the main project extends Sprite and the Main class in the "to-be-imported" project extends MovieClip. Looking at the loading of the swf in the debug window in FD it all seems fine:
[SWF] 'pathToSwf'\secondProject.swf - 410 626 bytes after decompression.
If i try to assign the loaded swf to a newly created MovieClip I get a coercion failiure:
swfContent = loader.content; // =>
Type Coercion failed: cannot convert Main#46c0201 to flash.display.MovieClip.
So, typecasting the loaded content like so:
swfContent = loader.content as MovieClip;
removes that error but then I fall into the next pit as I try to call addChild:
Error #2007: Parameter child must be non-null.
Trying to get around the issue I tried to add the loader directly into the container where I want to show the external swf. This is when the real interesting problems begin:
targetContainer.addChild(loader);
My main application now hang, restarting in a never ending loop. I have no idea why..
So my issue is really. How can my external swf be loaded but then again be null.
It works perfectly fine when I run the external swf by itself...
Use getQualifiedClassName and getQualifiedSuperclassName functions (and even describeType if you must) on loader.content to get its exact type information.
loader.content as MovieClip returns null because loader.content is not a MovieClip - casting with as keyword silently returns null when it fails. Is there any chance that the loaded content is an AS2 movie clip instead of AS3 movie clip? In that case getQualifiedClassName will return "AVM1Movie".
The latter issues is weird, but first try changing the type of swfContent to Sprite. A main class does not always extend MovieClip, and judging from the error message it indeed doesn't in this case.
Your swfContent will be null, if it cannot be casted to MovieClip. That is how the as operator is supposed to work when type coercion fails.
Modify your assignment operation like this:
var swfContent :MovieClip = MovieClip(loader.content);
You might want to encompass the assignment in a try...catch block, as an error will be thrown in case of failure, instead of swfContent being set null, as with as.
So the problem was that the main class of my loaded swf had the same name as the swf I was loading from. This led to that when flash tries to execute the loaded swf it actually calls the parent MAIN class which results in the looping behaviour.
To avoid this DHuntrods suggested to change the application domain which solved the issue.
loader = new Loader();
var AD:ApplicationDomain = new ApplicationDomain( null );
var context:LoaderContext = new LoaderContext( false, AD );
loader.load(new URLRequest(path), context);