我已經研究了許多不同的方式來處理android的輸入,但我真的無法繞過它。我想知道我提供給你的代碼,有人可以向我解釋如何將任一方法集成到我的代碼中嗎?爲什麼這樣做是合適的?Libgdx - 輸入處理[Android]。聽衆沒有聽到什麼?
首先,我將解釋我的意圖,並向您展示相關代碼的快速概述。我想要移動我的角色,左轉,右轉並跳轉(暫時)。這是我的第一個遊戲,它是一個沒有自動跳躍的肖像(與塗鴉Jump有點相似的上下文)。我得到我的關卡隨機生成,它呈現所有對象。現在我想添加輸入,但主要困惑是我在哪裏把代碼放在我的代碼中?
首先,我有一個GameObject
類和子類DynamicGameObject
,這是世界中的所有對象(平臺,通電,錢幣,玩家,敵人等)
的super
類GameObject.java
public class GameObject {
public final Vector2 position;
public final Rectangle bounds;
public GameObject (float x, float y, float width, float height) {
this.position = new Vector2(x, y);
this.bounds = new Rectangle(x - width/2, y - height/2, width, height);
}
DynamicGameObject.java
public class DynamicGameObject extends GameObject {
protected static Vector2 velocity;
public final Vector2 accel;
public DynamicGameObject (float x, float y, float width, float height) {
super(x, y, width, height);
setVelocity(new Vector2());
accel = new Vector2();
}
public Vector2 getVelocity() {
return velocity;
}
public void setVelocity(Vector2 velocity) {
DynamicGameObject.velocity = velocity;
}
}
然後,我有遊戲的主要對象,這將是莫對我將實施的行動進行評估。在這堂課我會把任何輸入處理?
Ruben。 java的[相關代碼]
public Ruben (float x, float y) {
super(x, y, RUBEN_WIDTH, RUBEN_HEIGHT);
state = RUBEN_STATE_IDLE;
stateTime = 0;
grounded = true;
facingRight = true;
}
public void update (float deltaTime) {
//getVelocity().add(World.gravity.x * deltaTime, World.gravity.y * deltaTime);
position.add(velocity.x * deltaTime, velocity.y * deltaTime);
bounds.x = position.x - bounds.width/2;
bounds.y = position.y - bounds.height/2;
if (position.x < 0) position.x = World.WORLD_WIDTH;
if (position.x > World.WORLD_WIDTH) position.x = 0;
stateTime += deltaTime;
}
然後,我有世界級的,將更新遊戲內的一切,相關的更新方法會在哪裏魯本得到更新,如下所示:
private void updateRuben (float deltaTime, float accelX) {
if (ruben.getState() != Ruben.RUBEN_STATE_IDLE && ruben.position.y <= 0.5f) ruben.hitPlatform();
if (ruben.getState() != Ruben.RUBEN_STATE_IDLE) ruben.getVelocity().x = -accelX/10 * Ruben.RUBEN_MOVE_VELOCITY;
// clamp the velocity to the maximum, x-axis only
if (Math.abs(ruben.getVelocity().x) > Ruben.RUBEN_MAX_VELOCITY) {
ruben.getVelocity().x = Math.signum(ruben.getVelocity().x) * Ruben.RUBEN_MAX_VELOCITY;
}
// clamp the velocity to 0 if it's < 1, and set the state to standing
if (Math.abs(ruben.getVelocity().x) < 1) {
ruben.getVelocity().x = 0;
if (Ruben.isGrounded()) {
Ruben.setState(Ruben.RUBEN_STATE_IDLE);
}
}
ruben.getVelocity().scl(deltaTime);
ruben.update(deltaTime);
heightSoFar = Math.max(ruben.position.y, heightSoFar);
}
所以現在我希望你能得到我正在處理的演員的一般背景,我會在哪裏做一個手勢監聽器回調?我把它放入ruben中嗎?還是我完全創建了一個單獨的課程?我嘗試了很多不同的方式,而且我並沒有真正得到任何地方,這讓人感到沮喪。輸入處理程序在哪裏進行手勢?我試圖把回調的姿態探測器用新的姿態聽衆魯
(GestureDetector gd = new GestureDetector(new GestureHandler(this));
的構造函數中
,然後用一個變量來表示一扔已經指向上方的屏幕上時,布爾觸發是真實的,設置ruben的狀態跳轉並改變y軸上的速度。它仍然沒有工作。有人可以幫忙嗎?
如果您需要其他東西,請告訴我。
主要遊戲類
public class GameView extends Game {
// used by all screens
public SpriteBatch batcher;
@Override
public void create() {
batcher = new SpriteBatch();
Settings.load();
Assets.loadBackgrounds();
Assets.loadItems();
Assets.loadRuben();
setScreen(new MainMenuScreen(this));
}
@Override
public void render() {
super.render();
}
}
不幸的是,這不是我要找的。我知道要實施什麼。我只是不知道在代碼中實現它的位置,我提到了我已經在上下文中提出了一個嘗試,希望獲得關於是否有特定方法來實現GestureListener的建議或信息。 – BIW
好吧,我認爲輸入未被識別。如果你初始化了重要的東西,你的主要類如何?什麼是基類? – Quallenmann
感謝您的及時回覆,我已經調整了問題。 – BIW