0

I'm writing an RPG game in javascript, and I want my main character to move when youi press the arrow keys or the wasd keys. I have code to generate him, but how should I define his postion so he moves when the aforementioned keys are pressed?

function mainchar(){
    var mainchar = new Image();
    mainchar.src = /*I'll add this soon*/;
    mainchar.style.top = /*What should I put here?*/;
    mainchar.style.left = /*What should I put here?*/;
    mainchar.style.position = "absolute";
    document.body.appendChild( mainchar );
}
Aido
  • 1,524
  • 3
  • 18
  • 41
  • 2
    "What should I put here?", well that depends on where you wan't your character placed. – Cyclonecode Aug 24 '12 at 15:54
  • @Krister Should I put the starting position there and change it later? – Aido Aug 24 '12 at 16:13
  • Her's a representation, mainly running Scmiddty's code. I'll code some myself later. http://dl.dropbox.com/u/18785762/Rust/index.html – Aido Aug 24 '12 at 16:48

1 Answers1

0

Use: http://help.dottoro.com/ljaffqqe.php

To capture when the keyup / keydown using the correct key code.

Then set mainchar.style.top to where ever you want it to be moved to...

e.g.

  1. Detect Key
  2. When correct key is pressed
  3. Updated mainchar.stlye.top
Lemex
  • 3,772
  • 14
  • 53
  • 87