我现在几个小时都无法解决这个问题;(。我想在容器中添加一个孩子。这是我到目前为止所做的。所有问题都在类Creator中。这行par2.addChild(船); //有效,但是这行par2.containerSDv.addChild(ship); //错误: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;
}
}
答案 0 :(得分: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并有艺术作品。
如果这不符合您的需求,请解释您尝试做的不适合这种非常简单的设计。
我看过很多帖子似乎让事情变得比他们需要的要困难得多。也许有充分的理由,但如果是这样,请解释为什么上述方法不适用于您的目的。
答案 1 :(得分:0)
如果我给Container命名并取消选中设置中的严格模式,我的代码就可以了。我发现了有关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;
}
}