У меня есть класс PlayScreen, который обрабатывает все элементы, которые будут отображаться на экране для моей игры.
'''
public class PlayScreen implements Screen {
private MyGame game;
private TextureAtlas atlas;
private OrthographicCamera gamecam;
private Viewport gamePort;
//Tiled map variables
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private Miner player;
//Box2d variable
private World world;
private Box2DDebugRenderer b2dr;
MyController controller;
public PlayScreen(MyGame game)
{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
atlas = new TextureAtlas("Mario_and_Enemies.pack");
this.game = game;
// create cam to follow character through map
gamecam = new OrthographicCamera();
// create a FitViewport to maintain virtual aspect ration despite device screen size
gamePort = new FitViewport(MyGame.V_WIDTH / MyGame.PPM, MyGame.V_HEIGHT / MyGame.PPM,gamecam);
mapLoader = new TmxMapLoader();
map = mapLoader.load("map3.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / MyGame.PPM);
gamecam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight()/2 , 0);
world = new World(new Vector2(0,-10 ), true);
b2dr = new Box2DDebugRenderer();
player = new Miner(world, this);
new B2DWorldCreator(world, map);
controller = new MyController();
}
public TextureAtlas getAtlas()
{
return atlas;
}
@Override
public void show()
{
}
public void handleInput(float dt) {
Gdx.app.debug("DEBUG", "handleInput");
if(controller.isUpPressed())
{
Gdx.app.debug("DEBUG", "upVelocity");
player.b2body.setLinearVelocity(new Vector2(0,4f));
}
if(controller.isRightPressed() && player.b2body.getLinearVelocity().x <= 2)
{
Gdx.app.debug("DEBUG", "rightVelocity");
player.b2body.setLinearVelocity(new Vector2(0.1f, 0));
}
if(controller.isLeftPressed() && player.b2body.getLinearVelocity().x <= -2)
{
Gdx.app.debug("DEBUG", "leftVelocity");
player.b2body.setLinearVelocity(new Vector2(-0.1f, 0));
}
if(controller.isDownPressed())
{
Gdx.app.debug("DEBUG", "downVelocity");
player.b2body.setLinearVelocity(new Vector2(0,-4f));
}
}
public void update(float dt)
{
player.update(dt);
handleInput(dt);
renderer.setView(gamecam);
world.step(1/60f, 6,2);
gamecam.position.x = player.b2body.getPosition().x;
gamecam.update();
}
@Override
public void render(float delta)
{
update(delta);
update(Gdx.graphics.getDeltaTime());
if(Gdx.input.isTouched())
{
handleInput(delta);
}
// Clear the game screen with black
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// render our game map
renderer.render();
b2dr.render(world, gamecam.combined);
controller.draw();
game.batch.setProjectionMatrix(gamecam.combined);
game.batch.begin();
player.draw(game.batch);
game.batch.end();
}
@Override
public void resize(int width, int height)
{
gamePort.update(width,height);
controller.resize(width,height);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose()
{
map.dispose();
renderer.dispose();
world.dispose();
b2dr.dispose();
}
}
'''
Для контроллера у меня есть мой класс MyController, который отображает экранный dpad и имеет прослушиватели, когда они нажимаются.
public class MyController {
Viewport viewport;
Stage stage;
boolean upPressed, downPressed, leftPressed, rightPressed;
OrthographicCamera cam;
public MyController()
{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
cam = new OrthographicCamera();
viewport = new FitViewport (800, 480, cam);
stage = new Stage(viewport, MyGame.batch);
Gdx.input.setInputProcessor(stage);
stage.addListener(new InputListener() {
@Override
public boolean keyDown(InputEvent event, int keycode) {
switch(keycode){
case Input.Keys.UP:
upPressed = true;
break;
case Input.Keys.DOWN:
downPressed = true;
break;
case Input.Keys.LEFT:
leftPressed = true;
break;
case Input.Keys.RIGHT:
rightPressed = true;
break;
}
return true;
}
@Override
public boolean keyUp(InputEvent event, int keycode) {
switch(keycode){
case Input.Keys.UP:
upPressed = false;
break;
case Input.Keys.DOWN:
downPressed = false;
break;
case Input.Keys.LEFT:
leftPressed = false;
break;
case Input.Keys.RIGHT:
rightPressed = false;
break;
}
return true;
}
});
Table table = new Table();
table.left().bottom();
final ImageButton upImg = new ImageButton(new TextureRegionDrawable(new Texture(Gdx.files.internal("up.png"))));
upImg.setSize(50,50);
stage.addActor(upImg);
upImg.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.debug("DEBUG", "upClicked");
upPressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
//upPressed = false;
}
});
ImageButton rightImg = new ImageButton(new TextureRegionDrawable(new Texture(Gdx.files.internal("right.png"))));
rightImg.setSize(50,50);
stage.addActor(rightImg);
rightImg.addListener(new InputListener()
{
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
//return super.touchDown(event, x, y, pointer, button);
Gdx.app.debug("DEBUG", "rightClicked");
rightPressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
//super.touchUp(event, x, y, pointer, button);
//rightPressed = false;
}
});
ImageButton downImg = new ImageButton(new TextureRegionDrawable(new Texture(Gdx.files.internal("down.png"))));
downImg.setSize(50,50);
stage.addActor(downImg);
downImg.addListener(new InputListener()
{
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
//return super.touchDown(event, x, y, pointer, button);
Gdx.app.debug("DEBUG", "downClicked");
downPressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
//super.touchUp(event, x, y, pointer, button);
//downPressed = false;
}
});
ImageButton leftImg = new ImageButton(new TextureRegionDrawable(new Texture(Gdx.files.internal("left.png"))));
leftImg.setSize(50,50);
stage.addActor(leftImg);
leftImg.addListener(new InputListener()
{
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
//return super.touchDown(event, x, y, pointer, button);
Gdx.app.debug("DEBUG", "leftClicked");
leftPressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
//super.touchUp(event, x, y, pointer, button);
//leftPressed = false;
}
});
table.add();
table.add(upImg).size(upImg.getWidth(), upImg.getHeight());
table.add();
table.row().pad(5,5,5,5);
table.add(leftImg).size(leftImg.getWidth(), leftImg.getHeight());
table.add();
table.add(rightImg).size(rightImg.getWidth(), rightImg.getHeight());
table.add();
table.row().padBottom(5);
table.add();
table.add(downImg).size(downImg.getWidth(), downImg.getHeight());
table.add();
table.pack();
stage.addActor(table);
}
public void draw()
{
stage.draw();
stage.act();
}
public boolean isUpPressed() {
return upPressed;
}
public boolean isDownPressed() {
return downPressed;
}
public boolean isLeftPressed() {
return leftPressed;
}
public boolean isRightPressed() {
return rightPressed;
}
public void resize(int width, int height)
{
viewport.update(width, height);
}
}
У меня есть журналы отладки, которые говорят мне, что Кнопки на самом деле работают, а вход ручки зациклен. Однако мой персонаж не двигается вообще. Я пробовал несколько версий метода handleInput, но ничего не меняется. Я не уверен, как это исправить.
Это ссылка на мой репозиторий github https://github.com/jaketlee07/minerGame.git