Switching my collision detection to array lists caused it to stop working
- by Charlton Santana
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..