2016-03-02 42 views
1
if(game.pressedKeys[37]) { 
     this.ship.x -= this.shipSpeed * dt; 
    } 
if(game.pressedKeys[39]) { 
     this.ship.x += this.shipSpeed * dt; 
    } 

此代碼運行良好。但我想,滾動路徑是彎曲的。一種像這樣:在JavaScript中的左/右箭頭上添加曲線路徑

enter image description here

應該是什麼我的解決方案?我該如何解決這個問題?謝謝。

+0

曲線可以拋物線嗎? –

+0

@MaxMastalerz是的,它可以。 –

回答

0

你總是知道你的船的x位置。創建一個需要x位置並返回y的函數。

你擁有的曲線可以由拋物線函數y=a(x-s)^2+b其中a是垂直伸展來表示,s是側向移位,並且b是垂直移位。

在這裏,我創造了這樣,對於500的屏幕寬度工作的功能,和船舶上升到150

enter image description here

function parabolic(x) { 
    return Math.pow((((1/20.412415)*x)-12.24745),2)+150; 
} 
//I am using ugly numbers, however you can have a variable `w` for the screen width 

這裏有一個工作演示一個最大:JSFiddle(點擊在鍵盤的畫布上工作)

document.addEventListener('keydown', function(e) { 
    if(e.which===37) { 
    ship.x--; 
    } else if(e.which===39) { 
    ship.x++; 
    } 
    ship.y = parabolic(ship.x); 
    update(); 
})