Hi, java noob here :clue: I’m currently making a tetris like puzzle game that only a pair of single randomly generated gem/blocks.
So far I’ve been able to
-create my various screens
-generate a gem pair & apply a random coloured texture to each individual gem
-move and rotate the gem/block pair about the screen using keyboard inputs
-collision detection so gem/block pairs cannot leave the play area.
-generate a new gem pair as soon as the current gem as fallen and reached the bottom of the screen.
My problem.
As soon as my gem pair reaches the bottom of the screen, it disappears and a new gem pair is generated at the top of the screen. I believe this is because I haven’t stored the data for the gem pair anywhere so it just gets wiped from memory as soon as the method for create a gempair is called again. So to prevent this I thought I should create an array to store my blocks.
static Array<Block> deadBlocks = new Array<Block>();
I have created a method that will add each ge/block pair that has reached the bottom of the screen into the array.
sidenote*I made a design choice early on to have individual gems block1 and block2 instead of having a single object that held both blocks (I haven’t had a chance to really experiment so I’m not sure how you’d do that either).
static void deadBlocks(){
static Block block1 = Block.getBlock1();
static Block block2 = Block.getBlock2();
deadBlocks.add(block1);
deadBlocks.add(block2);
if(!(deadBlocks.size==0)){
for(int i=0; i<deadBlocks.size;i++){
sprite.begin();
block1 = deadBlocks.get(i);
block2 = deadBlocks.get(i++);
sprite.draw(Block.blockTextures(block1, block2), deadBlocks.get(i).position.x, deadBlocks.get(i).position.y, Block.SIZE, Block.SIZE);
sprite.draw(Block.blockTextures(block2, block1), deadBlocks.get(i++).position.x, deadBlocks.get(i++).position.y, Block.SIZE, Block.SIZE);
sprite.end();
System.out.println("deadBlocks i x " + deadBlocks.get(i).position.x + ", y " + deadBlocks.get(i).position.y);
System.out.println("deadBlocks i++ x " + deadBlocks.get(i++).position.x + ", y " + deadBlocks.get(i++).position.y);
In the above code I have my array deadBlocks. I have used a separate class to create the blocks but for clarity I thought it would be best to remove those references in the code. (Block.SIZE should be just SIZE in this case).
I copied over my draw method from my gem/block pair creation method into the deadblocks method. I made adjusts to the position x and y so that pulls this data from the array. I used system.out so I could see if the correct information was being stored, it is, I’ve also checked using the debugger tool.
There seems to be a problem with null values so I enclosed my method call in my renderer class with an if statement that makes sure that the method is not called until the array is no longer empty.
The game runs fine until the first gem pair hits the bottom of the screen.
My system.out.prints the following (I realized I’m
GROUND COLLISION
deadBlocks i x 240.0, y 59.88349
deadBlocks i++ x 240.0, y -0.11639786
..
...
....
Exception in thread "LWJGL Application" java.lang.IndexOutOfBoundsException: index can't be >= size: 4 >= 4
at com.badlogic.gdx.utils.Array.get(Array.java:127)
at com.mygame.blockgame.GroundedBlocks.groundedBlocks(DeadBlocks.java:37)
at com.mygame.blockgame.BlockControl.blockWallCollision(BlockControl.java:158)
at com.mygame.blockgame.BlockControl.update(BlockControl.java:225)
at com.mygame.blockgame.GameScreen.render(GameScreen.java:46)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.mygame.blockgame.BlockGame.render(BlockGame.java:69)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Line 37 is the systemoutprint ln with i++ in the deadblocks method
Line 158 is the method call to my deadblocks method
Line 225 is the method call to my collision detection method
Line 46 is the movement update call in my render method of the game screen class
I’m guessing there is something wrong with how I’ve implemented the sprite draw method. I don’t understand the error about index can’t be greater or equal to 4. The game does crash when the 2nd block pair reaches the bottom, so I’m guessing because each block pair has two blocks thats where the 4 comes from, but I still do not know why it crashes at that point.
Apologies for the long post.
Many thanks