javascript buttons help

Started by
6 comments, last by JackBid 11 years, 3 months ago

In my html5/javascript project I have a loop that constantly refreshes and moves shapes across the screen. I am trying to make buttons to begin the game. To this I made a html link then created it into a variable in the javascript. I am using a ".click(function())" however it seems that once I click on the button, it runs the .click function, but only for a short period of time before automatically going back to how it previously ran.

Advertisement

I bet there is some issue in the the game logic somewhere, causing the game to end. It doesn't sound like a button issue.

Maybe open up the console in Chrome/Firebug and manually start the game, and see what happens. Use the debugger and step through your code.

I bet there is some issue in the the game logic somewhere, causing the game to end. It doesn't sound like a button issue.

Maybe open up the console in Chrome/Firebug and manually start the game, and see what happens. Use the debugger and step through your code.

I am coding it in sublime text (basically a notepad style application) then saving it all to an html file which I run in chrome. I do not understand how to open up the console or use the debugger. I just type some code, save it and then refresh the web page that it is on.

Chrome comes with "developer tools". In Firefox, if you install the "Firebug" extension you can get similar tools (I believe Firefox has some built-in tools, but I believe Firebug is popular for a reason).

As for your problem, it sounds like you have a logic error in your game loop. Can you post some of your code?

Chrome comes with "developer tools". In Firefox, if you install the "Firebug" extension you can get similar tools (I believe Firefox has some built-in tools, but I believe Firebug is popular for a reason).

As for your problem, it sounds like you have a logic error in your game loop. Can you post some of your code?

sure, just to warn you I am quite new to programming so for a lot of stuff I have just done it in a more simplistic, basic way that i can understand more easily.



var playArcade = false;

//convert html link item to a variable
var arcadeButton = $("#Arcade");
var infiniteButton = $("#Infinite");
var helpAndAboutButton = $("#HelpAndAbout");

arcadeButton.click(function(){
playArcade = true;
$(this).hide();
infiniteButton,hide();
helpAndAboutButton.hide();
});

infiniteButton.click(function(){
$(this).hide()
arcadeButton.hide();
helpAndAboutButton.hide();
});

helpAndAboutButton.click(function(){
$(this).hide()
arcadeButton.hide();
hinfiniteButton.hide();
});

function GameLoop(){

//lots of other code i cut out

context.clearRect(0, 0, canvasWidth, canvasHeight);
context.fillStyle="rgb(255, 151, 0)";
context.fillRect(x, y, xWidth, yHeight);

if(playArcade){
setTimeout(GameLoop, animate, collect, 33);
animate();
};
};

GameLoop();

//animate function for drawing shapes
function animate(){

//lots of other code that i cut out

var gradient = context.createLinearGradient(0, 0, 0, canvasWidth);
gradient.addColorStop(0, "rgb(63, 63, 63)");
gradient.addColorStop(1, "rgb(217, 217, 217)");
//adds a gradient
context.fillStyle = gradient;
context.fillRect(tmpShape.x2, tmpShape.y2, tmpShape.width, tmpShape.height);

//draw the x and y coords to the screen
context.fillStyle = "rgb(0, 0, 0)";
context.font = "bold 14px sans-serif";
context.fillText("(x: " + x, 700, 20);
context.fillText("y: " + y + ")", 750, 20);

context.fillStyle = "rgb(0, 0, 0)";
context.font = "bold 14px sans-serif";
context.fillText("time left: " + timeLeft, 360, 20);

collect();

};

};

function collect(){

//lots of other code i cut out

context.fillStyle = "rgb(252, 232, 73)";
context.beginPath();
context.arc(tmpCollect.x3, tmpCollect.y3, tmpCollect.radius, 0, Math.PI*2, false);
context.closePath();
context.fill();

};

};
});

I used the console and I get the error message: "uncaught referenceError: hide is not defined"

Your "arcadeButton" click handler contains a bug on this line:

infiniteButton,hide();

This syntax is legal,it is the "comma operator":

var x = 3;
var y = 5;
var z = (x, y);
console.log(z);

Z will be 5.

You probably meant to write:

infiniteButton.hide();

Also, one of your other click handlers contains another typo: "hinfiniteButton".

However, we can note that whatever button is clicked, there is a common action: hiding each button. Your code contains extra bugs because you write code to hide the buttons three times, and each is slightly different. We can reduce bugs by writing the code that hides all the buttons once, and then bind that code to each button:

var playArcade = false;
var arcadeButton = $("#Arcade");
var infiniteButton = $("#Infinite");
var helpAndAboutButton = $("#HelpAndAbout");
var hideAllTheButtons = function() {
arcadeButton.hide();
infiniteButton.hide();
helpAndAboutButton.hide();
};
arcadeButton.click(hideAllTheButtons);
infiniteButton.click(hideAllTheButtons);
helpAndAboutButton.click(hideAllTheButtons);
You can still bind an additional handler to the "playArcade" variable:
arcadeButton.click(function() {
playArcade = true;
});

Your "arcadeButton" click handler contains a bug on this line:


infiniteButton,hide();

This syntax is legal,it is the "comma operator":


 
var x = 3;
var y = 5;
var z = (x, y);
console.log(z);
 

Z will be 5.

You probably meant to write:


infiniteButton.hide();

Also, one of your other click handlers contains another typo: "hinfiniteButton".

However, we can note that whatever button is clicked, there is a common action: hiding each button. Your code contains extra bugs because you write code to hide the buttons three times, and each is slightly different. We can reduce bugs by writing the code that hides all the buttons once, and then bind that code to each button:


 
var playArcade = false;
        
var arcadeButton = $("#Arcade");
var infiniteButton = $("#Infinite");
var helpAndAboutButton = $("#HelpAndAbout");
 
var hideAllTheButtons = function() {
    arcadeButton.hide();
    infiniteButton.hide();
    helpAndAboutButton.hide();
};
 
arcadeButton.click(hideAllTheButtons);
infiniteButton.click(hideAllTheButtons);
helpAndAboutButton.click(hideAllTheButtons);
You can still bind an additional handler to the "playArcade" variable:

arcadeButton.click(function() {
    playArcade = true;
});
 

Thanks! Sorry about the typos, I was quickly editing the code for the site, not all of them were in the actual code!

This topic is closed to new replies.

Advertisement