Jump to content
  • Advertisement
Sign in to follow this  

Javascript object clicking

This topic is 2765 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I have a click function which works out which object was clicked on by checking its alpha channel in the pixel array. In short if the alpha is 0 then it did not click the object.

I use images with isometric perspective for my game.

The problem lies in one problem which im sure all games have the same problem using this style of graphics...

If there is an object behind the alpha channel - the pixel in question is no longer transparent, and so the function concludes that the user clicked object that in front of it.

This is how i done it:

function searchSprites(sprites, x, y) {
var matches = [];
var data = null;

for (i = (sprites.length-1); i > -1; --i) {
if (x > sprites.data[0] && y > sprites.data[1] && x < sprites.data[2] && y < sprites.data[3]) {

var imageData = ctx2.getImageData(x, y, 1, 1); //gets pixel info
if(imageData.data[3] !== 0){ //checks alpha channel transparency
return matches;

My array runs in reverse so the objects in front are the first to be matched.

I'm wondering how in isometric games developers solve this problem so if you click an object behind the alpha space of the object infront - it can still work out what you clicked ??

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!