Pressing key switch


This code is not efficient. What i am missing is the key up down, etc. Need help coming up with solution to fix this, which was in the last lesson. Can’t go back in the lesson.

$(document).ready(function() {
    $(document).keydown(function(key) {
        switch(parseInt(key.which,10)) {
			// Left arrow key pressed
			case 37:
				$('img').animate({left: "-=10px"}, 'fast');
			// Up Arrow Pressed
			case 38:
                $('img').animate({right: '+=10px'}, 'fast');
			// Right Arrow Pressed
			case 39:
				$('img').animate({up: '+=10px'}, 'fast');
			// Down Arrow Pressed
			case 40:
				$('img').animate({down: '+=10px'}, 'fast');


animate does not have a sense of direction, but relative offsets from the top and left boundaries.

top: '-=10px'  => move up
top: '+=10px'  => move down

left: '-=10px'  => move left
left: '+=10px'  => move right


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.