Sora
Sora

Reputation: 2551

an infinite loop inside a javascript game

my jsfiddle:http://jsfiddle.net/Qfe6L/2/

 $(window).keypress(function (e) {
            if (e.which == 32) {
                CreateChuriken();
                $(".Shuriken").animate({ left: '+=300px' }, 'slow');
            }
        });

as you can see if you hit on start button and keep on pressing space in a fast way the counter act weird and the amount of enemies created start to be massive the game enter an infinite loop somewhere but i can't find where can anyone help me please

Upvotes: 0

Views: 184

Answers (2)

Doug Moscrop
Doug Moscrop

Reputation: 4544

Each time the start button is clicked, you are creating a new timeout, which sets up a 60 second window of spawning enemies and increments the counter. As it has been stated, when you press space to attack, that can also press the start button if the button has focus.

Disable the button when you start things:

    $("#Start").click(function () {
        $('#Start').attr('disabled', 'disabled');

        startTimer();
        ReleaseEnemies()
    });

This will avoid what effectively is running multiple 'copies' of your game logic.

Upvotes: 1

kkarsko
kkarsko

Reputation: 103

If you press space while a button has focus, the browser counts it as a click.
In my experience multiple hits to the start button cause the problem you described.
Try to eliminate further clicks after the initial (disable it, maybe).

Upvotes: 0

Related Questions