Hello i’m making a platformer for practice and have run into a problem. I’m using Tiled for the map and eclipse for code.
CODE:
package com.pumkin.treejumper;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.pumkin.screens.GameScreen;
public class treeJumper extends Game {
public SpriteBatch batch;
public int PPM = 3;
public int Game_W = 100;
public int Game_H = 100;
@Override
public void create () {
batch = new SpriteBatch();
setScreen(new GameScreen(this));
}
@Override
public void render () {
super.render();
}
@Override
public void dispose () {
batch.dispose();
}
}
CODE:
package com.pumkin.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
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.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.pumkin.treejumper.treeJumper;
public class GameScreen implements Screen {
private treeJumper game;
private OrthographicCamera cam;
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer Renderer;
private World world;
private Box2DDebugRenderer b2dr;
private Texture img;
public GameScreen(treeJumper game) {
this.game = game;
//Cam and Vport
cam = new OrthographicCamera();
cam.setToOrtho(false, game.Game_W / game.PPM, game.Game_H / game.PPM);
cam.update();
img = new Texture("grass.png");
//Tmx map loading and renderer
mapLoader = new TmxMapLoader();
map = mapLoader.load("TestMap.tmx");
Renderer = new OrthogonalTiledMapRenderer(map, 1 / game.PPM);
//Box2d
world = new World(new Vector2(0, 0), true);
b2dr = new Box2DDebugRenderer();
BodyDef bdef = new BodyDef();
PolygonShape shape = new PolygonShape();
FixtureDef fdef = new FixtureDef();
Body body;
for(MapObject object : map.getLayers().get(0).getObjects().getByType(RectangleMapObject.class)) {
Rectangle rect = ((RectangleMapObject) object).getRectangle();
bdef.type = BodyType.StaticBody;
bdef.position.set((rect.getX() + rect.getWidth() / 2) / game.PPM, (rect.getY() + rect.getHeight() / 2) / game.PPM);
body = world.createBody(bdef);
shape.setAsBox((rect.getWidth() / 2) / game.PPM, (rect.getHeight() / 2) / game.PPM);
fdef.shape = shape;
body.createFixture(fdef);
}
}
@Override
public void show() {
// TODO Auto-generated method stub
}
public void update(float dt) {
cam.update();
world.step(1/60f, 3, 3);
if(Gdx.input.isKeyPressed(Keys.A)) cam.position.x += -1;
if(Gdx.input.isKeyPressed(Keys.W)) cam.position.y += 1;
if(Gdx.input.isKeyPressed(Keys.D)) cam.position.x += 1;
if(Gdx.input.isKeyPressed(Keys.S)) cam.position.y += -1;
}
@Override
public void render(float dt) {
update(dt);
Gdx.gl.glClearColor(0.5f, 0.4f, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Renderer.render();
Renderer.setView(cam);
game.batch.setProjectionMatrix(cam.combined);
game.batch.begin();
game.batch.draw(img, 0, 0);
game.batch.end();
b2dr.render(world, cam.combined);
System.out.println(Gdx.graphics.getFramesPerSecond());
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
}
Output:
(Hope it worked but if it did not it just shows Box2d debug lines)
I’m new to java/coding so all help in nice and Thank you.