vendredi 23 septembre 2016

Unable to addChild on MouseCoordinations

What i am trying to do is, to place a new tower each time on MouseX and MouseY.. but it seems it isn't working Any Idea guys?

or if you can create a tileMap array and add new child of PrototypeTower each time when we click on the Tower (on x=50, y=400) to select and place wherever we want

Main.as

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.ui.Mouse;

    public class Main extends Sprite 
    {
        private var  pTower:PrototypeTower = new PrototypeTower;
        private var zTower:PrototypeTower = new PrototypeTower;
        private var fTower:PrototypeTower = new PrototypeTower;
        public function Main() 
        {
            pTower.x = 50;
            pTower.y = 400;
            addChild(pTower);

            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }
        private function onAddedToStage(e:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

            pTower.addEventListener(MouseEvent.CLICK, onClicked);
        }
        private function onClicked(e:Event):void
        {
            removeEventListener(MouseEvent.CLICK, onClicked);

            zTower.startDrag();
            zTower.x = mouseX;
            zTower.y = mouseY;
            addChild(zTower);

            addEventListener(MouseEvent.CLICK, onPlaced);
        }
        private function onPlaced(e:Event):void
        {
            removeEventListener(MouseEvent.CLICK, onPlaced);
            zTower.stopDrag();
            fTower.x = mouseX
            fTower.y = mouseY;

            addChild(fTower);

        }
    }
}

PrototypeTower.as

package 
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.Event;

    public class PrototypeTower extends MovieClip 
    {   
        public function PrototypeTower() 
        {
            this.graphics.beginFill(0x00FF00);
            this.graphics.drawRect(this.x, this.y, 20, 20);
            this.graphics.endFill();
        }
    }
}

Thank you, i am totally noob, wondering around from days!

Aucun commentaire:

Enregistrer un commentaire