我在J2ME工作跳轉的數學,我有我的gameloop執行以下操作:在2D遊戲
public void run() {
Graphics g = this.getGraphics();
while (running) {
long diff = System.currentTimeMillis() - lastLoop;
lastLoop = System.currentTimeMillis();
input();
this.level.doLogic();
render(g, diff);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
stop(e);
}
}
}
所以它只是一個基本的gameloop,該doLogic()
功能適用於所有的邏輯功能調用在場景中的人物和render(g, diff)
通話場景的每一個字符,這之後的animateChar
功能,Character類的animChar
功能設置在屏幕的一切,因爲這:
protected void animChar(long diff) {
this.checkGravity();
this.move((int) ((diff * this.dx)/1000), (int) ((diff * this.dy)/1000));
if (this.acumFrame > this.framerate) {
this.nextFrame();
this.acumFrame = 0;
} else {
this.acumFrame += diff;
}
}
這保證了我牛逼一切都必須根據機器從週期到週期的時間移動(記住它是一部手機,而不是遊戲裝備)。我確定這不是實現這種行爲的最有效的方法,所以我完全公開批評我的編程技巧的評論,但在這裏我的問題:當我讓我跳字,我做的是我把他dy爲負值,例如-200,我設置布爾跳爲真,這使得角色上升,然後我有這個函數checkGravity()
,確保上升的所有東西都必須下降,checkGravity
還檢查字符被過度的平臺,所以我將剝離下來一點了你的時間緣故:
public void checkGravity() {
if (this.jumping) {
this.jumpSpeed += 10;
if (this.jumpSpeed > 0) {
this.jumping = false;
this.falling = true;
}
this.dy = this.jumpSpeed;
}
if (this.falling) {
this.jumpSpeed += 10;
if (this.jumpSpeed > 200) this.jumpSpeed = 200;
this.dy = this.jumpSpeed;
if (this.collidesWithPlatform()) {
this.falling = false;
this.standing = true;
this.jumping = false;
this.jumpSpeed = 0;
this.dy = this.jumpSpeed;
}
}
}
所以,問題是,這個功能更新DY不管diff,使得人物在慢速機器中像超人一樣飛行,我不知道如何實現因素,以便當角色跳躍時,他的速度按照與遊戲速度成比例的方式遞減。任何人都可以幫我解決這個問題嗎?或者給我指點一下在J2ME中做出一個2D跳轉的正確方式。
這是一個長期的問題。你可能想把精髓拉出來。 – 2009-05-29 00:11:22
作爲一個側面的問題 - 你真的想在J2ME遊戲中做100次/秒嗎?正如我想的那樣,你應該真的睡到下一個理想的起牀點,而不是每次固定的時間。 – 2009-05-29 02:02:59