0

我现在无法解决几个小时;(。我想在容器中添加一个孩子。这是我到目前为止所做的。所有问题都在 Creator 类中。这一行 par2.addChild(ship); // 有效, 但是这一行 par2.containerSDv.addChild(ship); //error:1119.

谢谢

    public class Main extends MovieClip
{
    private var containerSD:Container = new Container;
    private var ship:Ship2;

    public function Main()
    {
        // constructor code
        stage.addChild(containerSD);
        //ship = new Ship2;
        //containerSD.addChild(ship);

    }
}

.

    public class AddChild2 extends SimpleButton
{
    private var creatorche:Creator = new Creator;
    private var s:Ship2;

    public function AddChild2()
    {
        // constructor code
        addEventListener(MouseEvent.CLICK, onCLICK)
    }

    private function onCLICK(e:MouseEvent)
    {
        s = new Ship2;
        creatorche.onCreator(s, stage);
    }
}

.

    public class Creator extends MovieClip
{
    private var ship:MovieClip;
    private var containerSDv:Container = new Container;

    public function Creator()
    {
        // constructor code
    }

    public function onCreator(par1:MovieClip, par2:Stage)
    {
        ship = par1;
        //par2.addChild(ship); //works
        par2.containerSDv.addChild(ship); //error:1119
        ship.x = Math.random() * 200;
        ship.y = Math.random() * 200;
    }

}

.

4

2 回答 2

1

我认为您可能需要退后一步,重新评估您的整体设计和目标。

考虑这种方法:

public class Main extends MovieClip
{
    private var container:Sprite = new Sprite;
    private var newShipButton:NewShipButton = new NewShipButton;
    private var creator:Creator;

    public function Main()
    {
        creator = new Creator;
        addChild(container);
        addChild(newShipButton);

        newShipButton.addEventListener(MouseEvent.CLICK, newShipButtonHandler);

    }

    private function newShipButtonHandler(e:MouseEvent):void
    {
        // shipStyle would be a MovieClip of the artwork for a given ship.
        var ship:Ship = creator.makeShip(shipStyle, speed, power, container);
        ship.x = Math.random() * 200;
        ship.y = Math.random() * 200;
    }

}

下面是一个 Creator 类的例子

public class Creator
{

    public function Creator()
    {
        // constructor code
    }

    public function makeShip(style:MovieClip, speed:Number, power:Number, container:MovieClip):Ship
    {
        var ship:Ship = new Ship(style);
        ship.speed = speed;
        ship.power = power;
        container.addChild(ship);
        return ship;
    }

}

我做了一些假设,比如你有一个 Ship 类将扩展一个 MovieClip/Sprite 并拥有艺术作品,并且你的 NewShipButton 类将扩展 SimpleButton 并拥有艺术作品。

如果这不符合您的需求,请解释您正在尝试做的事情不适合这个极其简单的设计。

我看过你的一些帖子,这些帖子似乎让事情变得比他们需要的要困难得多。也许有一个很好的理由,但如果是这样,请解释为什么上述方法不适用于您的目的。

于 2013-04-24T18:29:20.993 回答
0

如果我给容器命名并取消选中设置中的严格模式,我的代码就可以工作。我发现关于 DisplayObjectContainer 的非常有用的文章http://joshblog.net/2007/07/12/disabling-actionscript-3-strict-mode-in-flash-cs3/

    public class Creator extends MovieClip
{
    private var ship:MovieClip;
    private var containerSDv:Container = new Container;

    public function Creator()
    {
        // constructor code
    }

    public function onCreator(par1:MovieClip, par2:Stage)
    {
        ship = par1;
        //par2.addChild(ship); //works
        par2.getChildByName("contain").addChild(ship); //error:1119
        ship.x = Math.random() * 200;
        ship.y = Math.random() * 200;
    }

}

.

    public class Main extends MovieClip
{
    private var containerSD:Container = new Container;
    private var ship:Ship2;

    public function Main()
    {
        // constructor code
        stage.addChild(containerSD).name = "contain";
        containerSD.x = 0;
        containerSD.y = 0;


    }
}
于 2013-04-24T18:52:18.013 回答