Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a object selection function by checking the mouse click and getting the relevant object.

How ever there is a rare situation where if one object is partially behind the other then both objects are in the given area so im wondering how I can make the game know which one was selected, as currently my method does not know.

This is my function that works it out:

function getobj(e){
    mx = e.pageX - curleft; //mouse click x
    my = e.pageY - curtop; //mouse click y

    function searchSprites(sprites, x, y) {
        var matches = [],
        i = 0,
        data = null;
        for (i = 0; i < spritea.length; ++i) {
            data = spritea[i].data;
            if (x > data[0] && y > data[1] && x < data[2] && y < data[3]) {     
                var imageData = ctx2.getImageData(x, y, 1, 1);
                if(imageData.data[3] !== 0){
                    return [spritea[i].id];
                    i = spritea.length;     
                }
            }
        }
    }
    res = searchSprites(spritea, mx, my);
    bid = res[0];
    if(bid === '1'){
        alert('You selected the skyscraper in front!');
    }else if(bid === '3'){
        alert('You selected the skyscraper behind!');
    }
}

Image of the map:

enter image description here

It keeps telling me I clicked the skyscraper behind which is not necessarily what the user is trying to do. How can I improve the accuracy of this ?

share|improve this question
    
Get that searchSprites function out there, you don't want to allocate if everytime you call getobj (which should be name getObject, naming conventions are you friend!) –  Thomas R. Koll Mar 21 '12 at 6:49
    
Thanks - i have done it was only there for testing purposes. –  Dave Mar 22 '12 at 1:15

1 Answer 1

up vote 6 down vote accepted

bid = res[0]; Seems like that's your issue. Instead of just selecting the first one, you'll want to sort through the found sprites and get the one that's closest to the camera. Also, not sure if it's a copy and paste error or what but:

if(imageData.data[3] !== 0){
    return [spritea[i].id];
    i = spritea.length;     
}

i = spritea.length; won't be reached.

EDIT

OK, I looked into this more and I can't find if getImageData can be changed to get multiple layers of images. However, the below function will return the ids of all the images that overlap the point the mouse is at. I don't know much about your code, but I'm assuming you have some way of getting the sprite ID from your sprite array, I'm just assuming it's spritea[i].id. You'll have to figure that one out on your own I'm afraid. Also keep in mind that I don't do javascript, so I'm just assuming this code works, don't expect to copy/paste it and have it work though.

function searchSprites(sprites, x, y) {
    var matches = [],
        i = 0,
        data = null;
    for (i = 0; i < sprites.length; ++i) {
        data = spritea[i].data;
        //Assuming data is an array that contains the corners of the sprite
        //data[0] = x_Minus_Side
        //data[1] = y_Minus_Side
        //data[2] = x_Plus_Side
        //data[3] = y_Plus_Side

        if (x > data[0] && y > data[1] && x < data[2] && y < data[3]) {     
            matches.push(spritea[i].id)
        }
    }
    return matches
}

Once you have that list, you can sort it based on depth, or the one closest to the bottom of the screen wins, or whatever you want.

share|improve this answer
    
So how would i return a list and also work out which is closest =/ –  Dave Mar 20 '12 at 21:33
    
@Dave How do you handle which objects are drawn on top of others? Could that be done in reverse? –  K.G. Mar 20 '12 at 22:01
    
They are drawn as i loop the tiles of the map.. if i do it in reverse, the objects that should be behind other objects would also be swapped around. –  Dave Mar 20 '12 at 22:03
    
Don't actually draw them in reverse, but loop through them in reverse when doing the selecting. –  jhocking Mar 21 '12 at 0:38
    
Its working although i have come across a new problem :( –  Dave Mar 21 '12 at 2:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.