2010-07-23 33 views
-2

http://davzy.com/gameA/爲什麼我的重力在這裏工作?

我找不出一個聰明的方法來獲得重力。現在,它會檢測哪個字符塊已經結束,但它不會掉到那個塊上!

有沒有更好的方法來做引力?我想在沒有遊戲庫的情況下這樣做。

+1

你最好粘貼一些代碼,而不是讓黑客有機會下載你的fi les和搜索那裏你不知道在哪裏。 – 2010-07-23 16:17:55

+1

@OP你在說什麼? – 2010-07-23 16:18:09

+0

我需要一種方法來爲我的遊戲添加重力,我不知道如何! – Macmee 2010-07-23 17:01:25

回答

0

基本上重力的平臺遊戲是這樣的:

var currentGrav = 0.0; 
var gravAdd = 0.5; // add this every iteration of the game loop to currentGrav 
var maxGrav = 4.0; // this caps currentGrav 

var charPosY = getCharPosY(); // vertical position of the character, in this case the lower end 
var colPosY = getColDow(); // some way to get the vertical position of the next "collision" 

for(var i = 0; i < Math.abs(Math.ceil(currentGrav)); i++) { // make sure we have "full pixel" values 
    if (charPosY == colPosY) { 
     onGround = true; 
     break; // we hit the ground 
    } 
    onGround = false; 
    charPosY++; 
} 

我們跳一個可以簡單地這樣做:

if (jumpKeyPressed && onGround) { 
    currentGrav = -5.0; // 
} 

你可以,如果你想(和理解C),退房我的基本平臺遊戲(移動平臺)在這裏:
http://github.com/BonsaiDen/Norum/blob/master/sources/character.c

2

我不知道你是什麼意思的「得到引力」;你的問題不清楚。我認爲,如果當該塊是在可以檢測,則可以使用下面的公式:

S(T)= UT + 1/2原子

s是距離在時間tu是初始速度(在你的情況下將爲零),a是加速度(在地球上這是9.8米/秒)。基本上你會根據你在toriginal top position of object + s(t))得到的值來調整對象的頂部位置。我會想象你會使用某種動畫循環。也許是setInterval。也許其他擁有更多Javascript動畫經驗的人可以通過談談實現這一點的最佳方式。但是,如果該對象在時間t處出現故障,那麼將使用這個公式來計算該對象的位置。

相關問題