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

查看:17
本文介绍了单击 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.

截图:

我的 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.

覆盖层由一个Image、屏幕本身和四个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)以某种方式被绘制在消息框的后面.这是为什么?注意:如果我采用完全相同的情况,并将我的自定义角色更改为常规图像,则所有内容都会正确绘制,但是我无法再单击较低层中的任何内容,如上所述.

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

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天全站免登陆