精灵设置纹理不起作用

Sprite setTexture not working

我是 libgdx 的新手,我还不知道 Sprite class 是如何工作的,所以我 运行 在使用 [=18= 时遇到了一些问题]() 没有唤醒(我需要这个来制作动画)。长话短说(举个例子):

sprite = new Sprite (imgb); //work flawlessly

sprite2 = new Sprite ();
sprite2.setTexture (imgb); //doesn't do anything

是我做事的方式有问题还是有其他问题?

原码我的播放器class:

package com.triodefender.game;

import java.util.Vector;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;


import jdk.nashorn.internal.runtime.Debug;

public class Player {
    SpriteBatch batch = new SpriteBatch ();
    Sprite sprite = new Sprite ();
    //Animator animator;
    float cx, cy;
    float hp;
    Texture img = new Texture ("bullet.png"), imgp = new Texture("turret.png");
    Vector<Bullet> bullets = new Vector<Bullet>();

    void Debug () {
        Sprite debug = new Sprite (new Texture("debug.png"), 0, 0, 15, 15);
        debug.setPosition (sprite.getX(), sprite.getY());
        batch.begin ();
        debug.draw (batch);
        batch.end ();

        debug.setPosition (cx, cy);
        batch.begin ();
        debug.draw (batch);
        batch.end();
    }

    Player (com.badlogic.gdx.graphics.Color color) {
        //animator = new Animator(a);
    //sprite = new Sprite (imgp); //this works
        sprite = new Sprite ();
        sprite.setTexture(imgp); //this doesn't work
        sprite.setOriginCenter();
        sprite.setColor(color);

        hp = 100f;
    }

    void Update (float ox, float oy, float rotation, boolean shoot) {
        cx = ox; cy = oy;

        sprite.setPosition(ox - sprite.getWidth()/2, oy - sprite.getHeight()/2);
        sprite.setRotation(-rotation);

        if (shoot) {
            AddBullet();
            //animator.lockRow(0, 1, 5);
        }

        UpdateBullets();
    }

    void AddBullet () {
        float l = sprite.getWidth()/2f;
        float sn = (float) Math.sin(Math.toRadians(-sprite.getRotation()));
        float cs = (float)Math.cos(Math.toRadians( -sprite.getRotation() ));

        float newpx = cx + (float)(l+16f)*sn;
        float newpy = cy + (float)(l+16f)*cs;


        bullets.add(new Bullet(new Sprite(img), newpx, newpy, -sprite.getRotation(), 10f, sprite.getColor()));
    }

    void UpdateBullets () {
        for (int i = 0; i < bullets.size(); i++)
            if (bullets.elementAt(i).Update() == false) {
                bullets.remove(i);
                i--;
            }
    }

    void DrawBullets () {
        for (int i = 0; i < bullets.size(); i++)
            bullets.elementAt(i).Draw();
    }

    float getRotation () {
        return sprite.getRotation();
    }

    void Draw () {
        //Debug ();
        batch.begin();
        sprite.draw(batch);
        batch.end();

        DrawBullets ();
    }
}

你应该查阅Sprite源代码:

/** Creates an uninitialized sprite. The sprite will need a texture region and bounds set before it can be drawn. */
public Sprite () {
    setColor(1, 1, 1, 1);
}

/** Creates a sprite with width, height, and texture region equal to the size of the texture. */
public Sprite (Texture texture) {
    this(texture, 0, 0, texture.getWidth(), texture.getHeight());
}

如您所见,第二个构造函数初始化精灵的纹理和边界(通过调用另一个精灵构造函数)。第一个构造函数只设置它的颜色。 为了让第一个构造函数跟上第二个构造函数的速度,您需要在绘制之前定义纹理区域和边界。您设置了纹理,但您仍然需要定义边界。尝试添加 sprite2.setBounds(0, 0, imgb.getWidth(), imgb.getHeight());

我觉得你需要

sprite.setRegion(..And select you need);