Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

there is a bug #2

Open
kiyonlin opened this issue Mar 8, 2019 · 2 comments
Open

there is a bug #2

kiyonlin opened this issue Mar 8, 2019 · 2 comments

Comments

@kiyonlin
Copy link

kiyonlin commented Mar 8, 2019

Suppose now that the snake is moving to the "right". I quickly press the "up" and "left" directions keys in the speed time. The direction$ will push the "up" and "left", but withLatestFrom only Taking "Left", "Up" will be ignored, causing the snake to immediately go to the "Left" and collide with itself.

So both ticks$ and direction$ should make the snake moving.

I think change snake$ to be like this will resolve this bug:

let snake$: Observable<Array<Point2D>> = combineLatest(ticks$, direction$).pipe(
    withLatestFrom(snakeLength$, ([_, direction], snakeLength) => [direction, snakeLength]),
    scan(move, generateSnake()),
    share()
  );
@CHBaker
Copy link

CHBaker commented Apr 10, 2019

@kiyonlin I noticed the bug too, tried your fix. The problem is now the speed of turning is much faster than the speed of going straight, and the game gets choppy

@Vikasg7
Copy link

Vikasg7 commented Oct 1, 2021

The problem with both is:-
combineLatest - Next tick is not respecting keyevents resulting in multiple steps while making turns.
withLatestFrom - Key events are getting lost between ticks.

So both combineLatest and withLatestFrom are not ideal for this. I think, we need some observable that resets the timer/interval on each key event so that the next tick is generated after N milliseconds of previous keyevent/tick.

PS:- Wrote an operator to achieve the required behaviour. Link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants