简体   繁体   English

策略设计模式-停止方法

[英]Strategy design pattern - stopping the method

I am working on a game in Java. 我正在用Java开发游戏。 I use libGDX as library. 我使用libGDX作为库。 I am apparently using the Strategy design pattern. 我显然正在使用“ 策略”设计模式。

Here is my SpawnMapScreen class. 这是我的SpawnMapScreen类。 It extends BaseScreen. 它扩展了BaseScreen。

public void render(float delta) {
    super.render(delta);

    npcTalking.update(Gdx.graphics.getDeltaTime());
    npcRunning.update(Gdx.graphics.getDeltaTime());

    renderer.getSpriteBatch().begin();
    WorldVars.player.draw(renderer.getSpriteBatch());
    npcTalking.draw(renderer.getSpriteBatch());
    npcRunning.draw(renderer.getSpriteBatch());
    renderer.getSpriteBatch().end();
}

Here is my BaseScreen class. 这是我的BaseScreen类。 It implements Screen. 它实现Screen。

public void render(float delta) {
    Gdx.graphics.setTitle("AHH V. " + version + " || Running at a mighty " + Gdx.graphics.getFramesPerSecond() + "fps"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    WorldVars.player.update(Gdx.graphics.getDeltaTime());

    renderer.setView(camera);
    renderer.render();
    camera.position.set(WorldVars.player.getX() + WorldVars.player.getWidth() / 2, WorldVars.player.getY() + WorldVars.player.getHeight() / 2, 0);
    camera.update();

    spriteBatch.begin();
    font.draw(spriteBatch, version, 0, 100);
    spriteBatch.end();
}

Here is my problem. 这是我的问题。 In my BaseScreen class, I want to check if the escape key is pressed. 在我的BaseScreen类中,我想检查是否按下了转义键。 If it is, I want to switch screens. 如果是,我想切换屏幕。 The thing is, when I call my game instance's setScreen(Screen screen) method, it changes screen, but continues the process in the render method. 问题是,当我调用游戏实例的setScreen(Screen screen)方法时,它会更改屏幕,但会继续执行render方法中的过程。 It goes like this: 它是这样的:

  1. SpawnMapScreen's render(float delta) method is called SpawnMapScreen的render(float delta)方法被称为
  2. In the SpawnMapScreen's render(float delta) method, BaseScreen's render(float delta) method is called 在SpawnMapScreen的render(float delta)方法中,调用BaseScreen的render(float delta)方法
  3. In BaseScreen, it checks if the escape key is pressed, if yes, it changes screen 在BaseScreen中,它检查是否按了退出键,如果是,则更改屏幕
  4. Continue with SpawnMapScreen's render(float delta) method. 继续使用SpawnMapScreen的render(float delta)方法。

How it should be working: 应该如何运作:

  1. SpawnMapScreen's render(float delta) method is called SpawnMapScreen的render(float delta)方法被称为
  2. In the SpawnMapScreen's render(float delta) method, BaseScreen's render(float delta) method is called 在SpawnMapScreen的render(float delta)方法中,调用BaseScreen的render(float delta)方法
  3. In BaseScreen, it checks if the escape key is pressed, if yes, it changes screen 在BaseScreen中,它检查是否按了退出键,如果是,则更改屏幕
  4. Do not continue de SpawnMapScreen's render(float delta) method 不要继续de SpawnMapScreen的render(float delta)方法

Is there a possible way to do like so? 有没有可能这样做的方法?

You just need a global (as in member of the class that this function is in) boolean flag to check in SpawnMapScreen to determine whether to do anything or not. 您只需要一个全局(如该函数所在的类的成员)布尔值标志即可签入SpawnMapScreen,以确定是否执行任何操作。 Or it could check the current screen member for this. 或者它可以为此检查当前屏幕成员。

I changed the way I work around input-processing. 我更改了输入处理的工作方式。 In fact, I opted for an InputProcessor class (called InputSystem), so that I can treat all my inputs in the same place. 实际上,我选择了InputProcessor类(称为InputSystem),以便可以在同一位置处理所有输入。 Each time new BaseScreen is created, I also call Gdx.input.setInputProcessor(new InputSystem) 每次创建新的BaseScreen时,我也会调用Gdx.input.setInputProcessor(new InputSystem)

I don't see this as a case of using Strategy. 我不认为这是使用策略的情况。 The whole idea behind Strategy is a pluggable way of doing something. 战略背后的整个想法是一种可插拔的做事方式。 For instance, LayoutManagers in Swing or a sort (eg Bubble vs. Quicksort or whatever.. If you have a bunch of conditional logic, that should probably either outside the Strategy implementation. 例如,Swing中的LayoutManagers或某种排序(例如Bubble与Quicksort等)。如果您有很多条件逻辑,则可能不在Strategy实施之外。

Actually, in reading through it again, I have a concrete suggestion: use a State Pattern. 实际上,在再次阅读它时,我有一个具体的建议:使用状态模式。 What you would do is have 2 state handlers and then you listen on the escape key and when it has been pressed, you swap in the EscapeHandler, which implements the variant behavior before calling for the layout. 您要做的是有2个状态处理程序,然后侦听转义键,当按下该键时,您将交换EscapeHandler,该EscapeHandler在调用布局之前实现了变体行为。

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

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