Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How To Make a GIF Move with Arrow Keys?

AndyMack

New Coder
I made a code, trying to make a little character run around my screen, but I cannot figure out where I went wrong. HELP!

When I ran the code, the GIFs all went in different directions, but i cannot make them stay together. I cannot figure out how to make the GIFs only work when that individual button is pressed.


HTML:
<!doctype html>
<html lang="en">

<head>
    <title>The Forest of Forgotten Souls</title>
    <script type="text/javascript" src="script.js"></script>
    <link rel="stylesheet" href="style.css" />
</head>

<body onkeydown="getKeyAndMove(event)">
  <div id="game"></div>
    <img id="image1" src="walkforward.gif"/>
    <img id="image2" src="walkleft.gif"/>
    <img id="image3" src="walkright.gif"/>
    <img id="image4" src="walkbackward.gif"/>
</body>

</html>



JavaScript:
var image1 = null;
var image2 = null;
var image3 = null;
var image4 = null;
function init() {
  image1 = document.getElementById("image1");
  image1.style.position = "absolute";
  image1.style.left = "250px";
  image1.style.top = "250px";
  image2 = document.getElementById("image2");
  image2.style.position = "absolute";
  image2.style.left = "250px";
  image2.style.top = "250px";
  image3 = document.getElementById("image3");
  image3.style.position = "absolute";
  image3.style.left = "250px";
  image3.style.top = "250px";
  image4 = document.getElementById("image4");
  image4.style.position = "absolute";
  image4.style.left = "250px";
  image4.style.top = "250px";
}
function getKeyAndMove(e) {
  var key_code = e.which || e.keyCode;
  switch (key_code) {
    case 37: //left arrow key
      moveLeft();
      break;
    case 38: //Up arrow key
      moveUp();
      break;
    case 39: //right arrow key
      moveRight();
      break;
    case 40: //down arrow key
      moveDown();
      break;
  }
}
function moveLeft() {
  image2.style.left = parseInt(image2.style.left) - 5 + "px";
}
function moveUp() {
  image1.style.top = parseInt(image1.style.top) - 5 + "px";
}
function moveRight() {
  image3.style.left = parseInt(image3.style.left) + 5 + "px";
}
function moveDown() {
  image4.style.top = parseInt(image4.style.top) + 5 + "px";
}
window.onload = init;
 
Last edited:
Hi.

You need to store the x and y locations properly. After keypress, reveal only that image which should be pressed and hide rest of them..

Code:
If movement then
  solve direction where
  update x & y accordingly
  by direction, hide other images and only show the correct one
End if
 
Try using one div with various backgrounds instead:

HTML:
<!doctype html>
<html lang="en">

<head>
    <title>The Forest of Forgotten Souls</title>
    <base href="https://cyberstatic.net/images/smilies/">
    <style>
    #player{
        width: 40px;
        height: 40px;
        border: 1px solid #040;
        border-radius: 50%;
        background: #abc url('eek2.gif') center center no-repeat;
        position: absolute;
        top: 250px;
        left: 250px;
        }
    </style>
</head>

<body>
  <div id="game"></div>
    <div id="player"></div>
</body>
<script>
/*
ag.gif -- leftward
rtfm.gif -- rightward
huh.gif -- upward
rofl.gif -- downward
*/
const pic = document.querySelector('#player');

document.body.addEventListener('keydown', (e) => {
 
  const k = e.which || e.keyCode,
        L = pic.offsetLeft + pic.scrollLeft,
        T = pic.offsetTop + pic.scrollTop;
 
  switch ( k ) {
    case 37: // left arrow key
      pic.style.left = L - 5 + 'px';
      pic.style.backgroundImage = "url('ag.gif')";
      break;
    case 38: // up arrow key
      pic.style.top = T - 5 + 'px';
      pic.style.backgroundImage = "url('huh.gif')";
      break;
    case 39: // right arrow key
      pic.style.left = L + 5 + 'px';
      pic.style.backgroundImage = "url('rtfm.gif')";
      break;
    case 40: // down arrow key
      pic.style.top = T + 5 + 'px';
      pic.style.backgroundImage = "url('rofl.gif')";
      break;
  }
});

document.body.addEventListener('keyup', () => pic.style.backgroundImage = "url('eek2.gif')");
</script>
</html>
 
JavaScript:
document.body.addEventListener('keydown', (e) => {
 
  const k = e.which || e.keyCode;
 
  if( ![37, 38, 39, 40].includes(k) ) return;
 
  const L = pic.offsetLeft + pic.scrollLeft,
        T = pic.offsetTop + pic.scrollTop,
        o = !(k % 2) ?
                k == 38 ?
                    {'top' : T - 5 + 'px', 'backgroundImage': "url('huh.gif')"}:
                        {'top' : T + 5 + 'px', 'backgroundImage': "url('rofl.gif')"}
                            :
                                k == 37 ?
                                    {'left' : L - 5 + 'px', 'backgroundImage': "url('ag.gif')"}:
                                        {'left' : L + 5 + 'px', 'backgroundImage': "url('rtfm.gif')"};
 
  Object.assign(pic.style, o);
});
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom