Domanda

I cannot sort this out few hours now ;(. I want to add a child in a container. This is what I have done so far. All the problems are in class Creator. This line par2.addChild(ship); //works, but this line par2.containerSDv.addChild(ship); //error:1119.

Thanks

    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;
    }

}

.

È stato utile?

Soluzione

I think you might need to take a step back and re-evaluate your overall design and objectives.

consider this approach :

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;
    }

}

Here's an example of a Creator class

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;
    }

}

I made a few assumptions such as you having a Ship class will extend a MovieClip/Sprite and has artwork, and that your NewShipButton class will extend SimpleButton and have artwork.

If this doesn't fit your needs, please explain what it is that you are trying to do that doesn't fit this extremely simple design.

I've seen a number of posts by you that seem to be making things far more difficult than they need to be. Maybe there is a good reason for that, but if so, please explain why the above approach will not work for your purposes.

Altri suggerimenti

If I give name to the Container and uncheck Strict Mode in Settings and my code works. I found very helpful article about the 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;


    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top