为什么X表现得像Y?

时间:2014-12-18 19:58:49

标签: javascript

这是我的代码,它看起来比它简单。我只是使用了很多变量来初始化我的地图的第一个方块。在那之后我尝试制作另一个具有正确尺寸的地图方块,看起来X的行为就像Y.我的意思是,我所说的它应该是创建一条垂直线,但它正在创建相反,它对我来说非常令人费解,我发现代码中没有任何错误,我正在打印coords,它们看起来没问题。

var addRooms = function(map, mapSize, roomz) {
            var firstX = 20 - (Math.floor(Math.random()*3)+5);
            var firstY = 20 - (Math.floor(Math.random()*3)+5);
            var firstEndX = 20 + (Math.floor(Math.random()*3)+5);
            var firstEndY = 20 + (Math.floor(Math.random()*3)+5);
            var startX = 0;
            var startY = 0;
            var endX = 0;
            var endY = 0;
            var rooms = [];
            var canAdd = false;
            var lastRoom = {};

        for(var i = 0; i < roomz; i++) {
            canAdd = false;
            startX = lastRoom.midX - (Math.floor(Math.random() * 2) + 1);
            endX = startX + Math.floor((Math.random() * 2) + 2);
            startY = lastRoom.endY;
            endY = startY + Math.floor((Math.random() * 10) + 5);
            if(endY > mapSize)
                endY = mapSize;
            if(endX > mapSize)
                endX = mapSize;

            var roomWidth = endX - startX;
            var roomHeight = endY - startY;
            var firstroomWidth = firstEndX - firstX;
            var firstroomHeight = firstEndY - firstY;
            var midX = (startX + Math.floor(roomWidth/2));
            var midY = (startY + Math.floor(roomHeight/2));
            var firstmidX = (firstX + Math.floor(firstroomWidth/2));
            var firstmidY = (firstY + Math.floor(firstroomHeight/2));
            //var rooms = [];
            var firstRoom = { startX: firstX, startY: firstY, endX: firstEndX, endY: firstEndY, midX: firstmidX, midY: firstmidY };

            var newRoom = { startX: startX, startY: startY, endX: endX, endY: endY, midX: midX, midY: midY };


                if(!rooms[0]) {
                    canAdd = true;
                    newRoom = firstRoom;
                    lastRoom = firstRoom;
                }
                if(!canAdd) {
                    for(var key in rooms) {
                        if(!checkIntersect(newRoom, key)) {
                            canAdd = true;
                            lastRoom = newRoom;
                        }
                    }
                }
                if(canAdd) {
                    console.log(newRoom)
                    rooms.push(newRoom);
                }
        }

        for(var key in rooms) {


                for (var y = rooms[key].startY; y < rooms[key].endY; y++) {
                    for (var x = rooms[key].startX; x < rooms[key].endX; x++) {
                    if(typeof map[x][y] !== "undefined") {
                    map[x][y] = 0;
                    }
                }
            }
        }
    };

正如你可以看到这些坐标印刷品应该创建一个垂直方块:

{ startX: 14, startY: 14, endX: 27, endY: 26, midX: 20, midY: 20 }
{ startX: 19, startY: 26, endX: 22, endY: 39, midX: 20, midY: 32 }
{ startX: 18, startY: 39, endX: 21, endY: 52, midX: 19, midY: 45 }
{ startX: 17, startY: 52, endX: 19, endY: 58, midX: 18, midY: 55 }
{ startX: 17, startY: 58, endX: 20, endY: 66, midX: 18, midY: 62 }

1 个答案:

答案 0 :(得分:1)

你不应该查询

map[x][y];

喜欢这样吗?

map[y][x];