虽然代码看起来是正确的,但由于游戏中的某种原因,精灵并没有明显地朝着光标旋转,它根本不旋转
Texture playerTexture;
Sprite playerSprite;
Vector3 playerPos = new Vector3();
public void create () {
batch = new SpriteBatch();
playerTexture = new Texture("playerAiming.png");
playerSprite = new Sprite(playerTexture);
playerSprite.setOrigin(playerSprite.getWidth() / 2, playerSprite.getHeight() / 2);
}
public void render () {
float speed = playerSpeed * Gdx.graphics.getDeltaTime();
float mouseX = Gdx.input.getX();
float mouseY = Gdx.input.getY();
float playerX = playerPos.x + playerSprite.getWidth() / 2;
float playerY = playerPos.y + playerSprite.getHeight() / 2;
float angle = MathUtils.atan2(mouseY - playerY, mouseX - playerX) * MathUtils.radiansToDegrees;
playerSprite.setRotation(angle); //Doesnt work for some reason
ScreenUtils.clear(1, 0, 0, 1);
batch.begin();
batch.draw(playerSprite, playerPos.x, playerPos.y);
batch.end();
//Movement codes and others...
}
变量工作得很好(我对它们进行了系统处理),我尝试使用直接纹理,但也不起作用
它不会旋转,因为您正在使用
SpriteBatch
上的方法来绘制 TextureRegions
并且需要传递位置和旋转参数,而您只传递位置。
Sprite
延伸 TextureRegion
所以当你打电话时
batch.draw(playerSprite, playerPos.x, playerPos.y);
您正在使用的函数不知道
Sprite
的属性,如果您使用
playerSprite.draw(batch);
draw
调用将尊重Sprite
的旋转。