简体   繁体   English

这个数组怎么了...?

[英]What's wrong with this array…?

I am making a game where a user moves around a grid and has to collect passengers. 我正在制作一个游戏,其中用户在网格上四处移动并且必须要收集乘客。 I have done almost everything except I need to check whether the player moves over a passenger and if so, display an alert box. 除了需要检查玩家是否在乘客上方移动之外,我几乎已完成所有操作,如果需要,请显示警报框。 Here is my JSFiddle: http://jsfiddle.net/nZ8vA/9/ and the previous: http://jsfiddle.net/nZ8vA/7/ which contains the array I had before. 这是我的JSFiddle: http : //jsfiddle.net/nZ8vA/9/和上一个: http : //jsfiddle.net/nZ8vA/7/ ,其中包含我以前拥有的数组。 As you can see I have an array called map which is what is used to construct the grid. 如您所见,我有一个名为map的数组,该数组用于构造网格。

In the previous JSFiddle I was still attempting to make it detect when the user collects a passenger (the letter "P" in the map). 在先前的JSFiddle中,我仍试图使其能够检测到用户何时集合乘客(地图中的字母“ P”)。 In my most recent JSFiddle I have created a 2d array of objects thanks to previous help on here. 在最近的JSFiddle中,由于这里的先前帮助,我创建了一个二维对象数组。 The map is not fully constructed, it should look like it is in the first JSFiddle, however it seems to read the whole line as being one colour. 该地图尚未完全构建,看起来应该像第一个JSFiddle一样,但是似乎将整条线读为一种颜色。 If you open your console and move the entire top row is read as green when it shouldn't. 如果打开控制台并移动整个顶行,则不应将其读取为绿色。 I also think it is reading the passengers by colour which is not what I intend as the passengers are on different colour squares. 我还认为这是按颜色读取乘客的,这不是我想要的,因为乘客位于不同的颜色方块上。

Can someone help me detect when a user collects a passenger, I have a function which detects squares with the "P" but I need to keep the same grid colour layout. 有人可以帮助我检测到用户何时收乘乘客吗?我有一个功能可以检测带有“ P”的正方形,但是我需要保持相同的网格颜色布局。 I hope this makes sense. 我希望这是有道理的。

This is the function: 这是功能:

Old JSFiddle: 旧的JSFiddle:

function checkPass(cell, row) {
    var pass = map[row][cell];
    console.log(pass);
    if (pass == "p") {
    alert("Passenger");
      }
    }

New JSFiddle: 新的JSFiddle:

function checkPass(cell, row) {
    var pass = map[row][cell].letter;
    console.log(pass);
    if (pass == "p") {
    alert("Passenger");
      }
    }

You have a 2x2 array 您有一个2x2的阵列

var map = [
        [
            {
                color: "g",
                letter: ""
            },
            {
                color: "g",
                letter: ""
            },
        ],[
            {
                color: "w",
                letter: "p"
            },
            {
                color: "b",
                letter: ""
            }
        ]];
map[0][1] = ...
map[1][1] = ...
map[1][0] = ...
map[1][1] = ...

and you are drawing a 4x1 block 并且您正在绘制一个4x1的块

your map code should look like this 您的地图代码应如下所示

var map = [
        [
            {
                color: "g",
                letter: ""
            },
            {
                color: "g",
                letter: ""
            },
            {
                color: "w",
                letter: "p"
            },
            {
                color: "b",
                letter: ""
            }
        ]];

The problem is with the array, because your grid has 1 row, but your array has 2 "row". 问题出在数组上,因为网格有1行,但是数组有2行。 With this it has been working for me: 有了它,一直为我工作:



    [
                    {
                        color: "g",
                        letter: ""
                    },
                    {
                        color: "g",
                        letter: ""
                    },
                    {
                        color: "w",
                        letter: "p"
                    },
                    {
                        color: "b",
                        letter: ""
                    }
                ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM