mercredi 21 septembre 2016

AS3 - 1st character resets (replaces) when i add another character

Well i am totally newbie to AS3, and i am learning it on my own.. but since this problem occurred, i can't help myself anymore

the thing is simple, when i run this code, if i place my first character, it gets placed wherever i want (based upon mouseX, mouseY) but when i place the second character (ConnecterMain.as) it resets the place of First Character, anyone better solution :/ and i wanted a block/area with menu of those Characters, but i find this alternative since i couldn't create a block/area like that (assuming all other classes are only body of the character, nothing more)

code

 package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;

    public class Main extends Sprite 
    {
        public var powers:PowerStation = new PowerStation();
        public var powers1:PowerStationMain = new PowerStationMain;
        public var powers2:PowerStationPrototype = new PowerStationPrototype();
        public var con:Connecter = new Connecter;
        public var con1:ConnecterMain = new ConnecterMain;
        public var con2:ConnecterPrototype = new ConnecterPrototype;
        public var sp:Boolean = false;
        public var spc:Boolean = false;

        public function Main() 
        {   
            powers.x = 50;
            powers.y = 550;
            addChild(powers);

            con.x = 120;
            con.y = 560;
            addChild(con);

            stage.addEventListener(Event.ENTER_FRAME, update);
        }

        public function update(e:Event):void
        {
            var mousePoint:Point = new Point(mouseX, mouseY);
            if (mousePoint.x < powers.x + (powers.width - 1) && mousePoint.x > powers.x + 1)
            {
                if (mousePoint.y < powers.y + (powers.height - 1) && mousePoint.y > powers.y + 1)
                {
                    addEventListener(MouseEvent.CLICK, selected);
                }   
            }
            if (mousePoint.x < con.x + (con.width) && mousePoint.x > con.x - con.width)
            {
                if (mousePoint.y < con.y + (con.height / 2) && mousePoint.y >  con.y - (con.height / 2))
                {
                    addEventListener(MouseEvent.CLICK, selectedCon);
                }
            }
            function selected(e:MouseEvent):void
            {
                powers2.x = mousePoint.x;
                powers2.y = mousePoint.y;
                addChild(powers2);
                powers2.startDrag();
                sp = true;
                removeEventListener(MouseEvent.CLICK, selected);
            }
            if (sp === true)
            {
                stage.addEventListener(MouseEvent.MOUSE_DOWN, place);
            }
            function place(e:MouseEvent):void
            {
                powers1.x = powers2.x;
                powers1.y = powers2.y;
                powers2.stopDrag();
                addChild(powers1);
                stage.removeEventListener(MouseEvent.MOUSE_DOWN, place);
            }
            function selectedCon (e:MouseEvent):void
            {
                con2.x = mousePoint.x;
                con2.y = mousePoint.y;
                addChild(con2);
                con2.startDrag();
                spc = true;
                removeEventListener(MouseEvent.MOUSE_DOWN, selectedCon);
            }
            if (spc === true)
            {
                stage.addEventListener(MouseEvent.MOUSE_DOWN, placedCon);
            }
            function placedCon(e:MouseEvent):void
            {
                stage.removeEventListener(MouseEvent.MOUSE_DOWN, placedCon);
                con1.x = con2.x;
                con1.y = con2.y;
                con2.stopDrag();
                addChild(con1);
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire