Hello All
My code is based off of this Website (Scroll down for video):
http://programmersweb.blogspot.ch/2012/ ... -ball.html
This code works no problem, i can see the ball bouncing off the ground. I have one single "Game" class
My next Step was to take the code out of my "Game" class and divide it into a "Game" class and a "Screen" class.
The code was changed slightly because there was an issue with the way World world was initialized but otherwise it is the same code.
Running the code like this i get just a Black screen which doesnt show the ball or the ground. I get no errors.
Why do the ball and the ground not show up correctly when i run the code from a "Screen" as opposed to a "Game"?
Hope someone can help! Thanks
My code is based off of this Website (Scroll down for video):
http://programmersweb.blogspot.ch/2012/ ... -ball.html
This code works no problem, i can see the ball bouncing off the ground. I have one single "Game" class
My next Step was to take the code out of my "Game" class and divide it into a "Game" class and a "Screen" class.
The code was changed slightly because there was an issue with the way World world was initialized but otherwise it is the same code.
Code: Select all
package com.me.blast.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
public class GameScreen implements Screen{
World world;
Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;
static final float BOX_STEP=1/60f;
static final int BOX_VELOCITY_ITERATIONS=6;
static final int BOX_POSITION_ITERATIONS=2;
static final float WORLD_TO_BOX=0.01f;
static final float BOX_WORLD_TO=100f;
public GameScreen(){
world = new World(new Vector2(0, -100), true);
camera = new OrthographicCamera();
camera.viewportHeight = 320;
camera.viewportWidth = 480;
camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);
camera.update();
//Ground body
BodyDef groundBodyDef =new BodyDef();
groundBodyDef.position.set(new Vector2(0, 10));
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundBox = new PolygonShape();
groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f);
groundBody.createFixture(groundBox, 0.0f);
//Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2);
Body body = world.createBody(bodyDef);
CircleShape dynamicCircle = new CircleShape();
dynamicCircle.setRadius(500f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = dynamicCircle;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 1;
body.createFixture(fixtureDef);
debugRenderer = new Box2DDebugRenderer();
}
@Override
public void render(float delta) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);
}
//Screen Methods omitted
}
Code: Select all
public class Blast extends Game{
@Override
public void create() {
setScreen(new GameScreen());
}
Running the code like this i get just a Black screen which doesnt show the ball or the ground. I get no errors.
Why do the ball and the ground not show up correctly when i run the code from a "Screen" as opposed to a "Game"?
Hope someone can help! Thanks