Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.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 处理js跳转系统_Javascript_Processing_Processing.js - Fatal编程技术网

Javascript 处理js跳转系统

Javascript 处理js跳转系统,javascript,processing,processing.js,Javascript,Processing,Processing.js,我正在尝试在processing.js中创建一个跳转系统,它已经完成了,但并不是我想要的。问题是,像这样,跳跃高度取决于按下键的时间。我想要的是相同的高度,不管按键多长时间 这是我的密码: var keys = []; void keyPressed() { keys[keyCode] = true; }; void keyReleased() { keys[keyCode] = false; }; var Player = function(x, y) { this.

我正在尝试在processing.js中创建一个跳转系统,它已经完成了,但并不是我想要的。问题是,像这样,跳跃高度取决于按下键的时间。我想要的是相同的高度,不管按键多长时间

这是我的密码:

var keys = [];
void keyPressed() {
    keys[keyCode] = true;
};
void keyReleased() {
    keys[keyCode] = false;
};

var Player = function(x, y) {
    this.x = x;
    this.y = y;
    this.g = 0;
    this.vel = 2;
    this.jumpForce = 7;
    this.jump = false;
};
Player.prototype.draw = function() {
    fill(255,0,0);
    noStroke();
    rect(this.x, this.y, 20, 20);
};
Player.prototype.move = function() {
    if(this.y < ground.y) {
        this.y += this.g;
        this.g += 0.5;
        this.jump = false;
    }
    if(keys[RIGHT]) {
        this.x += this.vel;
    }
    if(keys[LEFT]) {
        this.x -= this.vel;
    }

    //preparing to jump
    if(keys[UP] && !this.jump) {
        this.jump = true;
    }
    // if jump is true, than the ball jumps... after, the gravity takes place pulling the ball down...
    if(this.jump) {
        this.y -= this.jumpForce;
    }
};
Player.prototype.checkHits = function() {
    if(this.y+20 > ground.y) {
        this.g = 0;
        this.y = ground.y-20;
        jump = false;
    }
};

Var Ground = function(x, y, label) {
    this.x = x;
    this.y = y;this.label = label;
};
Ground.prototype.draw = function() {
    fill(0);
    rect(0, 580, width, 20);
};

var player = new Player(width/2, height/2);
var ground = new Ground(0, 580, "g");
void draw() {
    background(255,255,255);
    player.draw();
    player.move();
    player.checkHits();
    ground.draw();
}
var-keys=[];
按下void键(){
keys[keyCode]=真;
};
void keyReleased(){
keys[keyCode]=false;
};
变量Player=函数(x,y){
这个.x=x;
这个。y=y;
这个。g=0;
这个.vel=2;
这个力=7;
this.jump=false;
};
Player.prototype.draw=函数(){
填充(255,0,0);
仰泳();
rect(this.x,this.y,20,20);
};
Player.prototype.move=函数(){
如果(此.y<接地.y){
这个.y+=这个.g;
这是g+=0.5;
this.jump=false;
}
如果(键[右]){
this.x+=this.vel;
}
如果(键[左]){
this.x-=this.vel;
}
//准备跳跃
if(键[向上]&&!this.jump){
this.jump=true;
}
//如果跳跃是真的,那么球会跳跃……之后,重力会将球向下拉。。。
如果(这个跳转){
this.y-=this.jumpForce;
}
};
Player.prototype.checkHits=函数(){
如果(此.y+20>接地.y){
这个。g=0;
此.y=接地.y-20;
跳跃=假;
}
};
Var接地=功能(x、y、标签){
这个.x=x;
this.y=y;this.label=label;
};
Ground.prototype.draw=函数(){
填充(0);
矩形(0580,宽度20);
};
var玩家=新玩家(宽度/2,高度/2);
var接地=新接地(0580,“g”);
作废提款(){
背景(255255);
player.draw();
player.move();
player.checkHits();
地。画();
}

欢迎提供任何帮助。

您在这里做的是这样的:“如果这个(玩家)没有触地,那么,如果(另外)按了向上键,跳!”。因此,即使球员在空中,它也会允许他跳跃。。。增加跳跃高度

您应该这样做:“如果玩家触地,允许跳跃(如果按下向上键),否则您已经在跳跃!”

大概是这样的:

if(this.y < ground.y) {
    this.y += this.g;
    this.g += 0.5;
    this.jump = true;
}
else{
    this.jump = false;
}

if(keys[UP] && !this.jump) {
    this.jump = true;
    this.y -= this.jumpForce;
}
if(this.y
谢谢=)我不知道我做错了什么,但我重新编写了代码,它工作正常。我不认为跳转是真是假,但无论如何还是要感谢=)