diff --git a/core/src/main/java/io/github/com/quillraven/screen/GameScreen.java b/core/src/main/java/io/github/com/quillraven/screen/GameScreen.java index 8fc0186..6c341c0 100644 --- a/core/src/main/java/io/github/com/quillraven/screen/GameScreen.java +++ b/core/src/main/java/io/github/com/quillraven/screen/GameScreen.java @@ -107,6 +107,7 @@ public class GameScreen extends ScreenAdapter { @Override public void hide() { this.engine.removeAllEntities(); + this.stage.clear(); } @Override diff --git a/core/src/main/java/io/github/com/quillraven/ui/model/ViewModel.java b/core/src/main/java/io/github/com/quillraven/ui/model/ViewModel.java index 7627fd5..ef6a7e7 100644 --- a/core/src/main/java/io/github/com/quillraven/ui/model/ViewModel.java +++ b/core/src/main/java/io/github/com/quillraven/ui/model/ViewModel.java @@ -2,6 +2,7 @@ package io.github.com.quillraven.ui.model; import io.github.com.quillraven.GdxGame; +import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; public abstract class ViewModel { @@ -19,6 +20,12 @@ public abstract class ViewModel { ); } + public void clearPropertyChanges() { + for (PropertyChangeListener listener : this.propertyChangeSupport.getPropertyChangeListeners()) { + this.propertyChangeSupport.removePropertyChangeListener(listener); + } + } + @FunctionalInterface public interface OnPropertyChange { void onChange(T value); diff --git a/core/src/main/java/io/github/com/quillraven/ui/view/GameView.java b/core/src/main/java/io/github/com/quillraven/ui/view/GameView.java index c1c4a71..2f01bc8 100644 --- a/core/src/main/java/io/github/com/quillraven/ui/view/GameView.java +++ b/core/src/main/java/io/github/com/quillraven/ui/view/GameView.java @@ -22,7 +22,10 @@ public class GameView extends View { this.lifeGroup = findActor("lifeGroup"); updateLife(viewModel.getLifePoints()); + } + @Override + protected void setupPropertyChanges() { viewModel.onPropertyChange(GameViewModel.LIFE_POINTS, Integer.class, this::updateLife); viewModel.onPropertyChange(GameViewModel.PLAYER_DAMAGE, Map.Entry.class, this::showDamage); } diff --git a/core/src/main/java/io/github/com/quillraven/ui/view/View.java b/core/src/main/java/io/github/com/quillraven/ui/view/View.java index 1cb7300..ac9197c 100644 --- a/core/src/main/java/io/github/com/quillraven/ui/view/View.java +++ b/core/src/main/java/io/github/com/quillraven/ui/view/View.java @@ -28,8 +28,21 @@ public abstract class View extends Table implements EventLi setupUI(); } + @Override + protected void setStage(Stage stage) { + super.setStage(stage); + if (stage == null) { + viewModel.clearPropertyChanges(); + } else { + setupPropertyChanges(); + } + } + protected abstract void setupUI(); + protected void setupPropertyChanges() { + } + public void onLeft() { }