为什么我的玩家在 Eclipse 和 LibGDX 中没有下落(重力)?
Why isn't my player falling (gravity) in Eclipse and LibGDX?
我试图让我的球员 fall/go 失望,但他没有。他只是卡在地图上。他不能动。你能帮忙吗?
这是屏幕的代码。这是我拥有所有方法的地方,如显示、渲染、调整大小、暂停、恢复、隐藏等
package net.hasanbilal.prisonrevelations.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import net.hasanbilal.prisonrevelations.entities.Player;
public class Play implements Screen {
private TiledMap map;
private OrthogonalTiledMapRenderer otmr;
private OrthographicCamera camera;
private Player porter;
@Override
public void show() {
map = new TmxMapLoader().load("maps/level1.tmx");
otmr = new OrthogonalTiledMapRenderer(map);
camera = new OrthographicCamera();
porter = new Player(new Sprite(new Texture("img/porter.png")));
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
otmr.setView(camera);
otmr.render();
otmr.getBatch().begin();
porter.draw(otmr.getBatch());
otmr.getBatch().end();
}
@Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
dispose();
}
@Override
public void dispose() {
map.dispose();
otmr.dispose();
}
}
这是播放器的代码
package net.hasanbilal.prisonrevelations.entities;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
public class Player extends Sprite {
/**
* x and y velocity
*/
private Vector2 velocity = new Vector2();
//change 120 to 60*2 if it doesn't work
private float speed = 60 * 2, gravity = 60 * 1.8f;
public Player(Sprite s) {
super(s);
}
public void draw(SpriteBatch sb) {
update(Gdx.graphics.getDeltaTime());
super.draw(sb);
}
private void update(float deltaTime) {
velocity.y -= gravity * deltaTime; //gravity to player
if(velocity.y>speed)
velocity.y = speed;
else if(velocity.y < speed)
velocity.y = -speed;
setX(getX() + velocity.x * deltaTime);
setY(getY() + velocity.y * deltaTime);
}
}
这项作业即将到期!请帮忙!
没关系伙计们,我修好了。我所要做的就是在 porter.draw(otmr.getBatch());
和 otmr.getBatch().end();
之间添加 porter.update(1);
我试图让我的球员 fall/go 失望,但他没有。他只是卡在地图上。他不能动。你能帮忙吗?
这是屏幕的代码。这是我拥有所有方法的地方,如显示、渲染、调整大小、暂停、恢复、隐藏等
package net.hasanbilal.prisonrevelations.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import net.hasanbilal.prisonrevelations.entities.Player;
public class Play implements Screen {
private TiledMap map;
private OrthogonalTiledMapRenderer otmr;
private OrthographicCamera camera;
private Player porter;
@Override
public void show() {
map = new TmxMapLoader().load("maps/level1.tmx");
otmr = new OrthogonalTiledMapRenderer(map);
camera = new OrthographicCamera();
porter = new Player(new Sprite(new Texture("img/porter.png")));
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
otmr.setView(camera);
otmr.render();
otmr.getBatch().begin();
porter.draw(otmr.getBatch());
otmr.getBatch().end();
}
@Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
dispose();
}
@Override
public void dispose() {
map.dispose();
otmr.dispose();
}
}
这是播放器的代码
package net.hasanbilal.prisonrevelations.entities;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
public class Player extends Sprite {
/**
* x and y velocity
*/
private Vector2 velocity = new Vector2();
//change 120 to 60*2 if it doesn't work
private float speed = 60 * 2, gravity = 60 * 1.8f;
public Player(Sprite s) {
super(s);
}
public void draw(SpriteBatch sb) {
update(Gdx.graphics.getDeltaTime());
super.draw(sb);
}
private void update(float deltaTime) {
velocity.y -= gravity * deltaTime; //gravity to player
if(velocity.y>speed)
velocity.y = speed;
else if(velocity.y < speed)
velocity.y = -speed;
setX(getX() + velocity.x * deltaTime);
setY(getY() + velocity.y * deltaTime);
}
}
这项作业即将到期!请帮忙!
没关系伙计们,我修好了。我所要做的就是在 porter.draw(otmr.getBatch());
和 otmr.getBatch().end();
porter.update(1);