繁体   English   中英

libgdx不会绘制精灵或动画

[英]libgdx won't draw sprite or animation

似乎我无法使draw方法起作用??? 似乎bullet.draw(batcher)无法正常工作,我无法理解为什么子弹是精灵。 我做了一个Sprite []并将它们添加为动画。 可以吗?

我试过了

batcher.draw(AssetLoader.bulletAnimation.getKeyFrame(runTime), bullet.getX(), bullet.getY(), bullet.getOriginX() / 2, bullet.getOriginY() / 2, bullet.getWidth(), bullet.getHeight(), 1, 1, bullet.getRotation());

但这不起作用,它绘制的唯一方法是

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

下面是代码。

//这在资产类别中

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, bullets);
bulletAnimation.setPlayMode(Animation.PlayMode.LOOP);

//这是GameRender类

public class GameRender() {
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();
}
}

//这是gameworld类

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;
}
}

反正有没有让精灵工作?

我添加子弹类,看看那里可能有什么问题。

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));
}
}

您的关键帧保存在一个名为bullet的数组中,但是当您调用Animation构造函数时,您将传递一个名为“ aims”的东西作为第二个参数。 您应该尝试传递“子弹”,例如:

bulletAnimation = new Animation(0.06f,bullets);

使用Sprite []应该不会有问题,因为Sprite类扩展了我认为的TextureRegion。

------ OP修正了拼写错误,但仍然不起作用------

我认为问题将出在batcher.draw()调用的origin参数上。 Sprite的位置是相对于SpriteBatch坐标系的原点,而Sprite的原点是相对于此位置(即Sprite矩形的左下角)。 为了获得Sprite中心的原点,我认为originX应该为width / 2,originY应该为height / 2。 因此,请尝试:

batcher.draw(AssetLoader.bulletAnimation.getKeyFrame(runTime),bullet.getX(),bullet.getY(), bullet.getWidth()/2,bullet.getHeight()/2,bullet.getWidth(),bullet.getHeight(),1,1,bullet.getRotation());

因为如果您的getOriginX / Y方法返回相对于SpriteBatcher坐标系(屏幕坐标)的原点,则您的Sprite可能会围绕一些荒谬的原点旋转和缩放,最终在屏幕外绘制。

我希望我是对的,而且已经解决了问题。

----- OP发布了进一步的代码,“子弹”类-----

当您在draw方法中调用bullet.getWidth()bullet.getHeight()时,它们将返回0.0f,因为尚未为其指定值。 记住,实际绘制的Sprite是AssetLoader类中的bullet1和bullet2。 尝试使用以下方法设置项目符号的宽度和高度:

setSize(AssetLoader.bullet1.getWidth(), AssetLoader.bullet1.getHeight());

在您的子弹构造函数中。

我也不认为您也不需要在子弹类中使用setRegion() ,因为您实际绘制的Sprites是bullet1和2。

手指交叉。

尝试将更新方法更改为此

Vector2 target = new Vector2(GameWorld.getBall().getX(), GameWorld.getBall().getY());
    target.sub(getX(), getY());
    target.nor().scl(BULLET_HOMING);
    velocity.add(target.scl(delta));
    velocity.nor().scl(BULLET_SPEED);

    Vector2 v = velocity.cpy().scl(delta);
    translate(v.x, v.y);

    setOriginCenter();
    setRotation(velocity.angle());

那应该清理一下你的代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM