【问题标题】:Processing js jump system处理js跳转系统
【发布时间】:2016-02-15 12:13:46
【问题描述】:

我正在尝试在 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();
}

我们将不胜感激。

【问题讨论】:

    标签: javascript processing processing.js


    【解决方案1】:

    你在这里做什么就像:“如果这个(玩家)没有接触地面,那么如果(另外)按下 UP 键,跳!”。所以它会让玩家即使在空中也能跳跃……增加跳跃高度

    您应该执行以下操作:“如果玩家接触地面,则允许跳跃(如果按下 UP 键),否则您已经在跳跃!”。

    类似这样的:

    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;
    }
    

    【讨论】:

    • 谢谢 =) 我不知道我做错了什么,但我重新编写了代码并且它可以工作。我不认为这是跳跃是真是假,但无论如何谢谢=)
    猜你喜欢
    • 2011-06-28
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    相关资源
    最近更新 更多