Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Another dumb question..  (Read 1126 times)
0 Members and 1 Guest are viewing this topic.
Offline Bassex96

Senior Duke


Medals: 4



« Posted 2013-03-31 03:23:28 »

Hey guys. I can't express my appreciation for your help enough. I'm working on my first ever Java game. I'm using libgdx. I've got the paddle and ball set up and moving around. I know i'm doing some of this stuff incorrectly (ie: using rect.collide to check on collision of ball and paddle) but I plan on redoing all of it. Just getting my foot in the door and seeing something working right now.

I want to encapsulate the ball and the paddle classes with different java class files. I'm a little confused on how to do this with libgdx. I've found some tutorials, but haven't been able to discern this.

What do I need to do to set the class up? i'm not sure which classes I need to import or where to do the actual drawing of the paddle and ball. Do I need a create(), render(), etc. in the Ball class? I'm confused basically, on how to separate the code from the main Game class. I've attached my code so far, below. It's a bit messy, I apologize for that. That is the reason for me asking this question, so I can clean it up a bit.

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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
package com.psillicoder.brickbreaker;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
//import com.badlogic.gdx.graphics.Texture.TextureFilter;
//import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;


public class Game implements ApplicationListener {
   private OrthographicCamera camera;
   private SpriteBatch batch;
   private Texture ballImage;
   private Texture paddleImage;
   
   private int ballSpeedX;
   private int ballSpeedY;
   
   private int paddleSpeed;
   
   //private TextureRegion region;
   
   //rectangles
   Rectangle ballRect;
   private Rectangle paddleRect;
   
   @Override
   public void create() {      
      float w = Gdx.graphics.getWidth();
      float h = Gdx.graphics.getHeight();
   
     
      ballSpeedX = 1;
      ballSpeedY = 1;
     
      paddleSpeed = 2;
     
      paddleImage = new Texture(Gdx.files.internal("data/paddle.PNG"));
      ballImage = new Texture(Gdx.files.internal("data/ball.png"));
      //region = new TextureRegion(paddleImage,0,0,64,16);
     
      camera = new OrthographicCamera(1, h/w);
      batch = new SpriteBatch();
     
      //paddle
      paddleRect = new Rectangle();
      paddleRect.x = 400/2 - 64/2;
      paddleRect.y = 20;
      paddleRect.width = 64;
      paddleRect.height = 16;
     
     
      ballRect = new Rectangle();
      ballRect.x = 400/2;
      ballRect.y = 600/2;
      ballRect.width = 16;
      ballRect.height = 16;
     
     
     
      }
     

   @Override
   public void dispose() {
      batch.dispose();
      //ballImage.dispose();
      paddleImage.dispose();
   }

   @Override
   public void render() {      
      Gdx.gl.glClearColor(1, 1, 1, 1);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      //camera.update();

      CheckKeys();
      moveBall();
     
      //batch.setProjectionMatrix(camera.combined);
      batch.begin();
      batch.draw(ballImage,ballRect.x,ballRect.y);
      batch.draw(paddleImage, paddleRect.x, paddleRect.y);
      batch.end();
   }

   @Override
   public void resize(int width, int height) {
   }

   @Override
   public void pause() {
   }

   @Override
   public void resume() {
   }

   public void moveBall() {
      ballRect.x= ballRect.x + ballSpeedX;
      ballRect.y= ballRect.y + ballSpeedY;
     
      if (ballRect.x >= 400-16){ballSpeedX=-ballSpeedX;}
      if (ballRect.x <= 0){ballSpeedX=-ballSpeedX;}
     
      if (ballRect.y >= 600-16){ballSpeedY=-ballSpeedY;}
      if (ballRect.y <= 0){ballSpeedY=-ballSpeedY;}
     
      if (ballRect.overlaps(paddleRect)){ballSpeedY = -ballSpeedY;}
     
     
      ballRect.x= ballRect.x + ballSpeedX;
      ballRect.y= ballRect.y + ballSpeedY;
     
     
     
   }
   
