作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 libgdx 的新手。我正在开发一款具有玩家 body 的游戏,并且我已经给出了控制,但我想在游戏中的特定时间创建敌人。我尝试创建它,但只创建了玩家 body ,但没有创建敌人 body 。
这是我的代码
public class Player
implements Screen, InputProcessor {
private Body polybody;
private Player player;
private World world;
private Body enemybody;
private Sprite polysprite;
public final float width, height;
private Vector2 movement = new Vector2();
private float speed = 580;
public Player(World world, float x, float y, float width) {
this.width = width; //IMP
height = width * 2;
BodyDef polygon = new BodyDef();
polygon.type = BodyType.DynamicBody;
polygon.position.set(x, y); //
PolygonShape poly = new PolygonShape();
poly.setAsBox(width / 2, height / 2); //
//fixture defn
FixtureDef polyfixture = new FixtureDef();
polyfixture.shape = poly;
polyfixture.friction = 0.8f; //
polyfixture.restitution = 0.1f; //
polyfixture.density = 3; //
//creating actual body
polybody = world.createBody(polygon);
polybody.createFixture(polyfixture);
// polybody.applyAngularImpulse(52, true);
polysprite = new Sprite(new Texture("img/car.jpg"));
polysprite.setSize(0.5f, 1); //size of mario
polysprite.setOrigin(polysprite.getWidth() / 2, polysprite.getHeight() / 2);
polybody.setUserData(polysprite);
poly.dispose();
}
public void update() {
polybody.applyForceToCenter(movement, true);
}
public Body getBody() {
return polybody;
}
@Override
public void show() {
// TODO Auto-generated method stub
}
final float step = 0.1f;
float timeElapsed = 0f;
@Override
public void render(float delta) {
timeElapsed += delta;
while (timeElapsed > step) {
timeElapsed -= step;
Enemy();
}
}
public void Enemy() {
BodyDef enemy = new BodyDef();
enemy.type = BodyType.DynamicBody;
enemy.position.set(6, 3);
PolygonShape enemypoly = new PolygonShape();
enemypoly.setAsBox(2, 2);
FixtureDef enemyfixture = new FixtureDef();
enemyfixture.shape = enemypoly;
enemyfixture.friction = 0.75f;
enemyfixture.restitution = 0.1f;
enemyfixture.density = 5;
enemybody = world.createBody(enemy);
enemybody.createFixture(enemyfixture);
enemypoly.dispose();
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public boolean keyDown(int keycode) {
// TODO Auto-generated method stub
switch (keycode) {
case Keys.W:
movement.y = speed;
break;
case Keys.S:
movement.y = -speed;
break;
case Keys.D:
movement.x = speed;
break;
case Keys.A:
movement.x = -speed;
}
return true;
}
@Override
public boolean keyUp(int keycode) {
// TODO Auto-generated method stub
switch (keycode) {
case Keys.W:
movement.y = 0;
break;
case Keys.S:
movement.y = 0;
break;
case Keys.D:
movement.x = 0;
break;
case Keys.A:
movement.x = 0;
}
return true;
}
@Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
movement.x = speed;
Gdx.app.log("Example", "touch done ");
return true;
}
}
这是首先调用的玩家类,然后在渲染方法上调用敌人方法。玩家的 body 工作正常,但我看不到任何敌人的 body 。请帮忙。提前致谢!!
最佳答案
显然,您的 Player
类负责:
Sprite
时创建!)啊啊,你没有任何其他类(class)。
我认为你应该解决这个问题。
此外,我对每次创建对象后都要调用 dispose()
感到有点厌倦。
顺便说一下,只有当您为游戏设置 Activity 屏幕时才会调用 render()
方法,因此
public class MyGame extends Game {
...
MyScreen myScreen = new MyScreen();
...
public void goToMyScreen() {
this.setScreen(myScreen);
}
}
类似的东西对于 render()
的工作是必要的。
请检查How to track time in Libgdx(android)关于如何处理耗时。
关于java - 如何在 libgdx 中的特定时间创建敌人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28804087/
我是一名优秀的程序员,十分优秀!