开发者

as3: All elements of the array has same attributes :(

I don't know why it happens, but all elements of my array has same value :(

        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");
    }

The node class:

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

   开发者_如何学编程  public function Node()
     {

     }

}

I don't know why but all nodes in nodesList, has same location (the location of last point pushed to the array...)

Can somebody suggest why?


You are assigning a reference to the variable place to each of your nodes. Therefore, only one value is stored, which is called by all the nodes, and returns the last coordinates place is set to.

Try this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