I am trying to write a simple game using two.js. I have a function which is called either from button click event and from game loop:
function endGame(gameLoop){
showStartGame();
gameLoop.pause();
$("#gameSquare svg:first").remove();
$("#startGame").show();
}
The line $("#startGame").show()
executes correctly only when called from the event, the rest works perfectly fine in both cases.
Click event handler:
$("#abandonGame").click(function(){
endGame(two);
gameLoopPaused = true;
gameStarted = false;
});
The call that doesn't work properly(this.update() is called inside game loop):
this.update = function(){
var computedVector = new Two.Vector(0,0);
screens.forEach(function(screen){
screen.update(speed);
screen.getRectangles().forEach(function(item){
computedVector.x = item.rect.translation.x;
computedVector.y = item.rect.translation.y + screen.getPosition().y;
if(computedVector.distanceTo(new Two.Vector(ship.getX(), ship.getY())) < latura + 20)
endGameEvent();
});