K
K
kserg9032019-09-06 13:36:20
Angular
kserg903, 2019-09-06 13:36:20

How to stop an event stream?

There is a small application in which object1 moves across the screen. The direction of movement is determined by the pressed key. At the same time, another object (object2) moves across the screen, the direction of which is determined every second by a random number generator.
DEMO
The problem is that after the user presses a key, object1 moves in space (as intended), but in the following moments it continues to move in space (as not intended), although the user no longer presses the key.
Please help me fix this. That is, in other words, I need object1 to move only when the user presses a key.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikola-BLR, 2019-09-08
@kserg903

https://angular-rxjs6-3ge5rx.stackblitz.io
combineLatest is not suitable for your case. Already in the title you can understand the whole catch of the situation. It combines the latest values ​​for both Observables. Changing enemyMove$ every 200ms causes your subscribe to fire every 200ms and get both the random value for enemyMove$ and the last key pressed for playerMove$. Even if we don't press the key anymore, its last value goes into next.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question