Filling out the cases!


#1

Zues1963 :rage:
Here is my code,
$(document).ready(function() {
$(document).keydown(function(key) {
switch(parseInt(key.which,10)) {

case 37:
$('img').animate({left: " -=10px "}, 'fast');
break;
case 38:
$('img').animate({top: " -=10px "}, 'fast');
break;
case 39:
$('img').animate({left: " +=10px "}, 'fast');
break;
case 40:
$('img').animate({top: " +=10px "}, 'fast');
break;
};
});
});

When i click and arrow right etc: Mario moves as he should, but i keep getting oops did you move right etc: it is driving me a little bit nuts, any suggestions please!!!


#2

Just remove the empty spaces before and after -/+=10px like this: "-=10px".

case 37:
 $('img').animate({left: "-=10px"}, 'fast');
 break;

Do this for every case


#3

Thankyou Juliet, i have taken out the spaces as you suggested but still no joy, if you have any other ideas they would be greatly appreciated.


#4

When I copy and paste your code in script.js I have the same error. But when I remove the empty spaces from the strings in every case, everything works perfect. Can you give me a screenshot of your code?


#5

Thankyou for your help, $(document).ready(function() {
$(document).keydown(function(key) {
switch(parseInt(key.which,10)) {
case 37:
$('img').animate({left:"-=10px"}, 'fast');
break;
case 38:
$('img').animate({top:"-=10px"}, 'fast');
break;
case 39:
$('img').animate({left:"+=10px"}, 'fast');
break;
case 40:
$('img').animate({top:"+=10px"}, 'fast');
break;
}
});
});
I have tried it with spaces and without and every which way possible and i am now going slytly mad, thankyou for your patience.


#6

The code is ok. There is no problem when I test it. Try to refresh or to delete the code and paste it again. I have no idea why you have error message.


#7

Hi @zues1963,

If the code is working for @julietaatanasova, and her code is exactly the same as yours, you might need to refresh the page or switch browsers to pass :)
Sometimes caching or a certain feature not being available in certain browsers can cause issues like this, where code works for one person but not for the other :smile:


#8

Thankyou Juliet and Zystvan,
I changed my browser to chrome and joy at last, thankyou both for your time and patience.


#9

Hi there!
I have a question, why when I use: right, left, up, down, the image just move to the left? and do not recognize the other patters.

Thank u for your answer!

ANA


#10

I had the same problem. It finally worked when I used my quotes exactly as Case 37.
single quotes on 'img' and 'fast' double quotes on "+=10px"


#12

For anyone who like me struggled with getting the code to work - even when it was the right code, I just left it for the day came back to it, reset the code and used this:

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

Worked after using the same code again - think it's just a little buggy...