在JQuery中从数组中检索“id”

我正在做一个小游戏,我在arrays中有几个障碍:

obstacles = [ { id: '#car' }, { id: '#house' }, { id: '#door' }, ]; 

在我的代码的第二部分,我有:

JQuery的:

  $('#alert').hide(); for (index in obstacles) { object = $(obstacles[index]["id"]); obj_left = object.position().left + parseInt(object.css("margin-left")); obj_top = object.position().top + parseInt(object.css("margin-top")); if ((((posX > (obj_left - me.width() / 2)) && (posX  (obj_top - me.height() / 2)) && (posY < (obj_top + object.height() + me.height() / 2))) { // Cannot walk return false; } $('#alert').show(); // display error message } // Can walk again return true && $('#alert').hide(); // hide error again } 

HTML:

 
//...

我测试了它,效果很好。 但是我没有为每个障碍物显示警报,而是想要检索某个障碍物的值(即:如果玩家撞到汽车,输出“你撞了一辆车!”或者如果玩家撞到了房子,则输出“你可能不会进入“)。

希望我能够直截了当地提供相当数量的代码。

编辑:这是我所遵循的教程的链接: http : //blog.oscarliang.net/pokemon-online-game-theme-tutorial/

谢谢!

如果我理解正确,这将做你想要的:

注意 :当然,你需要包含jQuery才能使用它

  
Car
house
door
bank