【问题标题】:Extending Group in LibGdx在 LibGdx 中扩展组
【发布时间】:2017-03-10 05:28:35
【问题描述】:

我在项目的播放屏幕中创建了一个组。该组包含许多图像和按钮作为演员。

Group newGroup = new Group();

在show()里面

newGroup.addActor(bg);
stage.addActor(newGroup);

这很好用。但是我想在组中添加更多的东西。另外我还需要创建更多的组。所以我想我可以创建扩展组的新类。其实我想用模块化的方式来创建这些组。

public class newGroup extends Group {
 //want to add actors here-buttons,images and other scene2d elements
}

public class actor extends Actor{

}

我有类似的想法要做,但我不知道如何有效地做到这一点,以便我可以移动和缩放组项目并在播放屏幕中访问。 请告诉我如何正确扩展 LibGdx 中的组并在播放屏幕中访问它。

【问题讨论】:

    标签: libgdx scene2d


    【解决方案1】:

    因此,您需要执行与示例中相同的操作,但要在扩展类中执行。也许我的简短示例会对您有所帮助。

    以下示例中有 SKIN 变量。我还没有展示如何加载皮肤。阅读 Scene2D.ui 以了解 SKIN 的含义。

    第一个例子(没有扩展):

    Group group = new Group();
    TextButton b = new TextButton(SKIN, "Press Me");
    Label l = new Label(SKIN, "Some text");
    b.setPosition(0, 0); //in groups coordinates
    l.setPosition(0, 100);
    group.addActor(l);
    group.addActor(b);
    
    stage.addActor(group);
    

    你可以通过扩展来做同样的事情:

    public class MyGroup extends Group {
         private TextButton b;
         private Label l;
    
         public MyGroup() {
              b = new TextButton(SKIN, "Press me");
              l = new Label(SKIN, "Some text");
              b.setPosition(0, 0); //in coordinates of group
              l.setPosition(0, 100);
              //now we will add button and label to the our extended group.
              this.addActor(b);
              this.addActor(l);
              //"this" is unnecessary. I write this because it 
              //may be more clear for you to understand the code.
              //"this" is our extended group and we add actors to it.
         }
    
    }
    

    所以,现在您可以创建我们的新组并将其添加到舞台:

    MyGroup myGroup = new MyGroup();
    myGroup.setPosition(200, 200); //also in `stage` coords.
    stage.addActor(myGroup);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 2011-03-26
      相关资源
      最近更新 更多