Jump to content

  • Log In with Google      Sign In   
  • Create Account


javascript buttons help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 19 January 2013 - 10:20 AM

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.



Sponsor:

#2 EmployeeNumber8   Crossbones+   -  Reputation: 1103

Like
1Likes
Like

Posted 19 January 2013 - 11:12 AM

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.



#3 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 19 January 2013 - 11:40 AM

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.



#4 rip-off   Moderators   -  Reputation: 8113

Like
0Likes
Like

Posted 19 January 2013 - 12:14 PM

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?



#5 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 19 January 2013 - 12:33 PM

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();

};

};
});


#6 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 19 January 2013 - 01:14 PM

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



#7 rip-off   Moderators   -  Reputation: 8113

Like
1Likes
Like

Posted 19 January 2013 - 04:35 PM

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

infiniteButton,hide();

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

 
</p><div>var x = 3;</div>
<div>var y = 5;</div>
<div>var z = (x, y);</div>
<div>console.log(z);</div>
 

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:

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


#8 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 19 January 2013 - 05:01 PM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS