0
\$\begingroup\$

I'm making a simple platform game, and I was following this tutorial to make an onScreenController (https://www.youtube.com/watch?v=z4Vqkp_ve3I&t=66s). There's one think that's not how I want it to be, whenever I jump and I keep the jump button pressed the player keeps "glued" to where it hits, it wasn't happening when I was using the handleInput of the console, back then I used the "Gdx.input.isKeyJustPressed(Input.Keys.UP)" and it was working as intended, the player jumped and falled. But now, with this new onScreenController it doesn't. How do I make so the player must press up again so it jumps again and the player doesn't stick on the "roof"?.

Player "glued" to the roof

The controller class:

package com.jeancarlous.codewizard.Scenes;

 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.Input;
 import com.badlogic.gdx.graphics.OrthographicCamera;
 import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.scenes.scene2d.InputEvent;
 import com.badlogic.gdx.scenes.scene2d.InputListener;
 import com.badlogic.gdx.scenes.scene2d.Stage;
 import com.badlogic.gdx.scenes.scene2d.ui.Image;
 import com.badlogic.gdx.scenes.scene2d.ui.Table;
 import com.badlogic.gdx.utils.Disposable;
 import com.badlogic.gdx.utils.viewport.FitViewport;
 import com.badlogic.gdx.utils.viewport.Viewport;
 import com.jeancarlous.codewizard.CodeWizard;

/**
 * Created by Jean on 09/06/2017.
 */

public class Controle implements Disposable{


Viewport viewport;
Stage stage;
boolean upPressed, leftPressed, rightPressed;
OrthographicCamera cam;
boolean x=false;

public Controle(){
    cam = new OrthographicCamera();
    viewport = new FitViewport(800, 480, cam);
    stage = new Stage(viewport, CodeWizard.batch);

    stage.addListener(new InputListener(){

        @Override
        public boolean keyDown(InputEvent event, int keycode){
            switch (keycode){
                case Input.Keys.UP:
                    upPressed = 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.LEFT:
                    leftPressed = false;
                    break;
                case Input.Keys.RIGHT:
                    rightPressed = false;
                    break;
            }
            return true;
        }

    });

    Gdx.input.setInputProcessor(stage);

    Table table = new Table();
    table.left().bottom();

    Image upImg = new Image(new Texture("Controle/cima.png"));
    upImg.setSize(100, 100);
    upImg.addListener(new InputListener(){

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            upPressed = true;
            return true;
        }
        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            upPressed = false;
        }
    });


    Image rightImg = new Image(new Texture("Controle/direita.png"));
    rightImg.setSize(100, 100);
    rightImg.addListener(new InputListener(){

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            rightPressed = true;
            return true;
        }
        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            rightPressed = false;
        }
    });

    Image leftImg = new Image(new Texture("Controle/esquerda.png"));
    leftImg.setSize(100, 100);
    leftImg.addListener(new InputListener(){

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            leftPressed = true;
            return true;
        }
        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            leftPressed = false;
        }
    });

    table.add();
    table.add(upImg).size(upImg.getWidth(), upImg.getHeight());
    table.add();
    table.row().pad(10,10,10,10);
    table.add(leftImg).size(leftImg.getWidth(), leftImg.getHeight());
    table.add();
    table.add(rightImg).size(rightImg.getWidth(), rightImg.getHeight());
    table.row().padBottom(10);


    stage.addActor(table);

}

public void draw(){
    stage.draw();
}

public boolean isUpPressed() {
    return upPressed;
}

public boolean isLeftPressed() {
    return leftPressed;
}

public boolean isRightPressed() {
    return rightPressed;
}

public void resize(int width, int height){
    viewport.update(width, height);
}

@Override
public void dispose() {

}
}

How I'm handlingInput with the controller.

 public void handleInput(float dt) {
  if (player.currentState != TheWizard.State.DEAD && player.currentState != TheWizard.State.ACERTOU) {
      if (controle.isUpPressed() && player.b2body.getLinearVelocity().y == 0) {
          player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true);
      } else if (controle.isRightPressed() && player.b2body.getLinearVelocity().x <= 2) {
          player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true);
      } else if (controle.isLeftPressed() && player.b2body.getLinearVelocity().x >= -2) {
          player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true);
      } else {
          player.b2body.setLinearVelocity(new Vector2(player.b2body.getLinearVelocity().x, player.b2body.getLinearVelocity().y));
      }
  }
\$\endgroup\$
0
\$\begingroup\$

I managed to solve it.

My character body was two circles (one above the other), in the worldContactListener class I made it so a variable is set true or false whenever the "head" hits something, and I made it so the condition to jump is something like: "if (controle.isUpPressed() && player.b2body.getLinearVelocity().y == 0 && CodeWizard.CONTATO == true)". In other words, I used a GLOBAL BOOLEAN VAR CONTATO, if the head hits something the VAR value changes to false. I also fixed a problem when both circles was in contact with something, that would make the value of VAR to false, if I press "UP" in the Controller class the value is set to true, it doesn't bug cause it only changes to false when it hits something. This way it doesn't stop it from jumping.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.