简体   繁体   中英

LibGDX Draw Scrollpane WITHOUT Stage

I have a game made with LibGDX, I haven't implemented scene2d or stages or actors at all in my game, but am now in need of a scroll pane. Every example I've seen uses stages and actors to draw the scroll pane, is it possible to draw it using a batcher or anything? like so:

batcher.draw(scrollpane);

instead of creating a whole stage which I haven't done at all.

Every actor has a draw and act function and when you attach them to a stage these get called by that stage. So all you need to do is call draw yourself. And since you want to interact with a scrollpane you also need to call act on it. It also needs a GestureListener so you can scroll it and possibly more stuff the stage would handle otherwise.

scrollpane.act(deltaTime);
scrollpane.draw(spriteBatch, alpha);

I really do wonder why you do not want a stage. Stage gives you a ton of functionality and scalability. Yes for just one scrollpane a whole stage might be overkill but it does not eat your resources. And you need actors to fill up the scrollpane too (you want to handle these all yourself too?). I cannot really find a reason to leave a stage out, it's just a couple lines of code.

Since you are hesitant of learning how the stage works here is something that might help you out since there is little to understand about the basic usage of Stage .

Stage stage = new Stage();

stage.act();
stage.draw();
//This is pretty much it, you can start adding actors to it now.


Table myTable = new Table();
myTable.setFillParent = true; // <-- sets initial table to fill it's parent (in this case the stage)
Scrollpane myScrollpane = new Scrollpane(); // <-- Add actors to hold by scrollpane

myTable.add(myScrollpane);
stage.addActor(myTable);
stage.addActor(myTable);

To be able to interact with the stage you need to set it as a input processor.

Gdx.input.setInputProcessor(stage);
//If you want multiple input processors you need to use a InputMultiplexer

InputMultiplexer im = new InputMultiplexer();
im.addProcessor(stage);
im.addProcessor(new GestureDetector(someScreen));
im.addProcessor(player.getInputProcessor);

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