as3:数组的所有元素都具有相同的属性:(

时间:2010-12-26 20:30:01

标签: actionscript-3

我不知道为什么会这样,但我的数组的所有元素都有相同的值:(

        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()
     {

     }

}

我不知道为什么但是nodesList中的所有节点都有相同的位置(最后一个点的位置被推送到数组......)

有人能说明为什么吗?

1 个答案:

答案 0 :(得分:1)

您正在为每个节点分配对变量位置的引用。因此,只存储一个值,由所有节点调用,并返回设置的最后一个坐标。

试试这个:

node.location = new Point (place.x, place.y);