Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用箭头键移动javascript字符_Javascript_Move_Arrow Keys - Fatal编程技术网

使用箭头键移动javascript字符

使用箭头键移动javascript字符,javascript,move,arrow-keys,Javascript,Move,Arrow Keys,我正在用javascript编写一个RPG游戏,当我按下箭头键或wasd键时,我想让我的主角移动。我有代码生成他,但我应该如何定义他的位置,以便他在按下上述键时移动 function mainchar(){ var mainchar = new Image(); mainchar.src = /*I'll add this soon*/; mainchar.style.top = /*What should I put here?*/; mainchar.styl

我正在用javascript编写一个RPG游戏,当我按下箭头键或wasd键时,我想让我的主角移动。我有代码生成他,但我应该如何定义他的位置,以便他在按下上述键时移动

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 );
}
使用:

使用正确的按键代码捕捉按键向上/向下的时间

然后将mainchar.style.top设置为您希望将其移动到的位置

e、 g

  • 检测密钥
  • 当按下正确的键时
  • 更新mainchar.stlye.top

  • “我应该在这里放什么?”这取决于你不想把你的角色放在哪里。@Krister我应该把起始位置放在那里,以后再更改吗?她是一个代表,主要运行SCMiddy的代码。稍后我会自己编写一些代码。e、 g.当前排名前10位的角色?我不知道这取决于你想让他动多少?