【问题标题】:libgdx sprite or animation not rotating like a homing (array issues)libgdx 精灵或动画不像归位一样旋转(阵列问题)
【发布时间】:2014-06-05 00:52:38
【问题描述】:

我是 java 和 libgdx 的菜鸟。在某人的帮助下,我得到了归位子弹。现在我正在思考如何让它旋转,以便当它绕过它或接近它时它面向球(这是主角)。子弹面向

另外我如何使它成为一个最好的数组/数组列表,这样我就可以在随机或放置的地方拥有多个子弹。我尝试了很多东西都没有用:(

感谢您的帮助。

//如果你想调用它,下面是子弹或敌人。

public class Bullet extends Sprite {

public static final float BULLET_HOMING = 6000; 
public static final float BULLET_SPEED = 300; 
private Vector2 velocity;
private float lifetime;


public Bullet(float x, float y) {
 velocity = new Vector2(0, 0);
 setPosition(x, y);
}

public void update(float delta) {
 float targetX = GameWorld.getBall().getX();
 float targetY = GameWorld.getBall().getY();
 float dx = targetX - getX();
 float dy = targetY - getY();

 float distToTarget = (float) Math.sqrt(dx * dx + dy * dy); 
 dx /= distToTarget;
 dy /= distToTarget;
 dx *= BULLET_HOMING;
 dy *= BULLET_HOMING;
 velocity.x += dx * delta;
 velocity.y += dy * delta;

 float vMag = (float) Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
 velocity.x /= vMag;
 velocity.y /= vMag;
 velocity.x *= BULLET_SPEED;
 velocity.y *= BULLET_SPEED;

 Vector2 v = velocity.cpy().scl(delta);
 setPosition(getX() + v.x, getY() + v.y);
 setOriginCenter(); 
 setRotation(velocity.angle());
 lifetime += delta;
 setRegion(AssetLoader.bulletAnimation.getKeyFrame(lifetime));
 }
}

// 这是我加载图像的地方。

public class AssetLoader {
public static Animation bulletAnimation;
public static Sprite bullet1, bullet2;

public static void load() {

texture = new Texture(Gdx.files.internal("SpriteN1.png"));
texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);

bullet1 = new Sprite(texture, 380, 350, 45, 20);
bullet1.flip(false, true);

bullet2 = new Sprite(texture, 425, 350, 45, 20);
bullet2.flip(false, true);

Sprite[] bullets = { bullet1, bullet2 };
bulletAnimation = new Animation(0.06f, aims);
bulletAnimation.setPlayMode(Animation.PlayMode.LOOP);
}

public static void dispose() {
// We must dispose of the texture when we are finished.
texture.dispose();
}

//这是用于渲染图像等

public class GameRenderer {
private Bullet bullet;
private Ball ball;

public GameRenderer(GameWorld world) {
myWorld = world;
cam = new OrthographicCamera();
cam.setToOrtho(true, 480, 320);

batcher = new SpriteBatch();
// Attach batcher to camera
batcher.setProjectionMatrix(cam.combined);

shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(cam.combined);

// Call helper methods to initialize instance variables
initGameObjects();
initAssets();
}

private void initGameObjects() {
ball = GameWorld.getBall();
bullet = myWorld.getBullet();
scroller = myWorld.getScroller();
}

private void initAssets() {
ballAnimation = AssetLoader.ballAnimation;
bulletAnimation = AssetLoader.bulletAnimation;
}

public void render(float runTime) {

Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

batcher.begin();
// Disable transparency 
// This is good for performance when drawing images that do not require
// transparency.
batcher.disableBlending();

// The ball needs transparency, so we enable that again.
batcher.enableBlending();


batcher.draw(AssetLoader.ballAnimation.getKeyFrame(runTime), ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight());

batcher.draw(AssetLoader.bulletAnimation.getKeyFrame(runTime), bullet.getX(), bullet.getY());

// End SpriteBatch
batcher.end();
}
}

// 我猜这是在屏幕上加载图像等

public class GameWorld {

public static Ball ball;
private Bullet bullet;
private ScrollHandler scroller;

public GameWorld() {
ball = new Ball(480, 273, 32, 32);
bullet = new Bullet(10, 10);
scroller = new ScrollHandler(0);
}

public void update(float delta) {
ball.update(delta);
bullet.update(delta);
scroller.update(delta);
}

public static Ball getBall() {
return ball;
}

public ScrollHandler getScroller() {
return scroller;
}

public Bullet getBullet() { 
return bullet;
}
}

所以这就是全部。图像通过 AssetLoader 加载,然后通过 Bullet 类加载到 GameRenderer 和 GameWorld。我猜是这样的。抱歉新手,还在学习中。

感谢您的帮助或任何建议。

ps 我在 gamedev 上发布了这个,如果它是一个重复的帖子,请告诉我,我会删除它。 请不要因为我是新手而惩罚我 谢谢

【问题讨论】:

    标签: arrays animation arraylist libgdx


    【解决方案1】:

    将动画更改为精灵并遍历数组尝试一下,看看是否有效。 还 做子弹.draw(batcher);反而。 试试看 希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      您还需要为子弹提供旋转。这是API of MathUtils.

      Vector2 vel = bullet.getVelocity();
      float xComponent = vel.x;
      float yComponent = vel.y;
      float rotation = MathUtils.atan2(yComponent, xComponent)
                           * MathUtils.radiansToDegrees;
      

      也在Bullet 类中创建getVelocity() 方法。


      将该旋转应用于子弹的精灵。这是API of SpriteBatch draw() method

      替换

      batcher.draw(AssetLoader.bulletAnimation.getKeyFrame(runTime),
                       bullet.getX(), bullet.getY());
      

      与

      batcher.draw(AssetLoader.bulletAnimation.getKeyFrame(runTime),
                       bullet.getX(), bullet.getY(), bullet.getWidth(), bullet.getHeight(),
                       1.0f, 1.0f, rotation);
      

      关于您关于多个子弹的第二个问题,我建议您使用Array class provided by LibGDX。
      它易于使用且速度也很快。

      希望这会有所帮助。
      祝你好运。

      【讨论】:

      • 动画不是问题。动画只是改变纹理并与绘图逻辑解耦。仅当您显示堆栈跟踪时,才能对异常进行任何推理。
      • 数组示例可在 code.google.com/p/libgdx/wiki/SimpleApp 获得。查看该来源中如何使用雨滴。
      • com.aozora.GameWorld.GameWorld.update(GameWorld. java:30) at com.aozora.Screen.GameScreen.render(GameScreen.java:32) at com.badlogic.gdx.Game.render(Game.java:46) at com.badlogic.gdx.backends.lwjgl.LwjglApplication .mainLoop(LwjglApplication.java:206) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
      • 行是这个浮点旋转 = MathUtils.atan2(bullet.getVelocity().y, bullet.getVelocity().x) * MathUtils.radiansToDegrees;
      • 在该行之前写上bullet.getVelocity();。如果异常转移到该行,则您的 getVelocity 方法将返回 null。您可以轻松修复它。我的绘图代码中还有其他不相关的错字。请再复制一次。
      猜你喜欢
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      相关资源
      最近更新 更多