Help please find the reason why the game hangs. This is reflected in the inability to move the player's ship by more than 100px in any direction.

The main algorithm of the game is as follows: For each entity, a separate view has been created:

APP.SpaceView - содержит основную логику игры APP.InformerView - табличка, отображающая количество снарядов, жизненные силы, счёт и т.д. APP.InfolineView - бегущая строка с разной информацией APP.FieldView - игровая область(в ней летают корабли протвника, корабль игрока и звёзды) APP.PlayerShipView - корабль игрока APP.PlayerRocketView - снаряды игрока APP.StarView - звёзды на фоне 

Most of the logic is in APP.SpaceView and is organized as follows: When initializing this view, the main game loop is started, in which coordinates and conditions for each game object are calculated:

 setInterval(function() { self._makeMoves(self) }, 100); 

in _makeMoves () are calculated: the movements of the stars (enumeration of the entire collection), the movement of the player’s projectiles tracked keystrokes responsible for moving the player’s ship and shooting

That is, every 0.1 seconds, a fairly large number of operations are calculated. But I do not think that the reason for the brakes of the game in this. First of all, because there are not so many calculations (8GB RAM). Secondly, because I made a similar game in the same way (on pure js) and also with a considerable amount of object and there was not even a hint of a hang

Please help me find the cause of the hang

http://fh79272k.bget.ru/files/works/space/1/

https://github.com/zlodiak/space-1

  • one
    Sorry, but the game on divs in 2016 is some kind of perversion. Discover already Canvas and requestAnimationFrame and make normal games. And if you are too lazy to write from scratch, use any of the game engines . - hindmost

1 answer 1

I understand the problem in the file widgetsViews.js .
In the fourth line, you hang the listener to change the model in the initialize method.

Every time when the model changes (the ship moves) the listener is re-added and subsequently a large recalculation of the data takes place, which inhibits the game.

You can try the listenToOnce method instead of listenTo . After the first triggering, the event is deleted, but not added as it does with listenTo .

After ten taps (moving the ship down) to console it . This is with listenTo .
After ten taps (moving the ship down) in the console now this . This is with listenToOnce .

After these manipulations, the game ceased to slow down when moving.

Or you can check for a listener. Nope - added. Otherwise we do nothing.

PS Backbone.js see for the first time. The solution may not be the best.