   public void CheckKeys(){
      boolean isLeftPressed;
      boolean isRightPressed;
           
         if(Gdx.input.isKeyPressed(Keys.DPAD_LEFT))
         {
            isLeftPressed=true;
         }else{isLeftPressed=false;}
         
      if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT))
      {
         isRightPressed=true;
      }else{isRightPressed=false;}
     
      if (isLeftPressed==true && paddleRect.x >=0){paddleRect.x=paddleRect.x-paddleSpeed;}
      if (isRightPressed==true && paddleRect.x <= 400 - 64){paddleRect.x=paddleRect.x+paddleSpeed;}
     
      System.out.println(isLeftPressed);
      System.out.println("Paddle X: " + paddleRect.x + " Paddle Y: " + paddleRect.y );
   }
   
   
   }


Thanks! You guys are awesome!

Hopefully I won't have such elementary problems in the future.
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #1 - Posted 2013-03-31 03:42:48 »

In your separate ball class nothing is truly required From it.so just create a new class file, and maybe create one method called draw that takes a sprite batch as a parameter and then maybe another method called move ball which is what you had in your code above. Then in your main class create a new object for this class and call the two or more methods defined.

Offline Bassex96

Senior Duke


Medals: 4



« Reply #2 - Posted 2013-03-31 03:53:31 »

In your separate ball class nothing is truly required From it.so just create a new class file, and maybe create one method called draw that takes a sprite batch as a parameter and then maybe another method called move ball which is what you had in your code above. Then in your main class create a new object for this class and call the two or more methods defined.

I've tried this, but the issue i'm having is, Spritebatch cannot be resolved to a type..I know this is being called because spritebatch isn't setup in the ball class. There is something I am forgetting. Here is the ball.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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
package com.psillicoder.brickbreaker;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Rectangle;

public class Ball {
   private Texture ballImage;
   private int ballSpeedX;
   private int ballSpeedY;
   private Rectangle ballRect;
   
   
   public Ball(){
   //Load Ball Texture
   ballImage = new Texture(Gdx.files.internal("data/ball.png"));
   
   //Set Ball Speed
   ballSpeedX = 1;
   ballSpeedY = 1;
   
   //Setup initial Rectangle
   ballRect = new Rectangle();
   ballRect.x = 400/2;
   ballRect.y = 600/2;
   ballRect.width = 16;
   ballRect.height = 16;
   
   
   
   }
   
   public void Draw(SpriteBatch spritebatch) {
      spritebatch.draw(ballImage,ballRect.x,ballRect.y);
      return spritebatch;
   }
   
   
   
   
   
   public void moveBall() {
      ballRect.x= ballRect.x + ballSpeedX;
      ballRect.y= ballRect.y + ballSpeedY;
     
      if (ballRect.x >= 400-16){ballSpeedX=-ballSpeedX;}
      if (ballRect.x <= 0){ballSpeedX=-ballSpeedX;}
     
      if (ballRect.y >= 600-16){ballSpeedY=-ballSpeedY;}
      if (ballRect.y <= 0){ballSpeedY=-ballSpeedY;}
     
      if (ballRect.overlaps(paddleRect)){ballSpeedY = -ballSpeedY;}
     
     
      ballRect.x= ballRect.x + ballSpeedX;
      ballRect.y= ballRect.y + ballSpeedY;
     
     
     
   }
   
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Phased
« Reply #3 - Posted 2013-03-31 03:54:41 »

you dont need
1  
return spritebatch;


in your draw method.

You cant return a variable when its a void.
void methods do not return anything, you can have
1  
return;

to break out of the method, but it is useless in your case.
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #4 - Posted 2013-03-31 03:56:47 »

Also as a standard java technique, method names / variables usually start in a lower case letter while class names are capitol. So change Draw to draw

Offline Bassex96

Senior Duke


Medals: 4



« Reply #5 - Posted 2013-03-31 03:58:24 »

Nice!! Thanks. I think I got it figured out. I'm going to toy around with it a bit and encapsulate it better now using getters and setters. Appreciate it to both of you!
Offline Slyntax

Junior Duke


Medals: 3



« Reply #6 - Posted 2013-03-31 04:00:37 »

Call all the rendering in your main class...

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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
public class Game implements ApplicationListener {
   private OrthographicCamera camera;
   private SpriteBatch batch;
   //private Texture ballImage;
   //private Texture paddleImage;

