LibGDX – Actor类的父类是如何工作的?

huangapple go评论95阅读模式
英文:

LibGDX - How does the parent of an Actor class work?

问题

刚入门,所以这个问题可能有点愚蠢,抱歉。

我正在尝试为我正在开发的游戏制作一个菜单,我正在使用Scene2d。
我知道父类和子类是如何工作的,但我在尝试理解 Actor 类时遇到了困难。

我创建了一个名为 "Actor1" 的类,它继承自 Actor,覆盖了 draw 方法并简单地绘制了默认的 libgdx 图像:

public class Actor1 extends Actor{
    Texture tex = new Texture("badlogic.jpg");
    
    public void draw(Batch batch, float alphaParent){
        batch.draw(tex, 0, 0);
    }	
}

它工作得很好,但是我对 "alphaParent" 参数有点困惑。所以我进行了一些研究,阅读了大量关于 父类 的文章和问题,比如 GitHub 的 wiki:

如果将 parentAlpha 与该 actor 的 alpha 结合起来,子 actor 将受到父级的半透明影响。

传递给 draw 的 Batch 配置为在父级坐标中绘制,因此 0,0 是父级左下角。

根据我所知,我猜想 Actor 类是 Actor1 类的父类。但如果是这样,"父级的坐标" 或 "父级的半透明度" 是什么意思?它们在哪里初始化或定义?我从哪里可以更改它们?这些参数是来自 Actor 类还是由其他什么东西定义的,比如舞台(stage)?

英文:

Beginner here so this question might be a little dumb, my apologies.

I'm trying to make a menu for a game i'm working on, and i'm using Scene2d.
I know how parent and child classes work, but i got stuck trying to understand the Actor class.

I created a class called "Actor1" which extends from Actor, overrides the draw method and simply draws the default libgdx image:

public class Actor1 extends Actor{

	Texture tex = new Texture("badlogic.jpg");

	public void draw(Batch batch, float alphaParent){
        batch.draw(tex, 0, 0);
    }	
}

It works fine, but i was a little confused about the "alphaParent" parameter. So i did some research, and i've read tons of articles and questions talking about the parent class, like for example the github wiki:

> If the parentAlpha is combined with this actor's alpha as shown, child actors will be influenced by the parent's translucency.

>The Batch passed to draw is configured to draw in the parent's coordinates, so 0,0 is the bottom left corner of the parent.

Based on what i know, I guessed the Actor class is the Actor1 class' parent. But if so, what are the "parent's coordinates" or "parent's translucency"? Where are they initialized, or defined, and where can i change them? Do these parameters come from the Actor class or are these parameters defined by something else, like the stage?

答案1

得分: 1

不,当它涉及到父级时,并不是在谈论类层次结构。它是在谈论包含此角色的Group Actor实例。Group是Actor的子类,可以有Actor子元素。如果你直接将Actor添加到舞台(Stage),它的父级就是root Group,在坐标(0, 0)处,因此你的Actor位置是相对于世界的。

但你可以将Actor添加到一个Group中,这时它的位置是相对于其父级位置的。而alphaParent参数是父级的透明度级别。

draw()方法中,你必须在绘制之前始终设置Batch的颜色,因为不能保证它当前设置的颜色是什么。如果你希望Group的子元素能够与父元素一起逐渐淡入和淡出,你应该应用Actor的父级alpha。

一个典型的绘制方法如下:

    public void draw(Batch batch, float alphaParent){
        Color color = getColor();
		batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(tex, 0, 0);
    } 
英文:

No, when it talks about parents, it is not talking about class hierarchy. It's talking about the Group Actor instance that contains this Actor. Group is a subclass of Actor that can have Actor children. If you add an Actor directly to a Stage, its parent is the root Group, which sits at (0, 0), so your Actor's position is relative to the world.

But you can add an Actor to a Group, and then its position is relative to its parent's position. And the alphaParent parameter is the transparency level of the parent.

In the draw() method, you must always set the color of the Batch before you draw, because there is no guarantee of what color it is currently set at. And you should apply the Actor's parent alpha if you want the children of a Group to properly fade in and out along with the parent.

A typical draw method looks like this:

    public void draw(Batch batch, float alphaParent){
        Color color = getColor();
		batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(tex, 0, 0);
    } 

huangapple
  • 本文由 发表于 2020年9月9日 05:32:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63801842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定