jquery - Retrieve certain object from an array and act upon retrieval -
i hadn't received concrete answer previous thread: retrieve "id" array in jquery
so i'll try explain in more detail , show did.
i'm working on small game , have several obstacles in array so:
obstacles = [ { id: '#car' }, { id: '#house' }, { id: '#door' }, ];
in second part of code, have:
jquery:
$('#alert').hide(); (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_left + object.width() + me.width() / 2)))) && (posy > (obj_top - me.height() / 2)) && (posy < (obj_top + object.height() + me.height() / 2))) { // cannot walk return false; } // it's starts ugly here. if (object == '#car') { $('#character').hide(); } if (object == '#door') { $('#noenter-alert').show(); } } // can walk again return true && $('#character[).show(), $('#noenter-alert').hide(); }
html:
<div id="alert">//...</div> <div id="character"></div> <div class="door" id="door"></div> <div class="house" id="house"></div> <div class="car" id="car"></div>
i tested out , worked great. instead of displaying alert every obstacle, want retrieve value of obstacle (i.e: if player hits car, hide player or if player hits door output "you may not enter!").
hope i've been straightforward , have provided fair amount of code.
and here's link tutorial followed: http://blog.oscarliang.net/pokemon-online-game-theme-tutorial/
thanks! apologize repost hope can fix mess.
alert should in if condition
if ((((posx > (obj_left - me.width() / 2)) && (posx < (obj_left + object.width() + me.width() / 2)))) && (posy > (obj_top - me.height() / 2)) && (posy < (obj_top + object.height() + me.height() / 2))) { //here need put alert alert(object+'is here') return false; }
Comments
Post a Comment