在类AS3之间传递对象引用

时间:2015-04-13 02:49:02

标签: actionscript-3

制作地图制作程序。 Field.as实例化_player。 Player.as将Kid_editorPanel添加到父级。当我单击编辑器面板中的按钮时,EditorPanel.as实例化一个名为tilepalette的动画片段。 Tilepalette拥有我希望能够通过单击我想要的图块来选择图块的所有图块集。

Field.as:

package  {

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.display.FrameLabel;
    import flash.display.DisplayObjectContainer;
    import flash.geom.Rectangle;

    public class Field extends MovieClip{

        private var player:Player;
        private var sampleTile:Tile = new Tile();
        private var _tilePalette:TilePalette;
        private var _paletteArray:Array = [];

        public function Field() 
        {
            player = new Player();
            player.x = 0;
            player.y = 0;
            addChild(player);

            GetSampleTiles();
        }

        private function GetSampleTiles()
        {
            for (var i:int = 1; i <= sampleTile.totalFrames; i++)
            {
                var tileObj:Object = new Object();

                sampleTile.gotoAndStop(i);

                var graphicData:BitmapData = new BitmapData(32,32);
                graphicData.draw(sampleTile);

                tileObj.Name = sampleTile.currentFrameLabel;
                tileObj.Graphic = graphicData;
                tileObj.Frame = sampleTile.currentFrame;

                Engine._tilesData.push(tileObj);
            }
        }
    }
}

Player.as:

package  {

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.display.BitmapData;
    import flash.display.Bitmap;


    public class Player extends MovieClip 
    {
        private var _inp:Input = new Input();
        private var _playerXindex:int = 0;
        private var _playerYindex:int = 0;
        private var _heldTile:Object;
        private var _editorPanel:EditorPanel;

        public function Player() 
        {
            addChild(_inp);
            addEventListener(Event.ENTER_FRAME, HandleKeys);

            _heldTile = new Object();
        }

        private function HandleKeys(e:Event)
        {
            _playerXindex = x/32;
            _playerYindex = y/32;

            if(_inp.keyUp)
            {
                y -= 32;
            }
            if(_inp.keyDown)
            {
                y += 32;
            }
            if(_inp.keyLeft)
            {
                x -= 32;
            }
            if(_inp.keyRight)
            {
                x += 32;
            }
            if(_inp.keySpace)
            {
                try{DrawATile(_heldTile);}
                catch(err:Error){trace("Bad or no graphic. Using this instead.");}
                finally{DrawATile(Engine._tilesData[0]);}
            }
            if(_inp.keyA)
            {
                trace(Engine.tileObjIndex[_playerYindex][_playerXindex].Name);
            }
            if(_inp.keyB)
            {
                BuildStarterIndex();
                SetHeldTile(Engine._tilesData[0]);
            }
        }

        private function DrawATile(tileToDraw:Object)
        {
            if (Engine.tileObjIndex[_playerYindex][_playerXindex].Name.indexOf("Placeholder") >= 0)
            {
                trace("set");
                var graphicData:BitmapData = new BitmapData(32,32);
                graphicData.draw(tileToDraw.Graphic);

                Engine.tileObjIndex[_playerYindex][_playerXindex].Name = tileToDraw.Name;
                Engine.tileObjIndex[_playerYindex][_playerXindex].Graphic = tileToDraw.Graphic;

                var newTile:Bitmap = new Bitmap(tileToDraw.Graphic);
                newTile.x = x;
                newTile.y = y;
                parent.addChild(newTile);
            }
        }

        private function BuildStarterIndex()
        {
            _editorPanel = new EditorPanel();
            _editorPanel.x = 1;
            _editorPanel.y = 544;
            parent.addChild(_editorPanel);

            for (var i:int = 0; i < 20; i++)
            {
                Engine.tileObjIndex[i] = [];

                for (var u:int = 0; u < 20; u++)
                {
                    var tileObj:Object = new Object();

                    var graphicData:BitmapData = new BitmapData(32,32);
                    graphicData.draw(Engine._tilesData[0].Graphic);

                    tileObj.Name = "Placeholder" + "["+i+"]"+"["+u+"]";
                    tileObj.Graphic = graphicData;
                    tileObj.Frame = 0;

                    Engine.tileObjIndex[i].push(tileObj);
                }
            }
        }

        private function SetHeldTile(tiletoset:Object)
        {
            _heldTile.Name = tiletoset.Name;
            _heldTile.Graphic = tiletoset.Graphic;
            _heldTile.Frame = tiletoset.Frame;

            _editorPanel.tileName.text = _heldTile.Name;
            _editorPanel.tileFrame.text = _heldTile.Frame;

            var heldTile:Bitmap = new Bitmap(_heldTile.Graphic);
            heldTile.x = 30;
            heldTile.y = 31;
            _editorPanel.addChild(heldTile);
        }
    }
}

