Code help… (to make my topic longer)


#1

hello, im kinda a noob at the field of coding of javascript, css, html, and jquery. recently i’ve been trying to get a pong paddle to move and for some reason it won’t. this is what i have in jquery/javascript:
(document).ready(function() { (document).keydown(function(key) {
('start_button').click(function() { switch(parseInt(key.which,10)) { case 37: (‘paddle’).animate({left: “-=10px”}, ‘fast’);
break;
case 68:
$(‘paddle’).animate({right: “-=10px”}, ‘fast’);
break;
default:
break;
}
});
});
});

except i have a bunch of tabs, it just won’t let me do them. i have my

as well as my
START
in html. i have more than that, but that’s what matters here. i have all my styling in css. but for some reason, i just won’t do anything. i’m not sure if it’s not sensing the start button or if it is and just isn’t moving. or maybe both. any ideas?

#2

I’ll let the experts help… I’m only good(ish) at Python… :tongue:


#3

I’m with Alfie… only have any (small small) experience with Python but…

Does your switch have a ‘(’ going the wrong way? switch(parseInt(key.which,10() instead of what it is now? A lot of the time code goes wrong with a minor typo or out of place punctuation…


#4

EDIT: I’m rewriting my explanation from earlier because I had just woken up and I don’t think it made much sense before.

The current problem with your program is that your not actually instantiating listeners for keys 37 and 68. What your doing right now is telling the program to take whatever the value of “key” is at the moment that you press the start button and then decide which way to move. Problem is key doesn’t have a value at the button press. So you have to create individual listeners

As a visual ease you could create two buttons that said left and right to with the animations inside there “click” event.
To add a listener for keypress’s with JQuery though you could just use .bind(“event”, function() )

Oh and a bit on moving forward in JS. Personally, and others definitely may disagree, but i think you should learn to do the basics without necessarily using JQuery in the beginning to get more of a basic understanding of the language. Also, you should look into programming practices such as this:
http://markdalgleish.com/2011/03/self-executing-anonymous-functions/

which limit global variable use for larger projects for games and the like.


#5

#include < iostream>

using namespace std;

int main()
{
cout << “I really should learn some javascript” << endl;
return 0;
}