11/12 | Mario runs, but doesn't work


#1

Hey,
I have absolutely no idea what I am doing wrong. I constantly get the same error:

Oops, try again. Did you call the .animate() function on your $('img') for each of the three additional cases?

My code:

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

What is wrong with my code?
Please help! Thank youu :slight_smile:


#4

Your code is wrong because you put two 'left's in your code. Look at my code:

$(document).ready(function() {

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

Can you see the difference?


#5

See your other topic for more on this.


#6

@brent20020,

you can refter to my source code.
Recommend that you change fist $('img') to $("img"), then refresh webpage, and click " save & submit ". It may help. :slight_smile:

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


#7

@mtf is right. {right: "+=10px"} is wrong.


#8

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