简体   繁体   English

Libgdx:如何将控件与可移动对象分开?

[英]Libgdx: How do you separate the controls from the movable object?

I'm very new to Libgdx. 我是Libgdx的新手。 I have looked through many helpful tutorials, but nothing has implemented the following structure. 我浏览了许多有用的教程,但没有实现以下结构。 I've implemented a movable object that is an extension of InputAdapter, and overrides keyDown/Up to update its(object) location. 我实现了一个可移动对象,它是InputAdapter的扩展,并且覆盖keyDown / Up来更新其(对象)位置。 Now, I've implemented a touchPadController class that has a touchpad and knob that are visible on the screen. 现在,我实现了一个touchPadController类,该类具有在屏幕上可见的触摸板和旋钮。 Then, I added a variable that is an object of the touchPadController class. 然后,我添加了一个变量,该变量是touchPadController类的对象。

My ultimate goal in the future is to completely separate the controls class from any movable objects/characters. 我未来的最终目标是将控件类与所有可移动对象/字符完全分开。

The problem: I want to call the setInputProcessor only to the movable objects/characters, and not directly to the touchPadController class. 问题:我只想对可移动对象/字符调用setInputProcessor,而不能直接对touchPadController类调用。 I want the parent movable object to call its own controls. 我希望父级可移动对象调用其自己的控件。 But, I do not know where the of call for the touchPadController functions would happen?? 但是,我不知道touchPadController函数的调用会在哪里发生?

  • I tried this but it didn't work: 我试过了,但是没有用:

    //movable object @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { Gdx.input.setInputProcessor(touchPadController); } //touchPadController @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { parent.newSpeedX = touchpad.getKnobPercentX() * Speed; parent.newSpeedY = touchpad.getKnobPercentY() * Speed; return true; }

*Where should I call to the touchPadController within the parent(movable object)? *我应该在哪里调用父对象(可移动对象)中的touchPadController?

Separate your controllers and your characters/entities like this: 像这样分隔控制器和角色/实体:

public class Controller extends InputAdapter {
    private Entity _controllee;

    public void setControllee(Entity toControl) {
        _controllee = toControl;
    }

    // Override whichever InputAdapter methods you need to control your moveable objects, e.g.:
    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        _controllee.newSpeedX = touchpad.getKnobPercentX() * Speed;
        _controllee.newSpeedY = touchpad.getKnobPercentY() * Speed;
        return true;
    }
}

And finally somewhere in your ApplicationListener or Screen you create an instance of the controller, attach a controllee to it via setControlle and set it as an input processor, like this: 最后,在ApplicationListener或Screen中的某个位置上,创建控制器的实例,通过setControlle将控件附加到该控件,并将其设置为输入处理器,如下所示:

Controller myController = new Controller();
myController.setControllee(/*one of your movable objects*/);
Gdx.input.setInputProcessor(myController);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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