简体   繁体   English

管理Scene2d,libgdx

[英]Managing Scene2d, libgdx

I'm making a simple point&click game using libGdx and their Scene2d. 我正在使用libGdx和他们的Scene2d制作一个简单的点击游戏。 Now, when I enter a location my Stage is cleared and new Actors are beeing attached. 现在,当我进入一个位置时,我的舞台将被清除,新的演员将被蜂拥而至。 It doesnt feel right and its not efficient. 它感觉不正确,效率不高。

Can I make all Actors at the begining (except backgrounds, I will load them when entering a location), add them to Stage and associate them with locations, so the Stage would know witch to draw? 我可以在开始时制作所有Actor(背景除外,在进入位置时会加载它们),将它们添加到Stage中并将它们与位置关联,以便Stage会知道要绘制女巫吗?

My only idea was to check that in draw and act methods of every actor, but that would mean houndreds of checks in a loop. 我唯一的想法是在每个演员的绘画和表演方法中进行检查,但这意味着循环中要进行大量检查。 Maybe Scene2d got something to help me out? 也许Scene2d可以帮助我吗? Or maybe there is another way to do it? 或者,也许还有另一种方法?

My only idea was to check that in draw and act methods of every actor, but that would mean houndreds of checks in a loop. 我唯一的想法是在每个演员的绘画和表演方法中进行检查,但这意味着循环中要进行大量检查。

Yes, that will be inneficient, and above all, a hell to maintain. 是的,那将是毫无用处的,最重要的是要维持一个地狱。

Now, when I enter a location my Stage is cleared and new Actors are beeing attached. 现在,当我进入一个位置时,我的舞台将被清除,新的演员将被蜂拥而至。

This is where your problem is, you're not using scene2D as it should be IMHO. 这就是您的问题所在,您没有使用Scene2D,因为它应该是恕我直言。 I hope you're up for an intense architecture & code refactoring session. 希望您能参加激烈的架构和代码重构会议。

When entering a new location, you should be entering a new stage. 输入位置时,您应该进入一个阶段。 So first, you should have several stages : 所以首先,您应该有几个阶段:

class MainMenu extends Stage {
    public MainMenu(){
        // Add buttons to play or quit the game
    }
}

class PointNClickStage extends Stage {
    // Add stuff common to all point'n click stages such as an inventory display
}

class Island extends PointNClickStage {
    public Island (){
        // Add some palm trees and an hidden chest
    }
}

class PirateShip extends PointNClickStage {
    public PirateShip(){
        // Add some pirates and their ship
    }
} 

... etc

Then in your application listener, you should implement a way to change the current stage being rendered. 然后,在应用程序侦听器中,您应该实现一种方法来更改正在渲染的当前阶段。 Conceptually, this is often called a "scene/stage director". 从概念上讲,这通常被称为“场景/舞台导演”。 Some scene-based frameworks, such as Cocos2D provides their own scene director, but libgdx doesn't currently. 一些基于场景的框架(例如Cocos2D)提供了自己的场景导向器,但libgdx当前不提供。 So, you have to implement this mechanism by yourself and here is a very basic example to help you get the gist of it : 因此,您必须自己实现此机制,这是一个非常基本的示例,可以帮助您了解要点:

public MyApp extends ApplicationAdapter { 

    private Stage currentStage;

    private static MyApp instance;

    // ...

    @Override
    public void create () {
        instance = this;
        MyApp.setStage(new MainMenu()); // The game begins in the main menu
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0.15f, 0.1f, 0.15f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        currentStage.act();
        currentStage.draw();
    }

    public static void setStage(Stage stage){
        instance.currentStage = stage;
        Gdx.input.setInputProcessor(stage); // Important ;)
    }

    // ...

}

So that to change the location (current stage) you will only have to do : 这样只需更改位置(当前阶段)即可:

MyApp.setStage(new PirateShip())

Then, if you don't want to recreate a new stage every time you change your location, you may initialize and keep a reference on them somewhere so that you will be able to change the location like that for example. 然后,如果您不想在每次更改位置时都重新创建新舞台,则可以初始化它们并在其上保留引用,以便例如能够更改位置。

MyApp.setStage(some_list_containing_initialized_stage.get(id))


Alternatively, you may also look into this libgdx extension that provides scene2d utils classes such as a scene director, and transitions that may be useful for you if you don't want to reinvent the wheel later. 另外,您也可以研究此libgdx 扩展 ,该扩展提供了scene2d utils类(例如场景导演),以及如果您以后不想重新发明轮子的过渡可能对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM