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).
Related
My understanding was I could refer to a movie clip on the scene from AS via this.stage.movieclipname, but this keeps yielding the error:
Scene 1, Layer 'Actions', Frame 1, Line 18 1119: Access of possibly undefined property loader_obj through a reference with static type flash.display:Stage.
The loader_obj definitely has an instance name in the properties panel, and is a symbol in the library, so what causes this, and how should I reference it in AS3?
tried linking the object to actionscript, but that caused another error.
You should access root instead of this.stage when you are referring to pre-placed instances. This is considered bad practice, though. Like this:
MovieClip(root).loader_obj.x=0;
Linking an object to AS3 does a completely different thing: it creates a class with a provided name, so then you will be able to do var c=new LinkedClass() and receive an exact copy of a library movie clip or another kind of object.
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..
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"]()
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).
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);