Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Libgdx actor moveTo操作不工作_Java_Android_Libgdx_Actor - Fatal编程技术网

Java Libgdx actor moveTo操作不工作

Java Libgdx actor moveTo操作不工作,java,android,libgdx,actor,Java,Android,Libgdx,Actor,我正试图用动作将演员从舞台上的一点移动到另一点,但它不起作用,我已经尝试了几个小时,但它就是不起作用,我会很高兴得到帮助 我在互联网上搜索过代码,但我尝试过的所有代码都不起作用,我真的不明白这个代码有什么问题,它基本上与互联网上教程中的工作代码相同 演员班: public class Player extends Actor { float x,y; float screenWidth,screenHeight; private Sprite sprite; Texture image; fl

我正试图用动作将演员从舞台上的一点移动到另一点,但它不起作用,我已经尝试了几个小时,但它就是不起作用,我会很高兴得到帮助

我在互联网上搜索过代码,但我尝试过的所有代码都不起作用,我真的不明白这个代码有什么问题,它基本上与互联网上教程中的工作代码相同

演员班:

public class Player extends Actor {

float x,y;
float screenWidth,screenHeight;
private Sprite sprite;
Texture image;
float width,height;
public Player(Texture image,float x, float  y,float width,float height, float screenWidth, float screenHeight)
{
    this.width = width;
    this.height = height;
    this.x = x;
    this.y = y;
    this.screenWidth = screenWidth;
    this.screenHeight = screenHeight;

    this.image = image;
    sprite = new Sprite(image, 0, 0, image.getWidth(), image.getHeight());
    sprite.setPosition(x, y);

}

@Override
public float getX() {
    return x;
}

@Override
public void setX(float x) {
    this.x = x;
}

@Override
public float getY() {
    return y;
}

@Override
public void setY(float y) {
    this.y = y;
}

@Override
public float getWidth() {
    return width;
}

@Override
public void setWidth(float width) {
    this.width = width;
}

@Override
public float getHeight() {
    return height;
}

@Override
public void setHeight(float height) {
    this.height = height;
}

@Override
public void act(float delta) {
    super.act(delta);
}

@Override
public void draw (Batch batch, float parentAlpha) {
    batch.draw(sprite,x,y,width,height);
}
}
主要课程包括:

            player = new Player(playerTexture,screenWidth/2,screenHeight-200,playerTexture.getWidth(),playerTexture.getHeight(),screenWidth,screenHeight);
 MoveToAction action = new MoveToAction();
        action.setPosition(300f,0f);
        action.setDuration(10f);
        player.addAction(action);

  @Override
public void render(float delta) {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.setProjectionMatrix(orthographicCamera.combined);
    batch.begin();
    // batch.draw(playerTexture,10,10,10,10);

    batch.end();

    stage.act(Gdx.graphics.getDeltaTime());

    stage.draw(); //this will call the batch to draw the sprite
}

Actor已经有x、y、width和height字段。自从您创建了自己的参数后,就隐藏了这些参数

问题的根源是您未能覆盖
setPosition()
以使用字段。因此,移动操作将调用该操作并更改已隐藏的字段,当您绘制它时,您将使用自己未更改的字段

不要隐藏字段。它很容易出错。您应该删除我上面提到的字段,以及对getter和setter的所有重写

一般来说,OOP有一条经验法则:如果您在不调用super方法的情况下重写任何getter或setter,那么您可能正在破坏某些东西


不相关,但您应该使用TextureRegion而不是Sprite。Sprite是一个纹理区域,具有额外的大小和位置参数。因为这些特性也在Actor中,所以使用Sprite是多余的,并且容易出错

什么不起作用?你能看见那个球员但他不动吗?你是不是要把这个演员加入舞台?我看不到正在这样做。是的,我正在将他添加到舞台上,我看到了玩家,但他没有移动。您正在覆盖由Actor实现的所有就绪的setX和getX方法。如果删除实例变量x和y以及getX、getY、setX等的覆盖版本,会发生什么情况?这很有效,另一个问题,我希望它以固定的速度移动,当它到达点时不要停止,而是让它继续朝着同一个方向移动,你知道怎么做吗?