   Paddle paddle;
   Ball ball;

   @Override
   public void create() {    
       float w = Gdx.graphics.getWidth();
       float h = Gdx.graphics.getHeight();
       
       camera = new OrthographicCamera(1, h/w);
       batch = new SpriteBatch();      

       //Initialize your custom classes
       Ball ball = new Ball();
       Paddle paddle = new paddle();

   }  

   @Override
   public void dispose() {
      batch.dispose();
      ball.dispose();
      paddle.dispose();
   }

   public void render(){
       Gdx.gl.glClearColor(1, 1, 1, 1);
       Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

       CheckKeys();
       ball.overlaps(paddle);

       batch.begin()
       //Send the SpriteBatch to the ball and paddle render method
       ball.render(batch);
       paddle.render(batch);
       batch.end();
   }

    public void CheckKeys(){
           
         if(Gdx.input.isKeyPressed(Keys.DPAD_LEFT))
         {
            paddle.isLeftPressed=true;
         }else{paddle.isLeftPressed=false;}
         
      if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT))
      {
         paddle.isRightPressed=true;
      }else{paddle.isRightPressed=false;}
   }


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  
27  
28  
public class Paddle
{
     Texture texture;
     Rectangle paddleRect;
     
     int paddleSpeed;
     
     public boolean isRightPressed = false;
     public boolean isLeftPressed = false;
     
    //You could also create other constructors that allow you to assign a texture, paddle size etc.
     public Paddle(){
        texture = new Texture(...);
        paddleRect.x = 400/2 - 64/2;
        paddleRect.y = 20;
        paddleRect.width = 64;
        paddleRect.height = 16;
    }

    public void render(SpriteBatch batch){

         if (isLeftPressed==true && paddleRect.x >=0){paddleRect.x=paddleRect.x-paddleSpeed;}
         if (isRightPressed==true && paddleRect.x <= 400 - 64){paddleRect.x=paddleRect.x+paddleSpeed;}

        batch.draw(texture, paddleRect.x, paddleRect.y);
    }
    //dispose etc...
}


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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
//import your Paddle class
public class Ball
{
       Texture texture;
       Vector2 ballSpeed;

       Rectangle ballRect;
       
       //Once again you can add different contstructors to set different variables
       public Ball(){
           texture = new Texture(...);
           ballSpeed = new Vector2(1, 1);
           ballRect = new Rectangle();
           ballRect.x = 400/2;
           ballRect.y = 600/2;
           ballRect.width = 16;
           ballRect.height = 16;
       }
       
       public void render(SpriteBatch batch){
            ballRect.x= ballRect.x + ballSpeed.x;
            ballRect.y= ballRect.y + ballSpeed.y;
     
            if (ballRect.x >= 400-16){ballSpeedX=-ballSpeed.x;}
            if (ballRect.x <= 0){ballSpeedX=-ballSpeed.x;}
     
            if (ballRect.y >= 600-16){ballSpeedY=-ballSpeed.y;}
            if (ballRect.y <= 0){ballSpeedY=-ballSpeed.y;}
     
     
            ballRect.x= ballRect.x + ballSpeed.x;
            ballRect.y= ballRect.y + ballSpeed.y;

            batch.draw(texture,ballRect.x,ballRect.y);
       }

       //I made this a boolean but you can make it void if you don't want it to return anything
       public boolean overlaps(Paddle paddle){
            if (ballRect.overlaps(paddle.paddleRect)){ballSpeed.y = -ballSpeed.y; return true; }
            return false;
       }
       //dispose etc...
}


That's roughly what I would do.

Note: I changed a few variable types but forgot to change it in the original code I got from you but you get the idea.
Offline Bassex96

Senior Duke


Medals: 4



« Reply #7 - Posted 2013-03-31 04:42:35 »

1  
2  
3  
4  
5  
     
  public boolean overlaps(Paddle paddle){
            if (ballRect.overlaps(paddle.paddleRect)){ballSpeed.y = -ballSpeed.y; return true; }
            return false;
       }


