【问题标题】:Is it possible to draw a transparent layer without using Image?-LibGdx不使用Image可以画透明层吗?-LibGdx
【发布时间】:2017-05-30 11:04:53
【问题描述】:

在我的游戏中,当我点击一个按钮时,会弹出一个弹窗。

同时,我想在屏幕上画一个透明层,除了弹出窗口,给人的印象就像弹出窗口处于活动状态时,背景被禁用。

类似这样的:

是否可以使现有的实体图像创建透明覆盖?


我应该使用透明图像本身来制作透明层的印象吗?

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    Image 是一个可以绘制的Actor,但您需要为您的演员提供透明度,请使用Actor

    创建一个Actor 作为透明层并以正确的顺序添加,这样您就可以只为背景演员禁用触摸。

    您应该维护 Actor 的顺序。

    stage=new Stage();
    Texture texture=new Texture("badlogic.jpg");
    
    Image image=new Image(texture);
    image.addListener(new ClickListener(){
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    Gdx.app.log("TouchTest","Clicked on Image");
                }
            });
    
    stage.addActor(image);
    
    Actor actor=new Actor();   // this is your transparent layer 
    actor.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    stage.addActor(actor);
    
    // Popup should on the top or your actor(touch layer) 
    
    Image image1=new Image(texture);
    image.setPosition(100,100);
    stage.addActor(image1);
    
    Gdx.input.setInputProcessor(stage);    
    

    您还可以管理触摸层的可触摸性。

    actor.setTouchable(Touchable.disabled);  // when you want to disable touch on 
    

    在我的建议中,您应该使用Dialog 作为您的弹出窗口。 Dialog 是一个包含内容表的模式窗口。

    编辑

    从您的参考图像看来,您需要半透明层,所以在上面的代码中使用semiTL 而不是Actor

    Pixmap pixmap = new Pixmap(1,1, Pixmap.Format.RGBA8888);
    pixmap.setColor(Color.BLACK);
    pixmap.fillRectangle(0, 0, 1, 1);
    Texture texture1=new Texture(pixmap);
    pixmap.dispose();
    
    Image semiTL=new Image(texture1);
    semiTL.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    semiTL.getColor().a=.8f;
    stage.addActor(semiTL);
    

    【讨论】:

    • 谢谢。答案对于创建图层很有帮助。但实际上我的问题是关于从现有图像创建透明叠加层。可能是我的问题不清楚。现在进行编辑以使其清楚。
    【解决方案2】:

    正如 Abhishek Aryan 所说,您可以使用 Pixmap 为您的背景创建大小合适的基础。但是你可以像这样使用 sprite 和 sprite drawable 来代替使用图像

    private Drawable getStageBackground() {
        Pixmap stageBg = new Pixmap(
                Gdx.graphics.getWidth(),
                Gdx.graphics.getHeight(),
                Pixmap.Format.RGB888);
    
        Sprite image = new Sprite(new Texture(stageBg));
        image.setColor(1, 1, 1, 0.7f);
    
        return new SpriteDrawable(image);
    }
    

    并使用像这样的 stageBackground 属性在您的对话框窗口样式中传递此可绘制对象

    Window.WindowStyle style = new Window.WindowStyle();
    style.stageBackground = getStageBackground();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 2012-03-15
      • 1970-01-01
      • 2015-08-05
      • 2011-03-20
      • 2015-04-16
      • 1970-01-01
      相关资源
      最近更新 更多