mercredi 14 décembre 2016

ActionScript3, dropping on stage not working after using MouseEnabled

I'm targing to do do a BattleShip Flash game using ACtionScript 3. 1- I draw a grid (class: com.Battleship.grid) using a gridCell:movieClip (class: com.battleship.GridCell) 2- I draw a Ship (class: com.battleship.Bateau)

My objective is when I drag the ship to the grid the alpha cell under the mouse changes, and if i drop: 1- if the droptarget is a gridCell the ship takes place (works fine) 2- if the droptarget is stage the ship return to originPosition (not working) My problem: is after I used MouseEnabled= false, the ship can drop on the grid but not on the stage and still dragging. here is e my code:

var grid:Grid = new Grid(10,new Point(20,20));
var b1:Bateau = new Bateau();
b1.x = 490;
b1.y = 300;
b1.originPos = new Point(b1.x,b1.y);    
addChild(b1)
addChild(grid);

package com.battleship  {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.events.Event;


    public class Bateau extends MovieClip {

        private var _originPos:Point;

        public function Bateau() {
            originPos = new Point();
            buttonMode = true;
            addEventListener(MouseEvent.MOUSE_DOWN, onDrag);

        }
        protected function onDrag(e:MouseEvent):void
        {
            e.currentTarget.parent.addChild(e.currentTarget);
            e.currentTarget.startDrag();
            e.currentTarget.mouseEnabled = false;
            //e.currentTarget.mouseChildren = false;
            addEventListener(MouseEvent.MOUSE_UP, onDrop);
            parent.addEventListener(MouseEvent.MOUSE_UP, onDrop);
        }
        protected function onDrop(e:MouseEvent):void
        {
            e.currentTarget.mouseEnabled = true;
            e.currentTarget.mouseChildren = true;
            e.currentTarget.stopDrag();
            if(dropTarget == null){
                x = originPos.x;
                y = originPos.y;
            }else{
                trace(e.currentTarget.name);
                if(dropTarget.parent is GridCell)
                {
                    x = dropTarget.parent.x+20;
                    y = dropTarget.parent.y;
                }
            }           
        }
        public function get originPos():Point
        {
            return _originPos;
        }
        public function set originPos(originPos:Point):void
        {
            _originPos = originPos;
        }
    }

}

    package com.battleship  {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;


    public class GridCell extends MovieClip {
        private var _row:int;
        private var _col:int;
        private var _val:int;

        public function GridCell()
        {
            addEventListener(MouseEvent.CLICK,onClick);
            addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
            addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
        }

        public function onClick(e:MouseEvent)
        {
            //trace("(" + e.currentTarget.row+ ", " + e.currentTarget.col + ")");
            trace(e.currentTarget.name);
        }

        public function onMouseOver(e:MouseEvent)
        {
            alpha = 0.6;
        }

        public function onMouseOut(e:MouseEvent)
        {
            alpha = 1;
        }

        public function get row():int
        {
            return _row;
        }
        public function set row(row:int):void
        {
            _row = row;
        }
        public function get col():int
        {
            return _col;
        }
        public function set col(col:int):void
        {
            _col = col;
        }
        public function get val():int
        {
            return _val;
        }
        public function set val(val:int):void
        {
            _val = val;
        }
    }

}

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

    public class Grid extends Sprite {      
        private var _size:int;
        private var _position:Point;

        public function Grid(size:int,position:Point):void
        {
            _size = size;
            _position = position;
            var gridCells:Array = new Array(size,size);
            var i,j:int;
            for(i=0;i<size;i++)
            {
                gridCells[i] = new Array();
                for(j=0;j<size;j++)
                {
                    var cell:GridCell = new GridCell();
                    cell.row = j+1;
                    cell.col = i+1;
                    cell.x = this.position.x + cell.width * (i+1);
                    cell.y = this.position.x + cell.height * (j+1);
                    cell.name = "cell" + (j+1) + (i+1);
                    //cell.addEventListener(MouseEvent.CLICK,onClick);
                    gridCells[i][j] = cell;
                    this.addChild(gridCells[i][j]);
                }

            }
        }

        public function get size():int
        {
            return _size;
        }
        public function set size(size:int):void
        {
            _size = size;
        }
        public function get position():Point
        {
            return _position;
        }
        public function set position(position:Point):void
        {
            _position = position;
        }

    }

}

Aucun commentaire:

Enregistrer un commentaire