I originally did this, but paddleRect was private in the paddle class so it wasn't visible. I tried to make a getter for the ballRect in the paddle class, but quickly found out I couldn't do it..or I wasn't doing it right. This is my ball class now, its working pretty great.

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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
package com.psillicoder.brickbreaker;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Ball {
   private Texture ballImage;
   private int ballSpeedX;
   private int ballSpeedY;
   private Rectangle ballRect;
   
   
   public Ball(){
   //Load Ball Texture
   ballImage = new Texture(Gdx.files.internal("data/ball.png"));
   
   //Set Ball Speed
   ballSpeedX = 1;
   ballSpeedY = 1;
   
   //Setup initial Rectangle
   ballRect = new Rectangle();
   ballRect.x = 400/2;
   ballRect.y = 600/2;
   ballRect.width = 16;
   ballRect.height = 16;
   
   
   
   }
   
   public void draw(SpriteBatch spritebatch) {
      spritebatch.draw(ballImage,ballRect.x,ballRect.y);
   
   }

   public void checkPaddleCollision(Paddle paddle)
   {
      if (ballRect.overlaps(paddle.paddleRect))
      {
         ballSpeedY = -ballSpeedY;
      }
   
   }
   
   
   
   
   public void moveBall() {
      ballRect.x= ballRect.x + ballSpeedX;
      ballRect.y= ballRect.y + ballSpeedY;
     
      if (ballRect.x >= 400-16){ballSpeedX=-ballSpeedX;}
      if (ballRect.x <= 0){ballSpeedX=-ballSpeedX;}
     
      if (ballRect.y >= 600-16){ballSpeedY=-ballSpeedY;}
      if (ballRect.y <= 0){ballSpeedY=-ballSpeedY;}
     
      //if (ballRect.overlaps(paddleRect)){ballSpeedY = -ballSpeedY;}
     
     
      ballRect.x= ballRect.x + ballSpeedX;
      ballRect.y= ballRect.y + ballSpeedY;
     
     
     
   }
   
}
Offline Bassex96

Senior Duke


Medals: 4



« Reply #8 - Posted 2013-03-31 05:25:07 »

I can't seem to get my brick class working now either..It's throwing a null pointer exception. I've set it to load 10 bricks..very basic.

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  
27  
28  
29  
30  
31  
32  
33  
package com.psillicoder.brickbreaker;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
public class Brick {
   
   private Texture brickImage;
   
   private Rectangle brickRect;
   
   //private Vector2 brickLoc;
   
   //private int brickType;
   
   public Brick(int type, float x, float y) {
      brickImage = new Texture(Gdx.files.internal("data/bluebrick.PNG"));
      //brickType = type;
      brickRect.x = x;
      brickRect.y = y;
      brickRect.width = 32;
      brickRect.height = 16;
     
   }

public void draw(SpriteBatch spritebatch)
{
   spritebatch.draw(brickImage,brickRect.x,brickRect.y);
}

}



and also in my Game.java:

1  
2  
3  
4  
5  
6  
7  
public void loadBricks() {
      for(int i= 0; i < 10;i++) {
         bricks[i] = new Brick(0,i*32,i*16);
         bricks[i].draw(batch);
      }
     
   }


is called from create()
Offline Slyntax

Junior Duke


Medals: 3



« Reply #9 - Posted 2013-03-31 05:57:53 »

Do you ever initialize the Brick array?
1  
bricks = new Brick[10];
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bassex96

Senior Duke


Medals: 4



« Reply #10 - Posted 2013-03-31 06:09:32 »

I figured it out. I wasn't sure because I won't always know how many bricks need to be added. I'm going to make different levels for it. I'm thinking I may have to go with an arraylist. Gotta read up on them a bit.
Offline delt0r

JGO Knight


Medals: 27
Exp: 18 years


Computers can do that?


« Reply #11 - Posted 2013-03-31 10:39:53 »

Just a FYI: There are no dumb questions but there are dumb topic/subject titles. Ask the question, that way people who can/want to help will read your post. Things like "help please" "I have a question" are really bad since most people will not read the thread to see what you asked. Many forums will delete such topics.

But we are nicer around here.   persecutioncomplex

I have no special talents. I am only passionately curious.--Albert Einstein
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.

Longarmx (36 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (27 views)
2014-10-15 16:18:58

TehJavaDev (52 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (77 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!