项目:我一直在使用libgdx-box2d进行一个小项目,其中我想创建一个对象,将其渲染到屏幕并对其应用重力以使其与地面。我目前正在处理的部分是创建和渲染正文。
问题:问题是当我运行项目时,我创建的正文没有显示在屏幕上,我觉得我已经正确创建了正文,因为没有编译问题。< / p>
我尝试了什么:
我尝试了很多东西,例如将圆的半径增加到 使它更大(更明显)
通过执行camera = new OrthographicCamera(Gdx.graphics.getWidth() /10, Gdx.graphics.getHeight() /10);
来放大屏幕
太小了,但无论我尝试我创造的对象是什么
无法看到。
将身体的位置更改为不同的x,y位置
我想要的结果:我想要的是可能得到一个答案,为什么我创建的身体没有被显示,并且有一些帮助来渲染它以便可以看到它。我希望我提出的问题的结构清晰且具有信息性。提前谢谢你们提供任何帮助
这是我的相关代码:
package com.mohamed.JungleFighter;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
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.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.sun.xml.internal.ws.wsdl.writer.document.soap.Body;
//i'm extending libgdx's built in game class which implements the activity listener
public class JungleFighterMain extends Game {
private OrthographicCamera camera;
private SpriteBatch sBatch;
private Texture player;
private Texture enemy;
//private SpriteBatch enemyBatch;
private Sprite sprite1;
private Sprite sprite2;
//just setting my game heighty and width
public static int gameWidth = 1280, gameHeight = 720;
private World world;
private Box2DDebugRenderer debugRenderer;
p
@Override
public void create () {
//camera related
camera = new OrthographicCamera(Gdx.graphics.getWidth() /10, Gdx.graphics.getHeight() /10);
//end of camera related
//BOX2D CODE FOR CREATING WWORLD
World world = new World(new Vector2(0, -10), true);
//creating box2d body definition
BodyDef bodyDef = new BodyDef();
//setting body type to dynamic
bodyDef.type = BodyType.DynamicBody;
//position
bodyDef.position.set(0, 0);
//sending what i just made to the world i created
// making a circle with a radius of 6
CircleShape circle = new CircleShape();
circle.setRadius(30f);
//making my fixtures for the circle
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.4f;
//making it bounce a little
fixtureDef.restitution = 0.6f;
//addding fixture attributes to my ball
//Fixture fixture = body.createFixture(fixtureDef);
//end of creating my circle ball
//creating my actual ball in the world
world.createBody(bodyDef).createFixture(fixtureDef);
circle.dispose();
//GROUND START
BodyDef bodyGround = new BodyDef();
bodyGround.type = BodyType.StaticBody;
bodyGround.position.set(-100,-100);
//setting shape of ground
ChainShape groundShape = new ChainShape();
groundShape.createChain(new Vector2[] {new Vector2(-250, 0), new Vector2(250, 0)});
//fixtures for ground
FixtureDef fixtureDefGround = new FixtureDef();
fixtureDefGround.shape = groundShape;
fixtureDefGround.friction = 0.5f;
fixtureDefGround.restitution = 0;
world.createBody(bodyGround).createFixture(fixtureDefGround);
groundShape.dispose();
}
public void dispose() {
world.dispose();
}
public void render (float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// camera related
sBatch.setProjectionMatrix(camera.combined);
debugRenderer.render(world, camera.combined);
world.step(TIMESTEP, VELOCITYITERATIONS, POSITIONITERATIONS);
}
}
public void resize(int width, int height){
}
public void pause(){
}
public void resume(){
}
}
答案 0 :(得分:0)
在Java中,它不足以声明一个对象,就像你拥有:
private Box2DDebugRenderer debugRenderer
您还需要实例化它,否则它是一个空引用。尝试添加
debugRenderer = new Box2DDebugRenderer()
到您的创建功能,以避免您在评论中提到的错误。构造函数也可能需要一些参数,但我不熟悉Box2D或LibGDX。
答案 1 :(得分:0)