将参数传递给对象的构造函数 (Actionscript 3)
我正在开发一款横向卷轴平台游戏。因此,我有一个类作为地板。我还有一个代表敌人的班级。
我在将参数传递给自定义类的构造函数时遇到问题。类 (SkullDemon.as) 扩展了 MovieClip。我正在尝试传递名为“FloorLevel1”的客户类的实例(称为“floorL1”)。 “FloorLevel1”还扩展了 MovieClip(我没有“FloorLevel1”的书面 .as 文件;我只是将地板 MovieClip 导出到该类)。
我试图传递“FloorLevel1”的实例,以便“SkullDemon”对象可以像在平台游戏中一样落在地板上。
我的主类称为“TME2_Main”。这是我尝试将“floorL1”实例传递给“SkullDemon”类的类。这就是我尝试创建 Skull Demon 实例并将“floorL1”传递给其构造函数的方式:
skullD1 = new SkullDemon(floorL1);
我尝试在“TME2_Main”构造函数中创建 SkullDemon。
这是“SkullDemon”类的构造函数:
// Constructor (takes in Level 1's floor variable as an argument
public function SkullDemon(floorL1:FloorLevel1) {
//public function SkullDemon() {
// Move the Skull Demon as soon as it is created
moveSkullDemon();
}
运行 .swf 时出现两种类型的错误:
ArgumentError: Error #1063: Argument count mismatch on SkullDemon(). Expected 1, got 0.
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at TME2d_Main()
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChild()
at flash.display::Stage/addChild()
at TME2d_Main()
我在这里做错了什么?我花了一段时间寻找解决方案(包括将代码移到 TME2_Main 的构造函数之外),但到目前为止没有任何东西真正帮助我。
I am working on a side-scrolling platformer game. Because of this, I have a class that acts as a floor. I also have a class representing enemies.
I am having problems with passing a parameter to a custom class' constructor. The class (SkullDemon.as) extends MovieClip. I am trying to pass an instance (called "floorL1") of a customer class called "FloorLevel1." "FloorLevel1" also extends MovieClip (I do not have a written .as file for "FloorLevel1"; I just exported the floor MovieClip to that class).
I am trying to pass an instance of "FloorLevel1" so that the "SkullDemon" object can land on the floor just like in a platformer.
My main class is called "TME2_Main." This is the class from which I try to pass the "floorL1" instance to the "SkullDemon" class. This is how I try to create a Skull Demon instance and pass "floorL1" to its constructor:
skullD1 = new SkullDemon(floorL1);
I try to create the SkullDemon within "TME2_Main's" constructor.
Here is the "SkullDemon" class' constructor :
// Constructor (takes in Level 1's floor variable as an argument
public function SkullDemon(floorL1:FloorLevel1) {
//public function SkullDemon() {
// Move the Skull Demon as soon as it is created
moveSkullDemon();
}
I get two types of errors when I run the .swf:
ArgumentError: Error #1063: Argument count mismatch on SkullDemon(). Expected 1, got 0.
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at TME2d_Main()
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChild()
at flash.display::Stage/addChild()
at TME2d_Main()
What am I doing wrong here? I have spent a while looking for solutions (including moving code outside of TME2_Main's constructor), but nothing has really helped me so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来舞台上某处有一个 SkullDemon 实例。如果这样做,Flash 将尝试调用 SkullDemon 的构造函数而不传递任何参数。当问题发生在 Sprite 类的 constructChildren() 方法中时,您通常可以判断出这是问题所在。
Sounds like you have an instance of SkullDemon somewhere on the stage. If you do, Flash will attempt to call the constructor for SkullDemon without passing any arguments. You can usually tell this is the problem when it happens in the Sprite class's constructChildren() method.