Pausing a LIBGDX progress bar - java

I am trying to pause a progress bar. How would I go about it? I have a listener on a Pause button where I could do the pause but I cannot stop the animation.
Here is the code that currently runs the progress bar.
Pixmap pixmap = new Pixmap(100, 50, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.CYAN);
pixmap.fill();
TextureRegionDrawable drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
pixmap.dispose();
ProgressBar.ProgressBarStyle progressBarStyle = new ProgressBar.ProgressBarStyle();
progressBarStyle.background = drawable;
pixmap = new Pixmap(0, 50, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.RED);
pixmap.fill();
drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
pixmap.dispose();
progressBarStyle.knob = drawable;
pixmap = new Pixmap(100, 50, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.RED);
pixmap.fill();
drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
pixmap.dispose();
progressBarStyle.knobBefore = drawable;
ProgressBar bar = new ProgressBar(0.0f, 1.0f, 0.01f, false, progressBarStyle);
bar.setBounds(100, 1600, 875, 50);
bar.setAnimateDuration(25)
bar.setValue(1f);
stage.addActor(bar);

One option is to bypass the animation completely, update the progress bar from your render callback (by default libgdx combines these callbacks into one), and rely on the pause + resume callbacks at the application-level. Here's an example based on your initial code. Note the bar has no animation and a max value of 25, just to make the progress updates a bit more intuitive.
public class ProgressBarTest implements ApplicationListener {
boolean active;
SpriteBatch batch;
Stage stage;
int width = 380;
int height = 180;
ProgressBar bar;
#Override
public void create () {
active = true;
batch = new SpriteBatch();
stage = new Stage(new FitViewport(width, height));
Pixmap pixmap = new Pixmap(100, 50, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.CYAN);
pixmap.fill();
TextureRegionDrawable drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
pixmap.dispose();
ProgressBar.ProgressBarStyle progressBarStyle = new ProgressBar.ProgressBarStyle();
progressBarStyle.background = drawable;
pixmap = new Pixmap(0, 50, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.RED);
pixmap.fill();
drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
pixmap.dispose();
progressBarStyle.knob = drawable;
pixmap = new Pixmap(100, 50, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.RED);
pixmap.fill();
drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
pixmap.dispose();
progressBarStyle.knobBefore = drawable;
bar = new ProgressBar(0f, 25f, 0.01f, false, progressBarStyle);
bar.setBounds(100, 1600, 875, 50);
stage.setRoot(new Table() {
{
add(bar);
setFillParent(true);
setSize(width, height);
}
});
}
#Override
public void resize(int x, int y) {
}
#Override
public void render () {
if (active && bar.getValue() < bar.getMaxValue()){
bar.setValue(bar.getValue() + Gdx.graphics.getDeltaTime());
if (bar.getValue() >= bar.getMaxValue()){
System.out.println("Filled up!");
}
}
stage.act();
ScreenUtils.clear(0, 0, 0, 1);
batch.begin();
stage.draw();
batch.end();
}
#Override
public void pause() {
System.out.println("Paused!");
active = false;
}
#Override
public void resume() {
System.out.println("Resumed!");
active = true;
}
#Override
public void dispose () {
batch.dispose();
stage.dispose();
}
}
Maybe something like this works for your use case?
EDIT: Also, if you want to keep the animation, you can probably get away with conditionally calling stage.act() when active is true.

Related

Libgdx (Android) - ImageButton has wrong size (scale)

