2016-02-15 91 views
0

我試圖在processing.js中創建一個跳轉系統,並且完成了它,但不是我想要的。事情就是這樣,跳高取決於按鍵的時間長短。無論按下多長時間,我所需要的是相同的高度。處理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(); 
} 

任何幫助將aprreciated。

回答

0

你在這裏做什麼就像是:「如果這個(玩家)沒有接觸地面,那麼,如果(另外)按鍵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; 
} 
+0

謝謝=)我不知道我做錯了什麼,但我重拍的代碼和它的作品。我不認爲這是跳或是假,但無論如何感謝=) – eskimopest