Why doesn't my approach work to keep repeating the action until the libgdx scene2d button is pressed and stop it when the button is released?

I know that many questions were asked to solve a similar problem, but I could not find an answer to it.

Problem: I created a screen for my libgdx game that looks like this.

enter image description here I want the entrance to concern only. When I press and hold the button labeled "lower", it decreases the number in the middle once. I want it to continue to decrease until I release the bottom button. I placed the button in a table, which, in turn, was added to the scene. To achieve what I want, I used the following approach:

    TextButton less;   //for the button labeled 'lower'
    ....

***constructor start****
            stage = new Stage(new ScreenViewport());
            Gdx.input.setInputProcessor(stage);
            table = new Table();
            table.setFillParent(true);
            stage.addActor(table);
    ......
    ......
            style = new TextButton.TextButtonStyle();
            style.up = new TextureRegionDrawable(upRegion);
            style.down = new TextureRegionDrawable(downRegion);
            style.font = buttonFont;
            less = new TextButton("lower", style);
            table.add(less).expand().fill();
    ....
    ....
    less.addListener(new ChangeListener() {
     @Override
                public void changed(ChangeEvent event, Actor actor) {
                    while(less.isPressed()) {
                        Gdx.app.log("ispressed ","yes");
                        curLevel--;
                        level.setText("" + curLevel);
                        Gdx.app.log("curLevel: ",curLevel+"");
                        try{
                            Thread.sleep(1000);
                        }catch (Exception e){

                        }
                    }
                }
    }
....
....
***constructor ends****

while () , "/", . , .

: ( ):

ext {
        appName = 'kombat'
        gdxVersion = '1.5.2'
        roboVMVersion = '1.0.0-beta-01'
        box2DLightsVersion = '1.3'
        ashleyVersion = '1.3.1'
        aiVersion = '1.4.0'
    }

gaRos, . , gaRos:

  •         package com.zeher.kombat;
            import com.badlogic.gdx.scenes.scene2d.ui.Skin;
            import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
            import com.zeher.kombat.Screens.LevelChooser;
            /**
               * Created by zucky on 10/4/2015.
            */
                public class LessButton extends TextButton {
                    LevelChooser lc;
                    public LessButton(String text,TextButton.TextButtonStyle skin,Kombat game) {
        super(text, skin);
        this.lc=game.introScreen.lc;
    
    }
    @Override
    public void act(float delta){
        if(lc.lowerFlag ) {
            lc.curLevel--;
            lc.level.setText("" + lc.curLevel);
        }
    
    }
    

    }

  • listListener.

             less.addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            lowerFlag = true;
                return false;
            }
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                lowerFlag = false;
                Gdx.app.log("occured: ","touchUp on less");
            }
        });
    

, touchUp , lowerFlag false.

, , , .

0
2

, gaRos .

  • , , ActorGestureListener. :

    less.addListener(new ActorGestureListener() {
            public void touchDown(InputEvent event, float x, float y, int pointer, int button) {
                lowerFlag = true;
                Gdx.app.log("occured: ","touchDown on less"); //to test whether it works or not
            }
    
    
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                lowerFlag = false;
                Gdx.app.log("occured: ","touchUp on less");
            }
    });
    
  • act "", , TextButton , , true. delta , curLevel.

( , ). , LessButton.java.

    public class LessButton extends TextButton {
    LevelChooser lc;
    public LessButton(String text, TextButton.TextButtonStyle skin,Kombat game) {
        super(text, skin);
        this.lc=game.introScreen.lc;
    }
    @Override
    public void act(float delta){
        wait+=delta;
        if(lc.lowerFlag && lc.curLevel>0 && wait>delta*8) {
            lc.curLevel--;
            lc.level.setText("" + lc.curLevel);
            wait=0f;
        }
    }
    }

3. :

        LessButton less;
        ....
        less = new LessButton("lower", style,game);
+2

TextButton , InputListener.

:

less.addListener(new InputListener() {
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                lowerFlag = true;
                return true;
        }


    public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            lowerFlag = false;
        }
 }); 

act , true. , curLevel.

( , )

+4

All Articles