单击libGDX中的Actor [英] Click through an Actor in libGDX

查看:161
本文介绍了单击libGDX中的Actor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的游戏中有一个叠加层,其中包含一个屏幕图像,以及一组位于屏幕上的按钮。

I have an overlay in my game that consists of an image of a screen, and a set of buttons that are "on" the screen.

屏幕截图:

我的屏幕有一个 Stage Stage 有一组 Group 对象,我认为这些对象是图层。第一组具有背景,中间的组具有游戏元素,最前面的组具有屏幕覆盖。

My Screen has one Stage. The Stage has a set of Group objects, which I think of as layers. The first group has the backgrounds, the groups in the middle has the game elements, and the frontmost group has the screen overlay.

覆盖层由一个<$ c组成$ c>图像,屏幕本身,以及四个 TextButton (每个角落一个)。

The overlay layer consists of one Image, the screen itself, and four TextButton (one in each corner).

如果不是因为覆盖层中的图像位于其前面而无法点击游戏层中的任何内容,这将非常有用。即使图像是透明的,它仍会在到达游戏层之前与所有触摸事件相互接触。

This would work great, if it weren't for the fact that I can't click on anything in the game layer as long as the image in the overlay layer is in front of it. Even if the image is transparent, it still interecepts all touch events before they reach the game layer.

所以我的问题是:我如何制作图像覆盖层会忽略所有触摸事件,以便游戏层可以获得它们并且实际上可以玩游戏吗?

我尝试了一个想法我自己,但我不确定这是否是正确的方法:
我尝试将图像创建为自定义的 Actor 高度/宽度设置为0,但仍然(通过重载draw()方法)在整个屏幕上绘制图像。这非常有效,除了由于某种原因图像被绘制在较低层中的元素后面这一事实。

I tried one idea myself, but I'm not sure this is the right way to do it: I tried creating the image as a custom Actor that always had height/width set to 0, but still (by overloading the draw() method) drew the image on the entire screen. This works very well, except for the fact that the image for some reason gets drawn behind elements in lower layers.

屏幕截图: https://dl.dropboxusercontent.com/u/1545094/Screen2.png

在这个截图中,我打开了一个指令消息框,它将自己添加到其中一个游戏层(第6组)。
请注意,覆盖层(即组7)中的所有按钮都位于消息框的前面,但是屏幕框架(它是一个自定义Actor)会以某种方式在消息框后面绘制。这是为什么?
注意:如果我采用完全相同的情况,并将我的自定义actor更改为常规图像,则所有内容都被正确绘制,但是我不能再单击较低层中的任何内容,如上所述。

In this screenshot, I have opened a instruction messagebox, which adds itself to one of the game layers (group 6). Note that all the buttons in the overlay layer (which is group 7) is in front of the messagebox, but the screen frame (which is a custom Actor) somehow gets drawn behind the messagebox. Why is that? Note: If I take this exact same case, and change my custom actor into a regular Image, everything is drawn correctly, but then I can't click anything in the lower layers anymore, as described above.

这是我的自定义角色,如果有人能够理解它:

This is my custom actor, if anybody can make any sense of it:

public class ClickThroughImage extends Actor {

    BaseDrawable d;

    public NonexistingImage(BaseDrawable d){
        this.d = d;
        setSize(0, 0);
    }

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        d.draw(batch, 0, 0, 1024, 768); //Yes, I tried swapping these two lines. 
        super.draw(batch, parentAlpha); //It had no effect.
    }
}


推荐答案

使用 InputMultiplexer 。 InputMultiplexer类允许您在多个输入处理器之间共享用户输入。创建自己的类,扩展 InputProcessor ,并将其放入InputMultiplexer与您的舞台。这样您就可以自定义方式响应用户输入,并且仍然可以使用您的舞台。

Use an InputMultiplexer. The InputMultiplexer class allows you to share user input among multiple input processors. Create your own class extending InputProcessor, and put that in InputMultiplexer with your Stage. That way you can respond to user input in a custom way, and still be able to use your stage.

    InputMultiplexer multiplexer = new InputMultiplexer();
    Array<InputProcessor> processors = new Array<InputProcessor>();
    MyInputProcessor myInputProcessor = new MyInputProcessor(); 
    processors.add(myInputProcessor);
    processors.add(stage);
    this.multiplex.setProcessors(processors);

    //...
    //and in your show method in your Screen class
    Gdx.input.setInputProcessor(this.multiplex);

此外,请务必从 Actor.hit 。这应该导致演员不回应任何用户互动。

Also, be sure to return null from Actor.hit. This should cause the actor to not respond to any user interaction.

这就是我在游戏中解决这个问题的方法。

This is how I solved this problem in my game.

这篇关于单击libGDX中的Actor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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