I have a problem with size of ImageButton(s), buttons are not strectched/scaled because screen size of device its 1920x1080 and button picture is 342x129. (it is viewed smaller than original picture seems).
When I draw it through this game.batch.draw(playBtn, ...);
It works fine, but I need ImageButtons. What is wrong ?
Note: WIDTH = 480, HEIGHT = 800
picture: https://imgur.com/IJs4uPB
public MenuScreen(PlumberGame game) {
this.game = game;
camera = new OrthographicCamera();
viewport = new FitViewport(WIDTH,HEIGHT, camera);
stage =new Stage(viewport, spriteBatch);
background = new Texture("background.png");
title = new Texture("title.png");
camera.setToOrtho(false, WIDTH, HEIGHT);
}
#Override
public void show() {
stage = new Stage(); //Set up a stage for the ui
Gdx.input.setInputProcessor(stage); //Start taking input from the ui
atlas = new TextureAtlas("ui/buttons.pack");
skin = new Skin(atlas);
table = new Table(skin);
table.setBounds(0,0, WIDTH, HEIGHT);
ImageButton.ImageButtonStyle playBtnStyle = new ImageButton.ImageButtonStyle();
playBtnStyle.up = skin.getDrawable("playBtn");
playBtnStyle.down = skin.getDrawable("playBtnOn");
playBtn = new ImageButton(playBtnStyle);
playBtn.setSize(playBtn.getWidth(), playBtn.getHeight());
playBtn.getImage().setScaling(Scaling.fit);
playBtn.invalidateHierarchy();
playBtn.setPosition((float)(WIDTH * 0.15 ), (float) (HEIGHT * 0.5));
stage.addActor(playBtn);
}
#Override
public void render(float delta) {
stage.getBatch().setProjectionMatrix(camera.combined);
renderer.setProjectionMatrix(stage.getBatch().getProjectionMatrix());
renderer.setTransformMatrix(stage.getBatch().getTransformMatrix());
renderer.translate(WIDTH, HEIGHT, 0);
stage.act(Gdx.graphics.getDeltaTime()); //Perform ui logic
stage.getBatch().begin();
stage.getBatch().draw(background, 0, 0, WIDTH,HEIGHT);
stage.getBatch().draw(title, (float)(WIDTH * 0.12), (float) (HEIGHT * 0.75));
stage.getBatch().end();
stage.draw(); //Draw the ui
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
table.invalidateHierarchy();
table.setSize(width, height);
camera.update();
}
You are setting playBtn size wrong way.
This line doesn't make sense:
playBtn.setSize(playBtn.getWidth(), playBtn.getHeight());
I would recommend using Table which is really convinient when designing UIs. You specify the size of the actor when adding it to the table.
I can't try it out now since I'm not at home but it would look something like:
#Override
public void show() {
stage = new Stage(); //Set up a stage for the ui
Gdx.input.setInputProcessor(stage); //Start taking input from the ui
atlas = new TextureAtlas("ui/buttons.pack");
skin = new Skin(atlas);
table = new Table(skin);
table.setBounds(0,0, WIDTH, HEIGHT);
ImageButton.ImageButtonStyle playBtnStyle = new ImageButton.ImageButtonStyle();
playBtnStyle.up = skin.getDrawable("playBtn");
playBtnStyle.down = skin.getDrawable("playBtnOn");
playBtn = new ImageButton(playBtnStyle);
playBtn.setPosition((float)(WIDTH * 0.15 ), (float) (HEIGHT * 0.5));
Table table = new Table();
table.setFillParent(true);
table.add(playBtn).expand().fillX().height(yourHeight);
stage.addActor(table);
}
EDIT:
Another alternative is to set min size of the drawables you put in the image button style:
ImageButton.ImageButtonStyle playBtnStyle = new ImageButton.ImageButtonStyle();
playBtnStyle.up = skin.getDrawable("playBtn");
playBtnStyle.down = skin.getDrawable("playBtnOn");
playBtnStyle.up.setMinWidth(yourWidth);
playBtnStyle.up.setMinHeight(yourHeight);
playBtnStyle.down.setMinWidth(yourWidth);
playBtnStyle.down.setMinHeight(yourHeight);

Libgdx problems with viewport/camera/rendering

