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.

Hi I am developing a game in android just like Farmville by Zinga. In that game we have to place elements in the diamond shaped field so the don't overlap each other. Now I did coding for placing the field inside the farm field but I cannot stop the problem of overlapping of the farm field. I Am attaching the code that I have down for all this someone please help me....

try{
    if(bm1.getPixel((int)initX,(int)initY)!=0){
        if(bm1.getPixel((int)initX,(int)initY+20)!=0){
            if(bm1.getPixel((int)initX-20,(int)initY)!=0){
                if(bm1.getPixel((int)initX+20,(int)initY)!=0){
                    if(bm1.getPixel((int)initX,(int)initY-20)!=0){
                        c.drawBitmap(bm,initX-30,initY-20, paint);
                    }
                }
            }
        }
    }
}catch(Exception e) {
    Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT);
}
share|improve this question
4  
All those if statements look horrible. –  The Communist Duck May 28 '11 at 12:35

1 Answer 1

I wonder why you're choosing a pixel based collision detection here? A game like Farmville works with a tile-grid. I assume you have such a data-structure in your game that holds all the tiles? Why not simply query this data-structure and look up if a tile is already occupied? If yes, snap the new tile to an adjacent field, or simply prevent the user from dropping a new element there.

Update: Have a look at the following screenshot:

farmville grid

I drew some lines (white) over it to show you the tile grid. The smallest grid size is probably the size of the small pink rhombus. Of course you can also use bigger tiles, like the 2x2 tile (also colored pink). The Farm-House is a 4x3 tile.

When you place a new element on that grid, you can check if any other tile is already at that position. For a 2x2 tile, you'll only have to check 4 grid cells...

It is of course up to you how coarse your grid should be.

share|improve this answer
    
please can u guide me i am in android game for the first time –  Arun Apr 28 '11 at 9:09
    
Another main reason for not using the tiled grid is because if i use tile i can use only that fixed place for the placing of the farm field.Which will not give the look and feel of a good farm game. –  Arun Apr 28 '11 at 9:19
    
i dont know much about tiled grid if u can help me in this it would be very kind of u... –  Arun Apr 28 '11 at 9:21
    
@Arun I updated my answer with some more information –  bummzack Apr 28 '11 at 9:57
    
can u help me i dont have any experience –  Arun May 3 '11 at 5:23

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.