为什么我的工作方针,不断重复,直到一个libgdx scene2d按钮的动作是pressed和停止按钮被释放时? [英] Why doesn't my approach work to continuously repeat an action till a libgdx scene2d button is pressed and stop it when the button is released?

查看:234
本文介绍了为什么我的工作方针,不断重复,直到一个libgdx scene2d按钮的动作是pressed和停止按钮被释放时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道很多问题都被要求解决类似的问题,但我无法找到答案。

I know many questions have been asked to solve similar problem but i could not find an answer to it.

问题:我已经做了屏幕对我的libgdx游戏,看起来像下面的截图。

Problem : I have made a screen for my libgdx game which looks like the following screenshot.

在这里输入的形象描述
我想通过触摸only.When我preSS按住按钮标记为输入'低'呢递减中间的数字一次。我希望它保持递减,直到我释放低按钮。
我已放置在一个表中,而这又是在一阶段加入的按钮。为了实现我想要什么我用下面的方法:

I want the input through touch only.When i press and hold the button labeled 'lower' it decrements the number in the middle once. I want it to keep decrementing until i release the 'lower' button. I have placed the button in a table, which in turn is added on a stage. 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循环)变为无限即使我释放少/降
按钮。我也曾尝试解决<一个href=\"http://stackoverflow.com/questions/19071834/scene2d-how-to-handle-touched-actorlibgdx\">this的问题,但这也导致无限循环

But the while loop in changed() goes infinite even if i release the "less/lower" button. I have also tried the solution to this question but this also results in infinite loop.

此外我的项目细节是:(如果他们无所谓):

Also my project details are : (if they matter) :

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的说明:

I tried solution from gaRos but as a result the number in the label goes on decreasing infinitely even after a single touch and release. Here is what i did to follow gaRos's instructions :


  1. 推翻了该法的方法

  1. Overrode the act method

        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);
    }

}

}

使用inputListener。

Used the inputListener.

         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");
        }
    });


但问题是,触上事件不会被解雇,并lowerFlag从来没有切换到错误的。

But the problem is that the touchUp event does not get fired and lowerFlag never switches to false.

请告诉我是什么原因造成这个意外的行为,以及如何处理preSS并按住手势我想要的方式。

Kindly tell me what is causing this unexpected behaviour and how to handle the press and hold gesture the way i want.

推荐答案

首先,我要感谢GAROS给一个暗示。

First of all, i want to thank gaRos for giving a hint.


  1. 我们应该与演员要使用的监听器用于此目的是<一个href=\"https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/utils/ActorGestureListener.html\"相对=nofollow> ActorGestureListener 。
    它应该是这样的:

  1. The listener we should be using with actors for this purpose is ActorGestureListener . It should be like this :

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");
        }
});


  • 现在通过延伸TEXT按钮,这样就降低了值,如果标志为true一个新的类覆盖了少按钮的行为方式。使用增量,只要你想如何快速降低curLevel参数。

  • Now override the act method for that 'less' button by making a new class which extends TextButton such that it lowers the value if the flag is true. Use the delta as a parameter how fast you want to decrease curLevel.

    (如果增量越大减少多,如果是低级下降更少)。例如我创建的类LessButton.java。

    (if the delta is bigger decrease more, if is lower decrease less). For example i created the class 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。现在,使用这样的:

    3. Now use it like this:

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

    这篇关于为什么我的工作方针,不断重复,直到一个libgdx scene2d按钮的动作是pressed和停止按钮被释放时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