1

I've been making a game to practice programming, and I am having trouble using the Jquery .click() function. I have two buttons in my code, the start button and the attack button. When I click the start button, the .click() function fires the code for the other button as well, which causes my main menu to freeze up and not draw the game screen. I've used separate id's for the buttons, but they both seem to recognize the click on the start button. I can't get it to work in JSFiddle, but all the code is there. Can someone please tell me how to use multiple buttons?

//start button
$('#startButton').click(function() {
    stage.state = "battle";
    stage.update();
})

//attack button
$('#attack').click(firstTurn());

//attack button code
function firstTurn() {
    console.log("firstTurn Fired");
    if(p1.speed > opp.speed){
        turn = 1;
    } else{
        turn = 0;
    }
    battle();
};

function battle(){
    var battling = 1;
    while(battling == 1) {
        if(turn == 0) {
            p1.health = p1.health-opp.attack;
            $("#textBox").append('<p>'+opp.name+' hit you for '+ opp.attack+' points.</p><br/>');
            draw();
            sleep(1000);
            console.log("attacked");
        } else{
            opp.health = opp.health-p1.attack;
            $('#textBox').append('<p> You hit '+opp.name+' for '+p1.attack+' points.</p><br/>');
            draw();
            sleep(1000);
        }
    }
};

https://jsfiddle.net/memersond/m3gvv8y6/

3
  • 1
    What exactly is sleep() supposed to do? ... oh OK I see it. That is absolutely, positively not the way to delay execution in JavaScript. That code will freeze the whole browser. Commented Aug 14, 2015 at 19:56
  • Can you recommend another way to accomplish the task? I'll assume that if you are correct, this is what is freezing my code Commented Aug 14, 2015 at 20:02
  • What you've got is a busy loop. You're just burning client CPU cycles for a given amount of time, using up batteries, making fans spin, etc. In JavaScript, you use timeouts to achieve that, because they burn no CPU at all (or an immeasurably small amount). You have to rearrange the way the logic works, however. In your case, it may be that you can just set a "don't do anything" flag that's cleared by a timeout 1 second later. Commented Aug 14, 2015 at 20:17

2 Answers 2

10
 $('#attack').click(firstTurn());

Should be:

$('#attack').click(firstTurn);

You want to pass the function as a reference, not have it executed immediately.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, that fixed the freezing at the main menu, but now it freezes when I click the attack button. Any Idea why?
I was trying to subtract the value opp.attack from p1.health and visa vera. Why do I need an array to do this?
@user2905256 You don't. Sorry, from the way it's written it looks like you were trying to access a complex object property.
So you don't know what's freezing me up? :/ I'm stumped. Whats weird is that if I step through the code in developer tools, it works perfectly, but when I let it run alone, it freezes up.
0
$('#attack').click(firstTurn());

This causes firstTurn() to be called when the listener is initiated, use one of the alternatives:

$('#attack').click(firstTurn );

$('#attack').click(function() {
    firstTurn()
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.