Switching my collision detection to array lists caused it to stop working
Posted
by
Charlton Santana
on Game Development
See other posts from Game Development
or by Charlton Santana
Published on 2012-04-11T22:08:19Z
Indexed on
2012/04/11
23:46 UTC
Read the original article
Hit count: 343
I have made a collision detection system which worked when I did not use array list and block generation. It is weird why it's not working but here's the code, and if anyone could help I would be very grateful :)
The first code if the block generation.
private static final List<Block> BLOCKS = new ArrayList<Block>();
Random rnd = new Random(System.currentTimeMillis());
int randomx = 400;
int randomy = 400;
int blocknum = 100;
String Title = "blocktitle" + blocknum;
private Block block;
public void generateBlocks(){
if(blocknum > 0){
int offset = rnd.nextInt(250) + 100; //500 is the maximum offset, this is a constant
randomx += offset;//ofset will be between 100 and 400
int randomyoff = rnd.nextInt(80); //500 is the maximum offset, this is a constant
randomy = platformheighttwo - 6 - randomyoff;//ofset will be between 100 and 400
block = new Block(BitmapFactory.decodeResource(getResources(), R.drawable.block2), randomx, randomy);
BLOCKS.add(block);
blocknum -= 1;
}
The second is where the collision detection takes place note: the block.draw(canvas);
works perfectly. It's the blocks that don't work.
for(Block block : BLOCKS) {
block.draw(canvas);
if (sprite.bottomrx < block.bottomrx && sprite.bottomrx > block.bottomlx && sprite.bottomry < block.bottommy && sprite.bottomry > block.topry ){
Log.d(TAG, "Collided!!!!!!!!!!!!1");
}
// bottom left touching block?
if (sprite.bottomlx < block.bottomrx && sprite.bottomlx > block.bottomlx && sprite.bottomly < block.bottommy && sprite.bottomly > block.topry ){
Log.d(TAG, "Collided!!!!!!!!!!!!1");
}
// top right touching block?
if (sprite.toprx < block.bottomrx && sprite.toprx > block.bottomlx && sprite.topry < block.bottommy && sprite.topry > block.topry ){
Log.d(TAG, "Collided!!!!!!!!!!!!1");
}
//top left touching block?
if (sprite.toprx < block.bottomrx && sprite.toprx > block.bottomlx && sprite.topry < block.bottommy && sprite.topry > block.topry ){
Log.d(TAG, "Collided!!!!!!!!!!!!1");
}
}
The values eg bottomrx
are in the block.java file..
© Game Development or respective owner