Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: box2dlight + libgdx -> cant get it to work (SOLVED) on: 2013-08-22 14:59:52
I agree. Documenting your mistakes and fixes can be hugely beneficial to other users, especially when they get snatched up by the Google indexer. They are also great to turn into blog posts if you are into that sort of thing!
2  Game Development / Newbie & Debugging Questions / Re: [LibGDX][Box2D] I'm having an issue with my ContactListener not setting a screen on: 2013-08-20 23:19:01
Wow, thanks! This did it and it works great now.
3  Game Development / Newbie & Debugging Questions / [Solved] I'm having an issue with my ContactListener not setting a screen on: 2013-08-19 23:49:21
Hello everyone,

First post here, so I'd like to extend my appreciation to the community. I've been lurking a bit and you guys are very helpful!

My problem is with setting a ContactListener on a Border class I've created that registers when something has hit it. After three collisions, I would like it to go to a Game Over screen. Here are the important bits of my code. I hope someone can show me what I'm missing, so I can patch one more hole in the mystery of the LibGDX and Box2D combo. Of course, other methods of implementation are welcome as well. Wink

DatGame.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  
public class DatGame extends Game {
   
   World world;
   OrthographicCamera camera;
   Border border;
   
   Box2DDebugRenderer renderer;
   
   Body body;
   
   GameOver gameOver;
   
   @Override
   public void create() {      
     
      gameOver = new GameOver(this);
     
      world = new World(new Vector2(0, -9.81f), true);
      camera = new OrthographicCamera(28, 20);
      border = new Border(world, camera.viewportWidth, camera.viewportHeight, this);
     
      renderer = new Box2DDebugRenderer();
     
      PolygonShape square = new PolygonShape();
      square.setAsBox(2, 2);
     
      BodyDef bodyDef = new BodyDef();
      bodyDef.type = BodyType.DynamicBody;
      bodyDef.position.set(new Vector2(0,0));
     
      FixtureDef fixtureDef = new FixtureDef();
      fixtureDef.shape = square;
      fixtureDef.density = 1;
      fixtureDef.friction = .4f;
      fixtureDef.restitution = .8f;
     
      body = world.createBody(bodyDef);
      Fixture fixture = body.createFixture(fixtureDef);
      fixture.setUserData("Square");
   
      world.setContactListener(border);
     
   }
   
   @Override
   public void render() {      
      super.render();
      Gdx.gl.glClearColor(0, 0, 0, 1);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
     
      body.applyAngularImpulse(100, true);

      world.step(1/60f, 6, 3);
     
      renderer.render(world, camera.combined);
   }
}

Border.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  
public class Border implements ContactListener {
   
   BodyDef bodyDef;
   FixtureDef fixtureDef;
   PolygonShape plank;
   
   DatGame game;

   Sound ding;
   float tones[] = {1f, 12*(float)(Math.log10(.84)/Math.log10(2)) - 1, 12*(float)(Math.log10(.667)/Math.log10(2)) - 1, 12*(float)(Math.log10(.561)/Math.log10(2)) - 1, 2f};
   int lives = 3;
   
   public Border(World world, float borderWidth, float borderHeight, DatGame game) {
     
      this.game = game;
     
      // top
     createPlank(world, borderWidth, 0, 0, borderHeight / 2);
      // bottom
     createPlank(world, borderWidth, 0, 0, -borderHeight / 2);
      // left
     createPlank(world, 0, borderHeight, -borderWidth / 2, 0);
      // right
     createPlank(world, 0, borderHeight, borderWidth / 2, 0);
   }
   
   public void createPlank(World world, float plankWidth, float plankHeight, float borderWidth, float borderHeight) {
     
      ding = Gdx.audio.newSound(Gdx.files.internal("data/ding.wav"));
     
      plank = new PolygonShape();
      plank.setAsBox(plankWidth, plankHeight);
     
      bodyDef = new BodyDef();
      bodyDef.type = BodyType.StaticBody;
      bodyDef.position.set(borderWidth, borderHeight);
     
      fixtureDef = new FixtureDef();
      fixtureDef.shape = plank;
     
      world.createBody(bodyDef).createFixture(fixtureDef).setUserData("Border");
   }

   @Override
   public void beginContact(Contact contact) {
      Fixture fixtureB = contact.getFixtureB();
      if (fixtureB.getUserData().equals("Square")) {
         ding.setPitch(ding.play(), tones[MathUtils.random(0,4)]);
         lives--;
      }
      if (lives == 0) {
         Gdx.app.log("Border", "Game Over!");
         game.setScreen(game.gameOver);
      }
   }
}

GameOver.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  
public class GameOver implements Screen {
   
   DatGame game;
   
   Sprite sprite;
   SpriteBatch batch;
   
   public GameOver (DatGame game) {
      this.game = game;
   }
   
   @Override
   public void show() {
      sprite = new Sprite(new Texture("data/libgdx.png"));
      batch = new SpriteBatch();
   }

   @Override
   public void render(float delta) {
      Gdx.gl.glClearColor(0, 0, 0, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      batch.begin();
      sprite.draw(batch);
      batch.end();
   }
}
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (27 views)
2014-09-07 01:11:22

Longarmx (27 views)
2014-09-07 01:10:19

mitcheeb (35 views)
2014-09-04 23:08:59
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!