Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (474)
Games in Android Showcase (105)
games submitted by our members
Games in WIP (524)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Having trouble loading a brick level  (Read 1209 times)
0 Members and 1 Guest are viewing this topic.
Offline Bassex96

Senior Member


Medals: 4



« Posted 2013-04-06 03:36:31 »

Hey guys. Sorry for another noob post. I'm still working on my brickbreaker game, but i'm having a bit of trouble figuring out how to set up levels, basically. Right now, i've got loading a square of bricks.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   public void loadBricks() {
      int i = 0;
     
      for(int j= 0; j < 5;j++) {
         for (int k = 0;k < 5; k++) {
         
         bricks[i] = new Brick(0,j*32+100,k*16 + 400);
         i++;
         
         }
     
      }
     
   }


Very basic as you see and definitely doesn't do what I need it to do. This is in my game.java class.

And i'm drawing them like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
batch.begin();
     
      for (int i = 0; i <bricks.length;i++)
      {
         if (bricks[i].isDestroyed == true) {
            bricks[i].brickRect.x = 0;
            bricks[i].brickRect.y = 0;
         }else{
         
         ball.checkBrickCollision(bricks[i]);
         
         
         bricks[i].draw(batch);
      }}
     
      ball.draw(batch);
      paddle.draw(batch);
      batch.end();
   }


I know I need to use a multidimensional array, but i'm having trouble implementing it. I hope this makes sense, i'm having a little trouble explaining my question..lol

Forgot to add, i'm using LibGDX. I have Game.java, Paddle.java, Ball.java, Brick.java classes.
Offline Bassex96

Senior Member


Medals: 4



« Reply #1 - Posted 2013-04-06 04:15:48 »

This is what i've got so far..I added a Level.java

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
package com.psillicoder.brickbreaker;

public class Level {
   final int BLANK = 0;
   final int BLUE = 1;
   
   int board[][] = new int[12][12];
   
   public Level() {
      int[][] board = new int[][] {
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,}
      };
   }

}


and in my loadlevel method in game.java, I have

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   public void loadBricks() {
      int i = 0;
     
      for(int j= 0; j < 11;j++) {
         for (int k = 0;k < 11; k++) {
         
            if (level.board[j][k] != 0) {
         bricks[i] = new Brick(level.board[j][k],j*32,k*16 + 400);
         i++;
         }
         }
     
      }
     
   }


But i'm getting a nullpointer exception when trying to run bricks.draw(batch); which just draws the brick:

public void draw(SpriteBatch spritebatch)
{
   spritebatch.draw(brickImage,brickRect.x,brickRect.y);
}
Offline Phased
« Reply #2 - Posted 2013-04-06 04:26:09 »

You have empty parts of the array because your only setting what is not 0, when your drawing you will have to check its not null

1  
2  
3  
If (block[x][y]] != null){
//draw
}

Or set the block to a air block which the remeer method doesn't draw anything
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bassex96

Senior Member


Medals: 4



« Reply #3 - Posted 2013-04-06 04:30:01 »

You have empty parts of the array because your only setting what is not 0, when your drawing you will have to check its not null

1  
2  
3  
If (block[x][y]] != null){
//draw
}

Or set the block to a air block which the remeer method doesn't draw anything


I commented that whole line of code out but i'm still getting a nullpointerexception. I've been going through the code and can't figure out why.


[edit] I think I see what your saying..I need to rewrite all of this, it's starting to become unmanagable.
Offline Bassex96

Senior Member


Medals: 4



« Reply #4 - Posted 2013-04-06 05:00:52 »

I'm trying to rewrite it using an Arraylist..I don't think it's going to work though. I've had enough for tonight lol.
Offline cubus

Junior Member


Medals: 2



« Reply #5 - Posted 2013-04-06 05:02:14 »

you have 2 x int[][] board. one is a class member, the other one is a local variable in the constructor.
you only fill the local variable, but then you use the class member, which will only contain 0's.




Offline Bassex96

Senior Member


Medals: 4



« Reply #6 - Posted 2013-04-08 01:07:16 »

So I should declare the actual level information in the game class?
Offline Screem
« Reply #7 - Posted 2013-04-08 02:39:56 »

Change

1  
2  
   public Level() {
      int[][] board = new int[][] { ...

to

1  
2  
   public Level() {
      board = new int[][] { ...


Edit: Basically, in the code you have, you create a whole new board array in your Level constructor, instead of setting the global board array. So the global board array you declared in the Level class outside the constructor was always filled with zeros since you initialized it to a blank array. If you change what I showed you, you'll be accessing the global board variable and setting all its contents to ones.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #8 - Posted 2013-04-08 03:24:55 »

I'm trying to rewrite it using an Arraylist..I don't think it's going to work though. I've had enough for tonight lol.
I wouldn't recommend that, you should keep the integrity of the grid using your 2d array.
Offline Bassex96

Senior Member


Medals: 4



« Reply #9 - Posted 2013-04-09 04:24:19 »

Change

1  
2  
   public Level() {
      int[][] board = new int[][] { ...

to

1  
2  
   public Level() {
      board = new int[][] { ...


Edit: Basically, in the code you have, you create a whole new board array in your Level constructor, instead of setting the global board array. So the global board array you declared in the Level class outside the constructor was always filled with zeros since you initialized it to a blank array. If you change what I showed you, you'll be accessing the global board variable and setting all its contents to ones.


When I do that, I get an error on the board variable saying "Syntax error on token "board", VariableDeclaratorId expected after this token"

I even took a bunch of the variables out and replaced with numbers because I was getting errors. It seems like something is wrong..
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cubus

Junior Member


Medals: 2



« Reply #10 - Posted 2013-04-09 08:52:53 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public class Level {
   int[][] board;
   
   public Level() {
      board = new int[][] {
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,},
            {1,1,1,1,1,1,1,1,1,1,1,1,}
      };
   }
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (26 views)
2014-07-10 20:20:18

CopyableCougar4 (24 views)
2014-07-10 02:26:14

CopyableCougar4 (31 views)
2014-07-09 02:55:38

Code Mage (31 views)
2014-07-08 23:57:00

Code Mage (19 views)
2014-07-08 23:49:08

AppleSauce (27 views)
2014-07-08 19:25:32

CopyableCougar4 (27 views)
2014-07-06 01:51:26

ipe369 (33 views)
2014-07-05 14:18:25

vastrolorde (43 views)
2014-07-04 18:45:44

theagentd (60 views)
2014-07-02 22:19:37
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!