D
D
Dimazsever2014-09-05 16:32:32
JavaScript
Dimazsever, 2014-09-05 16:32:32

How to calculate the jump vector?

Help calculate the jump vector for the player. The player's speed is 2. In general, if you hold down the movement button, then the player should jump in the direction of movement. Well, if you just press the spacebar, then up. Help to understand, please. Here is the jsfiddle code .

var canvas = document.querySelector("#canvas");
var ctx = canvas.getContext("2d");
var player = {
color: "black",
x: 0,
y: 60,
w: 10,
h: 10
}
var kc;
document.addEventListener('keydown',function(e){
kc = e.keyCode;
console.log(kc);
},false);
document.addEventListener('keyup',function(e){
kc = false;
},false);
function update(){
switch(kc){
case 37:
player.x -= 2;
break;
case 39:
player.x += 2;
break;
case 32:
//код прыжка
break;
}
}
function draw(){
ctx.clearRect(0,0,800,600);
ctx.fillStyle = player.color;
ctx.fillRect(player.x,player.y,player.w,player.h);
update();
window.requestAnimationFrame(draw);
}
draw();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IVAN SAVCHENKO, 2014-09-05
@Dimazsever

You mixed up the keycode number. Here is the correct decision about the jump. To prevent the behavior of endless jumps, you need to check for compliance with the initial position of the object along the y axis, if it does not correspond to the initial one, then return false so as not to jump far up))) implement

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question