-4

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();
});
John Lion
  • 5
  • 6

2 Answers2

0

try wrapping it in document.ready

$(document).ready(function() {
    $("#startGame").show();
});

You can't call show() on an object before it exists

Dotan
  • 6,602
  • 10
  • 34
  • 47
0

The problem was with the parent of $("#startGame"), it was hidden as well. It worked with event because by the time I was hitting the $("#abandonGame") button It was displayed on the screen.

John Lion
  • 5
  • 6