这个数组怎么了...? [英] What's wrong with this array...?

查看:71
本文介绍了这个数组怎么了...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个游戏,其中用户在网格上四处走动,不得不收拾乘客.除了需要检查玩家是否在乘客上方移动之外,我几乎已完成所有操作,如果需要,请显示警报框.这是我的JSFiddle: http://jsfiddle.net/nZ8vA/9/和上一个:<一个href ="http://jsfiddle.net/nZ8vA/7/" rel ="nofollow"> http://jsfiddle.net/nZ8vA/7/,其中包含我之前拥有的数组.如您所见,我有一个称为map的数组,该数组用于构造网格.

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. As you can see I have an array called map which is what is used to construct the grid.

在先前的JSFiddle中,我仍试图使其能够检测到用户何时收拾乘客(地图中的字母"P").在最近的JSFiddle中,由于这里的先前帮助,我创建了一个二维对象数组.该地图尚未完全构建,看起来应该像第一个JSFiddle一样,但是似乎将整条线读为一种颜色.如果打开控制台并移动整个顶行,则不应将其读取为绿色.我还认为这是按颜色读取乘客的,这不是我想要的,因为乘客位于不同的颜色方块上.

In the previous JSFiddle I was still attempting to make it detect when the user collects a passenger (the letter "P" in the map). In my most recent JSFiddle I have created a 2d array of objects thanks to previous help on here. 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. 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.

有人可以帮助我检测用户何时收乘乘客吗?我有一个功能可以检测带有"P"的正方形,但是我需要保持相同的网格颜色布局.我希望这是有道理的.

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. I hope this makes sense.

这是功能:

旧的JSFiddle:

Old JSFiddle:

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

新的JSFiddle:

New JSFiddle:

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

推荐答案

问题出在数组上,因为网格有1行,但是数组有2行.有了它,它对我一直有效:

The problem is with the array, because your grid has 1 row, but your array has 2 "row". With this it has been working for me:



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

这篇关于这个数组怎么了...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