I am having trouble for the last 2 days to figure out what is wrong with my application. I'm trying to build a game, a platformer where the player needs to go up, (somewhat like doodlejump). I have build my testmap in tmx format. I have been trying to start the camera and the viewport to show the left-bottom of the world, so the player can move to the right and also upward. But this seems to difficult for a beginning libgdx learner!
I am using a Playscreen, a HUD and a MainActivity:
PlayScreen
private MainActivity game;
private OrthographicCamera gamecam;
private Viewport gameport;
private HUD hud;
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private World world;
private Box2DDebugRenderer b2dr;
private Jack player;
public PlayScreen(MainActivity game){
// Initiates the game, camera, viewport and HUD
this.game = game;
gamecam = new OrthographicCamera();
hud = new HUD(game.batch);
gameport = new FitViewport(MainActivity.V_WIDTH, MainActivity.V_HEIGHT, gamecam);
// loading the map
mapLoader = new TmxMapLoader();
map = mapLoader.load("level1revised.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / MainActivity.PPM);
//initially set our gamecam to be centered correctly at the start of of map
gamecam.position.set(gameport.getWorldWidth() / 2, gameport.getWorldHeight() / 2, 0);
// Box2D world (graphics)
world = new World(new Vector2(0, -10), true);
b2dr = new Box2DDebugRenderer();
// New Jack
player = new Jack(world);
BodyDef bdef = new BodyDef();
PolygonShape shape = new PolygonShape();
FixtureDef fdef = new FixtureDef();
Body body;
// The ground as an object is defined
for(MapObject object: map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)){
Rectangle rect = ((RectangleMapObject) object).getRectangle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set((rect.getX() + rect.getWidth()/2)/MainActivity.PPM, (rect.getY() + rect.getHeight()/2) /MainActivity.PPM);
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth()/2, rect.getHeight()/2);
fdef.shape = shape;
body.createFixture(fdef);
}
public void update(float dt){
handleInput(dt);
world.step(1 / 60f, 6, 2);
//gamecam.position.x = player.b2body.getPosition().x;
gamecam.update();
renderer.setView(gamecam);
}
#Override
public void render(float delta) {
update(delta);
// Clear the screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// render the map
renderer.render();
// render the box2d
b2dr.render(world, gamecam.combined);
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
#Override
public void resize(int width, int height) {
gamecam.viewportWidth = width;
gamecam.viewportHeight = height;
}
HUD & Mainactivity
public HUD(SpriteBatch sb){
worldTimer = 300;
timeCount = 0;
score = 0;
viewport = new FitViewport(MainActivity.V_WIDTH, MainActivity.V_HEIGHT, new OrthographicCamera());
stage = new Stage(viewport, sb);
Table table = new Table();
table.top();
table.setFillParent(true);
countdownlabel = new Label(String.format("%03d", worldTimer), new Label.LabelStyle(new BitmapFont(), Color.RED));
scoreLabel = new Label(String.format("%06d", score), new Label.LabelStyle(new BitmapFont(), Color.RED));
timeLabel = new Label("TIME", new Label.LabelStyle(new BitmapFont(), Color.RED));
levelLabel = new Label("level 1", new Label.LabelStyle(new BitmapFont(), Color.RED));
table.add(timeLabel).expandX().padTop(10);
table.add(levelLabel).expandX().padTop(10);
table.row();
table.add(scoreLabel).expandX().padTop(10);
stage.addActor(table);
}
public class MainActivity extends Game {
public SpriteBatch batch;
public static final int V_WIDTH = 720;
public static final int V_HEIGHT = 480;
public static final float PPM = 1;
public static final String TITLE = "Jack the Pirate";
#Override
public void create () {
batch = new SpriteBatch();
setScreen(new PlayScreen(this));
}
#Override
public void render () {
super.render();
}
}
I have no clue where to look anymore, been through the basics countless times :( My tmx tilemap is 14 tiles wide, and 70 tiles high. The tiles themselves are 16x16. My test device is a Samsung Galaxy S3, and i wanted to use it in portrait mode.
Any ideas where to look? Thanks in advance for help!

Libgdx render for a rectangle array different textures

I am developing a game with libgdx and i got stuck at a point. So My SpriteBatch draws for all the Rectangles that are in an array with the same texture but I want that every single one has its own texture. MY Code looks like this
public class GameScreen implements Screen{
final MrJetpack game;
OrthographicCamera camera;
SpriteBatch batch;
ShapeRenderer rend;
private Array<Rectangle> raindrops;
Texture enemy1,enemy2,enemy3,enemy4,endScreen;
TextureRegion[] enemys = new TextureRegion[4];
private int random;
public GameScreen(final MrJetpack game){
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
enemy1 = new Texture(Gdx.files.internal("boxk.png"));
enemy2 = new Texture(Gdx.files.internal("boxg.png"));
enemy3 = new Texture(Gdx.files.internal("kugel.png"));
enemy4 = new Texture(Gdx.files.internal("kugelk.png"));
enemys[0] = new TextureRegion(enemy1);
enemys[1] = new TextureRegion(enemy2);
enemys[2] = new TextureRegion(enemy3);
enemys[3] = new TextureRegion(enemy4);
raindrops = new Array<Rectangle>();
rend = new ShapeRenderer();
batch = new SpriteBatch();
}
#Override
public void render(float delta) {
// TODO Auto-generated method stub
//Gdx.gl.glClearColor(0, (float)148/255,(float) 255/255, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
rend.begin(ShapeType.Filled);
rend.rect(0, 0, 800, 10);
rend.rect(0, 160, 800, 10);
rend.rect(0, 320, 800, 10);
rend.setColor(Color.ORANGE);
rend.end();
batch.begin();
for(Rectangle raindrop: raindrops) {
batch.draw(enemys[random], raindrop.x - 10, raindrop.y);
}
batch.end();
if(TimeUtils.nanoTime() - lastDropTime > spawnTime){
spawnRaindrop();
}
Iterator<Rectangle> iter = raindrops.iterator();
while(iter.hasNext()) {
Rectangle raindrop = iter.next();
raindrop.x -= 20 * Gdx.graphics.getDeltaTime();
if(raindrop.x < 0) {
spawnRaindrop();
iter.remove();
}
}
}
private void spawnRaindrop() {
Rectangle raindrop = new Rectangle();
raindrop.x = 800;
stages = MathUtils.random(1, 3);
random = MathUtils.random(0, 3);
raindrop.width = 30;
raindrop.height = 53;
raindrops.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
}
So what actually happening is that everytime a new Rectangle spawns in the screen the other ones who were already displayed change the Texture so every Rectangle got the same Texture . Any solutions or examples?
EDIT: http://imgur.com/46ywYyy
This is my problem for people who understood my question false :)
Like you can see the texture is changing for all the other rectangles but i want everyone to have their static texture
They told you, one way to do it, but I see you do not clear, I'll put more or less as you might do.
I'll try to make the form more similar to what you already have in your code.
note
this code may have syntax error, among others, becouse I'm doing from the editor StackOverflow.
1- create a class that is derived from sprite, for example something like:
public class SpriteRaindrop extends Sprite{
Rectangle raindrop = new Rectangle();
public SpriteRaindrop(Texture t, int srcWidth,
int srcHeigth,
float posX,
float posY){
super(t, srcWidth, srcHeigth);
raindrop.x = posX;
raindrop.y = posY;
raindrop.width = srcWidth;
raindrop.height = srcHeigth;
setPosition(posX, posY);
}
public void updateR(){
raindrod.x = getX();
raindrod.y = getY();
}
}
This your code with with any changes
public class GameScreen implements Screen{
final MrJetpack game;
OrthographicCamera camera;
SpriteBatch batch;
ShapeRenderer rend;
private Array<SpriteRaindrop> raindrops;
Texture enemy1,enemy2,enemy3,enemy4,endScreen;
TextureRegion[] enemys = new TextureRegion[4];
private int random;
public GameScreen(final MrJetpack game){
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
enemy1 = new Texture(Gdx.files.internal("boxk.png"));
enemy2 = new Texture(Gdx.files.internal("boxg.png"));
enemy3 = new Texture(Gdx.files.internal("kugel.png"));
enemy4 = new Texture(Gdx.files.internal("kugelk.png"));
enemys[0] = new TextureRegion(enemy1);
enemys[1] = new TextureRegion(enemy2);
enemys[2] = new TextureRegion(enemy3);
enemys[3] = new TextureRegion(enemy4);
raindrops = new Array<SpriteRaindrop>();
rend = new ShapeRenderer();
batch = new SpriteBatch();
}
#Override
public void render(float delta) {
// TODO Auto-generated method stub
//Gdx.gl.glClearColor(0, (float)148/255,(float) 255/255, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
rend.begin(ShapeType.Filled);
rend.rect(0, 0, 800, 10);
rend.rect(0, 160, 800, 10);
rend.rect(0, 320, 800, 10);
rend.setColor(Color.ORANGE);
rend.end();
batch.begin();
for(SpriteRaindrop raindrop: raindrops) {
//raindrop.translatex(-10f);
//what is raindrop.y value
raindrop.updateR();
raindrop.draw(batch);
}
batch.end();
if(TimeUtils.nanoTime() - lastDropTime > spawnTime){
spawnRaindrop();
}
Iterator<SpriteRaindrop> iter = raindrops.iterator();
while(iter.hasNext()) {
SpriteRaindrop raindrop = iter.next();
raindrop.translateX(-20f * Gdx.graphics.getDeltaTime());
if(raindrop.getX() < 0) {
spawnRaindrop();
iter.remove();
}
}
}
private void spawnRaindrop() {
stages = MathUtils.random(1, 3);
random = MathUtils.random(0, 3);
lastDropTime = TimeUtils.nanoTime();
SpriteRaindrop raindrop = new SpriteRaindrop(enemys[random],
30, 53,
800, 0);
raindrops.add(raindrop);
}
this is just an idea, but I think it can work, I hope you help
The variable random is global for class, once a new sprite spawns it sets the value to new random value and all of them are draw by:
batch.draw(enemys[random], raindrop.x - 10, raindrop.y);
You need to track this together with location per instance.

Superposed the sprites with libGDX

I would like to superpose two sprites but, when I resize my windows the sprites moves ...
My code:
public void resize(int width, int height) {
// TODO Auto-generated method stub
if ( height < width ){
spriteAbalone.setScale(height/700f);
billeBlanche.setScale(height/700f);
}
else{
spriteAbalone.setScale(width/700f);
billeBlanche.setScale(width/700f);
}
spriteAbalone.setPosition((width-700)/2, (height - 700)/2);
billeBlanche.setPosition((width-400)/2,(height-400)/2);
camera.setToOrtho(false, width, height);
camera.update();
}
#Override
public void show() {
// TODO Auto-generated method stub
float screenW = Gdx.graphics.getWidth();
float screenH = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, screenW, screenH);
batch = new SpriteBatch();
spriteAbalone = new Sprite(new TextureRegion(new Texture(Gdx.files.internal("data/AbaloneCS5.gif")), 0, 0, 704, 704));
spriteAbalone.setSize(700 , 700);
spriteAbalone.setOrigin(704/2,704/2);
billeBlanche = new Sprite(new TextureRegion(new Texture(Gdx.files.internal("data/billeblanche.gif")),0,0,200,200));
billeBlanche.setSize(65, 65);
billeBlanche.setOrigin(704/2,704/2);
}
Sprite#setOrigin Sets the origin in relation to the sprite's position for scaling and rotation. In this code you set the same origin for both sprites despite one of them being considerably smaller:
spriteAbalone = new Sprite(new TextureRegion(new Texture(Gdx.files.internal("data/AbaloneCS5.gif")), 0, 0, 704, 704));
spriteAbalone.setSize(700 , 700);
spriteAbalone.setOrigin(704/2,704/2);
billeBlanche = new Sprite(new TextureRegion(new Texture(Gdx.files.internal("data/billeblanche.gif")),0,0,200,200));
billeBlanche.setSize(65, 65);
billeBlanche.setOrigin(704/2,704/2);
Change the billeBlanche origin to this:
billeBlanche.setOrigin(200/2,200/2);
Also,in resize the position you set for the sprites is fixed -always (width-700)/2 or (width-400)/2- but your sprite sizes is different. Thats the reason they "move".

LibGDX Tablelayout Menu does not show

I'm writing a new program in LibGDX handling Backgroundtextures and have just begun to implement the screens. But when I test it, it just shows me a black cleared screen with the given resolution.
I call the screen with the setScreen(screen)- Method in an implemented Game class.
So here is the code:
public class MenuScreen implements Screen{
Table table;
Stage stage;
TextButton button;
TextField textField;
Texture texture;
Pic2Box2d game;
public MenuScreen(Pic2Box2d gameH)
{
this.game = gameH;
stage = new Stage(0, 0, true);
table = new Table();
}
public void create()
{
final TextFieldStyle fieldStyle = new TextFieldStyle(new BitmapFont(), Color.WHITE, new BitmapFont(), Color.GRAY, null, null, null);
textField = new TextField("path", fieldStyle);
final TextButtonStyle buttonStyle = new TextButtonStyle();
buttonStyle.font = new BitmapFont();
buttonStyle.fontColor = Color.WHITE;
buttonStyle.pressedOffsetY = 1f;
buttonStyle.downFontColor = new Color(0.8f, 0.8f, 0.8f, 1f);
button = new TextButton("Übernehmen", buttonStyle);
button.setClickListener(new ClickListener() {
#Override
public void click(Actor actor, float x, float y) {
if(textField.getText() != "" && textField.getText() != "path")
{
texture = new Texture(textField.getText());
game.setScreen(new Workscreen(texture));
}
}
});
table.row();
table.add(textField);
table.add(button);
stage.addActor(table);
}
#Override
public void render(float delta) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(0, 0, 0, 1);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
}
#idaNakav is correct about using show() and setting the Stage dimensions. However, that will still give you a black screen because your table doesn't have any size. Try changing your show() code to include table.setFillParent(true).
It should look something like this (code based on the example that you provided).
#Override
public void show() {
final TextFieldStyle fieldStyle = new TextFieldStyle(new BitmapFont(), Color.WHITE, null, null, null);
textField = new TextField("path", fieldStyle);
final TextButtonStyle buttonStyle = new TextButtonStyle();
buttonStyle.font = new BitmapFont();
buttonStyle.fontColor = Color.WHITE;
buttonStyle.pressedOffsetY = 1f;
buttonStyle.downFontColor = new Color(0.8f, 0.8f, 0.8f, 1f);
button = new TextButton("Übernehmen", buttonStyle);
button.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
if (textField.getText() != "" && textField.getText() != "path") {
texture = new Texture(textField.getText());
game.setScreen(new Workscreen(texture));
}
}
});
table.row();
table.add(textField);
table.add(button);
// Make the table fill the stage.
table.setFillParent(true);
stage.addActor(table);
}
When screen is becoming the current screen show method is called (not create like in your case), try changing your create method to show
also you are init the stage wrong , you should send it width and height , you sent 0,0
try something like
stage = new Stage(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),false);

Categories

Resources