简体   繁体   中英

Input handling in stage and actor

I am setting stage as input processor in the constructor of my screen,

public GameScreen() {
    stage = new GameStage();
    Gdx.input.setInputProcessor(stage);
}


@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act();
    stage.draw();
}

In stage, I am calling event handlers like this,

   private OrthographicCamera camera;
    private Box2DDebugRenderer renderer;
    private Vector3 touchPoint;

    private static final int VIEWPORT_WIDTH = 1280;
    private static final int VIEWPORT_HEIGHT = 720;

    public GameStage() {
        touchPoint = new Vector3();
        setupCamera();
        this.setViewport(new ScreenViewport(camera));
        renderer = new Box2DDebugRenderer();
        setUpWorld();
    }


    private void setUpWorld() {
        world = WorldUtils.createWorld();
        world.setContactListener(this);
        setUpGround();
    }

    private void setUpGround() {
        addActor(new Rock(WorldUtils.createGroundRock(world, 320 / Constants.PIXELS_TO_METERS, Constants.GROUND_HEIGHT, 640f / Constants.PIXELS_TO_METERS, 130f / Constants.PIXELS_TO_METERS)));
    }


    private void setupCamera() {
        camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
        camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
        camera.update();
    }

    @Override
    public void act(float delta) {
        super.act(delta);
        accumulator += delta;
        while (accumulator >= delta) {
            world.step(TIME_STEP, 6, 2);
            accumulator -= TIME_STEP;
        }
        camera.update();
    }

    @Override
    public void draw() {
        super.draw();
        renderer.render(world, camera.combined);
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        translateScreenToWorldCoordinates(screenX, screenY);
        Gdx.app.log("LOG", "Touch Coords " + screenX + " : " + screenY);
        return super.touchDown(screenX, screenY, pointer, button);
    }

    private void translateScreenToWorldCoordinates(int x, int y) {
        getCamera().unproject(touchPoint.set(x, y, 0));
    }

    @Override
    public void beginContact(Contact contact) {
        //TODO
    }

    @Override
    public void endContact(Contact contact) {

    }

    @Override
    public void preSolve(Contact contact, Manifold oldManifold) {

    }

    @Override
    public void postSolve(Contact contact, ContactImpulse impulse) {

    }

    @Override
    public boolean scrolled(int amount) {
        camera.zoom -= .42 * amount;
        return true;
    }

Now I have an actor which has an event handler in its constructor,

   public class Rock extends Actor {
    Texture texture = new Texture(Gdx.files.internal("brick.png"));
    Sprite sprite = new Sprite(texture, 150, 130);

    public Rock(Body body) {
        setBounds(body.getPosition().x * Constants.PIXELS_TO_METERS, body.getPosition().y * Constants.PIXELS_TO_METERS, 150, 130);
        setTouchable(Touchable.enabled);
        addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.log("LOG", "TAPPED");
                return super.touchDown(event, x, y, pointer, button);
            }
        });
    }


    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(sprite, getX(), getY(), getWidth(), getHeight());
    }

    @Override
    protected void positionChanged() {
        sprite.setPosition(getX(), getY());
        super.positionChanged();
    }
}

Now, the main input handler in the stage works, but not the one in the actor. It doesn't work even if I remove the input handlers in the stage. I am guessing it needs to be implemented differently, but not sure. How do I make sure both these input handlers work. Also, if I have several Rock actors, I need input handler for all of them to work.

I would suggest that you approach this using the Singleton design pattern.

Theoratically:

  1. create multiple InputProcessors for various tasks
  2. create an instance of all the input processors statically upon start-up and create a single instance of an InputMultiplexer - this allows you to handle multiple InputProcessors
  3. in your game class whenever you're done loading the map - set the InputMultiplexer containing all of your InputProcessors.

Practically:

create a class that extends the InputProcessor , we will call it UiInput :

public class UiInput implements InputProcessor {

@Override
public boolean keyDown(int keycode) {

    //not a great way but what ever. 
    if (keycode == Keys.ESCAPE) {
        GameConstants.gameScreen.pauseOrResume = !GameConstants.gameScreen.pauseOrResume;
        if (!GameConstants.gameScreen.pauseOrResume) {
            GameConstants.gameScreen.resume();
        } else {
            GameConstants.gameScreen.pause();
        }
        return true;
    } else {
        return false;
    }

}

@Override
public boolean keyUp(int keycode) {
    return false;
}

@Override
public boolean keyTyped(char character) {
    return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {return false;}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {return false;}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {return false;}

@Override
public boolean mouseMoved(int screenX, int screenY) {return false;}

@Override
public boolean scrolled(int amount) {return false;}


}

You can create Multiple classes.

Next you would create an instance for all your InputProcessors and including the InputMultiplexer

/* UI-Input */
private static UiInput uiInput = new UiInput();

/* DebugInput */
private static DebugInput debugInput = new DebugInput(); //etc. 

/* inputMultiplexer */
public static InputMultiplexer inputMultiplexer = new InputMultiplexer(uiInput, debugInput);

Now instead of setting the stage as the InputProcessor - in your GameScreen you would put the InputMultiplexer as the InputProcessor:

public GameScreen() {
   stage = new GameStage();
   //Gdx.input.setInputProcessor(stage);
   Gdx.input.setInputProcessor(inputMultiplexer);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM