as3: все элементы массива имеют одинаковые атрибуты :( - PullRequest
0 голосов
/ 26 декабря 2010

Я не знаю, почему это происходит, но все элементы моего массива имеют одинаковое значение: (

        public function populateNodes():void
    {   
        // Calculate tile size: 
        // width = sceneWidth/mapSize (map size is Size of the map in tiled editor)
        _tileSize = new Point (PBE.scene.sceneView.width/mapSize.x, PBE.scene.sceneView.height/mapSize.y) 

        // Get Scene bounds
        var left:Point = PBE.scene.sceneViewBounds.topLeft;
        var right:Point = PBE.scene.sceneViewBounds.bottomRight;

        // Defines the center of first tile
        var minX:int = left.x + _tileSize.x/2;
        var minY:int = left.y + _tileSize.y/2;

        // Defines the center of last tile
        var maxX:int = right.x - _tileSize.x/2;
        var maxY:int = right.y - _tileSize.y/2

        //var nodes:Array = new Array(100);
        var place:Point = new Point(minX, minY);

        //_tileSize = new Point(PBE.scene.sceneView.width/mapSize.x, PBE.scene.sceneView.height/mapSize.y);

        var debugLoop:int = 0;
        for each (var tile:* in layers.data.tile)
        {
            var node:Node = new Node();
                node.gid = tile.@gid;
                node.location = place;
                node.size = _tileSize;
                             nodesList.push(node);
            if (place.y >= maxY)
            {
                if(place.x >= maxX)
                    trace("End loop");
            }

            place.x += _tileSize.x;
            // Review case when x reached boarder
            // If yes, add y and point x to begining
            if (place.x >= maxX)
            {
                place.x = minX;
                place.y += _tileSize.y
            }

            // Review when y reached max y size
            // If yes put y to to begining

            if (place.y >= maxY)
            {
                place.y = minY;
            }   
            debugLoop += 1;
        }

        trace ("Done");
    }

Класс узла:

    public class Node
{
    public var collidable:Boolean = false;
    public var gid:int;
    public var size:Point;
    public var location:Point;

     public function Node()
     {

     }

}

Не знаю, почему, но все узлы в списке узлов имеют одинаковое местоположение (местоположение последней точки, помещенной в массив ...)

Может кто-нибудь подсказать почему?

1 Ответ

1 голос
/ 26 декабря 2010

Вы назначаете ссылку на место переменной для каждого из ваших узлов. Следовательно, сохраняется только одно значение, которое вызывается всеми узлами и возвращает последнее установленное значение координат.

Попробуйте это:

node.location = new Point (place.x, place.y);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...