EditorPanel.as:

package  {

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

    public class EditorPanel extends MovieClip
    {
        public var _tilePalette:TilePalette;
        private var _pArray:Array = [];
        private var _mouseXi:int;
        private var _mouseYi:int;
        public var _tileGrabbed:Object;

        public function EditorPanel() 
        {
            btnpalette.addEventListener(MouseEvent.CLICK, BuildPalette);
        }

        private function UpdateMouse(e:Event)
        {
            _mouseXi = Math.floor(_tilePalette.mouseX/32);
            _mouseYi = Math.floor(_tilePalette.mouseY/32);
        }

        private function BuildPalette(e:MouseEvent)
        {
            _tilePalette = new TilePalette();
            _tilePalette.x = mouseX;
            _tilePalette.y = mouseY-256;
            addChild(_tilePalette);
            var counter:int = 1;

            for (var i:int = 0; i < 8; i++)
            {
                _pArray[i] = [];
                for(var u:int = 0; u < 8; u++)
                {
                    if(counter >= Engine._tilesData.length)
                    {
                        counter = 1;
                    }
                    var b:Bitmap = new Bitmap(Engine._tilesData[counter].Graphic);
                    b.x = 32 * u;
                    b.y = 32 * i;
                    _tilePalette.addChild(b);

                    _pArray[i].push(Engine._tilesData[counter]);
                    counter++;
                }
            }
            _tilePalette.addEventListener(MouseEvent.MOUSE_MOVE, UpdateMouse);
            _tilePalette.addEventListener(MouseEvent.CLICK, GrabATile);
        }

        private function GrabATile(e:MouseEvent)
        {
            return _pArray[_mouseXi][_mouseYi];
        }
    }
}

问题在于,当我执行GrabATile时,我不知道如何处理返回的Object引用。我想在Player函数SetHeldTile中使用该Object。

1 个答案:

答案 0 :(得分:0)

所以这会使它工作,但我建议稍微重构一下代码。添加向父母添加内容的子项并不是最好的设计......在编辑器选择图块的地方还会有循环搜索,但是当玩家被告知有关时,播放器会将类似tileName的内容设置回编辑器。新瓷砖。这应该发生在进行磁贴选择等的编辑器中。

那就是说,根据您的流程,以下是如何使其发挥作用:

EditorPanel已经有一个属性_tileGrabbed。所以在你的GrabATile()方法中只需设置

_tileGrabbed = _pArray[_mouseXi][_mouseYi];

然后在你的setHeldTile()方法中......简单地说

_heldTile = _editorPanel._tileGrabbed.  

现在,如果你需要GrabATile()到TRIGGER setHeldTile(),处理它的最佳方法是使用一个事件。

// in EditorPanel

private function GrabATile(e:MouseEvent){
    _tileGrabbed =  _pArray[_mouseXi][_mouseYi];
    dispatchEvent(new Event("tileChanged"));
}

// in Player

private function BuildStarterIndex(){
    _editorPanel = new EditorPanel();
    _editorPanel.addEventListener("tileChanged", setHeldTile);
    // ... rest of code
}

private function SetHeldTile(event:Event){
    _heldTile = _editorPanel._tileGrabbed;
    // ... rest of code
}