问题
Firstly, something I want to explain is I am not familiar with ActionScript, so don't blame me a lot for some basic mistakes. I have just learned it for a short time only. Thus, a few tips of scripting ActionScript is always appreciated. :)
I don't know why the compiler said the constructor of one of my classes doesn't accept any parameter.
OK. You might get my meaning through code shown below.
Here a Player.as:
public class Player extends Sprite
{
public var mcHealthBar:HealthBar;
public function Player()
{
// Here a compiler error is found.
mcHealthBar = new HealthBar(max_health);
}
}
and also a HealthBar.as:
public class HealthBar extends MovieClip
{
private var max_hp:int;
public function HealthBar(MaxHP:int)
{
// constructor code
max_hp = MaxHP;
}
}
The compiler said HealthBar's constructor could not be used with arguments, but you can clearly see the HealthBar() constructor inside the HealthBar.as has already been written with a argument MaxHP.
Lastly, what i want to ask is, why the compiler error was produced?
NOTE: I am coding by using Flash-Builder and Flash-Professional together. I created a Flash-Professional project through Flash-Builder. I draw by using Flash-Professional, and I code by using Flash-Builder.
There may be some grammatically-wrong English mistakes... So I am very sorry for my very bad English lol...
回答1:
This is generally the problem when I've faced this issue before:
HealthBar
is likely tied to a library object in FlashPro. (you've exported it for actionscript and given it the class name HealthBar
).
The issue is that FlashPro is not seeing the class file, so it generates it's own (which doesn't have the constructor argument).
You can test if flashPro sees the file by clicking the edit icon on the linkage properties of the library object (see image). If the custom made .as file comes up, then this is not the problem.
Usually the cause is that the .as file is not in the right place, or the package name isn't right for where the file physically lives.
If your .as
file is just using package {
, then by default the file should be placed in the same directory as the .fla
file.
If was package bob {
then it should be in a folder called bob (and the folder by default would be in the same directory as the .fla)
You can change where the .fla looks for class files by going to the ActionScript Settings which can be found on the file menu.
That window has tab called Source Path where you can add directories to look for .as files.
回答2:
You might be running into a problem with a Library object you've named HealthBar and assigned the class the same name. If this is the case name the Library object HealthBarMC and the keep the class the same name and see if that works.
来源:https://stackoverflow.com/questions/27021357/a-compiler-error-that-makes-me-be-confused-in-actionscript