AS3/Flash: Why moving a symbol's class file from a top-level directory to a deeper directory break the link between symbol and class file?
:: EDIT ::
This question is a little sprawling, so I'm trimming it down to make it more concise.
SUMMARY:
When I have a class linked to a MovieClip in my library and that class takes an argument in its constructor method. That class will compile properly ONLY when it's located in my top-level directory (same dir as the .fla and Document.as files). If I move that class to a deeper directory, say com.place, and update the package statement and symbol link appropriately, the compiler will generate error "1136: Incorrect number of arguments. Expected 0."
T开发者_运维问答O RECREATE:
Create flash project and put a rectangle on the stage. Covert it to symbol and assign it to class TestPanel - or whatever you choose. Also configure the fla so that is uses a Document (Main) class.
Create Main.as and TestPanel.as in the same folder. In the Main class, instantiate a instance of TestPanel and add it to the stage. Flash will, predictably, add the rectangle symbol and everything is fine.
Now modify TestPanel so that its constructor method takes a Number and have Main.as pass some number to TestPanel.
public function TestPanel( num:Number ) { trace( 'TestPanel is created: num = ' + num ); }
public function Main() { trace( 'Main is initialized' ); var myTestPanel:TestPanel = new TestPanel( 5 ); addChild( myTestPanel ); }
Now move TestPanel to com/place/TestPanel and update the package statement to reflect its new location. Also update the rectangle symbol in the library so that it links to com.place.TestPanel.
You now get the error: 1136: Incorrect number of arguments. Expected 0.
When I move the TestPanel.as into a deeper directory, Flash somehow is looking elsewhere for the base class for Symbol, even though I'm mapping that Symbol to com.place.TestPanel.
Can anybody recreate this and tell me where I'm tripping up?
( sorry for not highlighting the code in this post. I can't seem to get this editor to work properly )
I've added com.place.MO
to the base class (works for class too for me)
and created an as file
package com.place{
import flash.display.Sprite;
public class MO extends Sprite {
public function MO():void {
rotation = 45;
}
}
}
which is in directory com/place/ which is in the same place as the fla file.
and this works. Could you share the class or the important parts of it ?
Change at the beginning of your AS file:
package {
...
to
package com.place {
...
EDIT : I reproduced the steps and it all works perfectly fine. Make sure FlashIDE finds your class (by clicking on the green tick beside the path). Also double check you don't have any other symbol by the same name that messes it up (stupid but common).
Old answer :
It's all a bit confusing by having definitions in the Library and Linking them to scripts.
If you code using pure Actionscript, the same error message will appear if you extend a class requiring constructor arguments without invoking "super(*arguments*)
" from the extended class. The issue is quite obvious, since all the inherited constructors need to be called at the instantiation of the object, any missing arguments will fail this to happen.
will work :
- class Citroen() -> super(4) -> extends class Car(wheels:int) -> 4 wheels
- class Citroen() -> extends class Car(wheels:int=2) -> at least 2 wheels if not specified
- class Citroen(wheels:int) -> super(wheels) -> extends class Car(wheels:int) -> number of wheels to be specified on instantiation
will not work :
- class Volvo() -> extends class Car(wheels:int) -> no wheels ever :P
Now I guess if you add a class definition to Symbol in the library, you implicitly allow this symbol to be instantiated by dragging it to the stage within Flash IDE without defining its constructor arguments (even though you specifically won't do it). So in theory, this will result in the same thing as explained previously, throwing a '1203: No default constructor found in base class'
Try this : in your base class add default values to all the arguments in the constructor (e.g. name:String = 'me', age:int = 99
). This will allow you to extend the class (by subclassing in from the Library) without passing any arguments. Hopefully this will help the compiler allow you compile.
In case you really want to enforce arguments to be passed, I would suggest you do it the other way around by instantiating and adding the Symbol defined in Flash IDE's library to your TextPanel class ...
Hope it helps a bit ; )
I realized where I'm getting my error.
First, thanks for everybody's input.
What happened was that my Symbol was linking to my class in the 'Base class' field as opposed to the 'Class' field. I don't know exactly when I started this habit but I've been doing it forever and apparently it didn't matter because none of my classes used arguments in their constructor- until now. This also explains why it works in the top-level directory and not when nested in com/place/whatever.
For anybody who stumbles upon this same/similar problem, remember to check to see that you symbol links to your class in the 'Class' field, not the 'Base Class' field.
Here's a link a Adobe's documentation on the subject.
精彩评论