开发者

Passing parameters to the constructor to an object (Actionscript 3)

开发者 https://www.devze.com 2023-04-10 09:59 出处:网络
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 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 fu开发者_开发百科nction 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号