简体   繁体   English

LibGDX Scene2D:将动作应用于单独类中的Actor

[英]LibGDX Scene2D: Applying Actions to Actors in separate class

I'm having trouble getting a MoveToAction to work on an Actor (menuBackground), when the Actor is in a separate class. 当Actor在单独的类中时,我无法让MoveToAction在Actor(menuBackground)上工作。 I have attached relevant code below- which doesn't make the Actor move at all. 我在下面附加了相关代码-根本不会使Actor行动。

I have had success in apply other actions to the root stage in the MainMenuScreen class, and to a different Actor (Button) in the MainMenuScreen class, but have had no success applying actions to Actors in a separate class. 我已经成功地将其他操作应用于MainMenuScreen类的根阶段以及MainMenuScreen类中的其他Actor(按钮),但是没有成功将操作应用于单独的类中的Actor。

I've tried putting the MoveToAction in the act(float delta) method within the MenuBackground class, but that didn't work either. 我试过将MoveToAction放在MenuBackground类的act(float delta)方法中,但是那也不起作用。 Neither did assigning the MoveToAction to the menuBackground from within the MainMenuScreen class. 都没有从MainMenuScreen类中将MoveToAction分配给menuBackground。

Note that I am making a call to super.act(delta); 请注意,我正在调用super.act(delta);。 within my MenuBackground class. 在我的MenuBackground类中。

I would ultimately like to put the code for the MoveToAction within the Actor MenuBackground class, to make things neat and tidy. 我最终希望将MoveToAction的代码放在Actor MenuBackground类中,以使事情整洁。

Cheers. 干杯。

Class containing stage: 类包含阶段:

public class MainMenuScreen implements Screen
{
     private Stage stage;
     public MainMenuScreen()
     {
         stage = new Stage(new FitViewport(800, 480));
         Gdx.input.setInputProcessor(stage);

         menuBackground = new MenuBackground();
         MoveToAction moveToAction = new MoveToAction();
         moveToAction.setPosition(242f, 276f);
         moveToAction.setDuration(10f);
         menuBackground.addAction(moveToAction);
         stage.addActor(menuBackground);

    @Override
    public void render(float delta) 
    {
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);    
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }
...
}

Actor Class: 演员班:

public class MenuBackground extends Actor
{
    private Texture menuBackgroundTexture;
    private float actorX;
    private float actorY;

    public MenuBackground()
    {
        menuBackgroundTexture = new Texture(Gdx.files.internal("data/menuTitleTexture.png")); 
        actorX = 242f;
        actorY = 350f;
        setBounds(actorX,actorY,316,128);   
    }

    @Override
    public void draw(Batch batch, float alpha)
    {
        batch.draw(menuBackgroundTexture,actorX,actorY);
    }

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

The problem is inside your draw() method. 问题出在您的draw()方法内部。

Look at code draws your texture, it uses actorX and actorY which are actually fields that don't change their values. 看一下代码绘制纹理,它使用actorXactorY ,它们实际上是不会更改其值的字段。

The proper way is: 正确的方法是:

batch.draw(menuBackgroundTexture, getX(), getY(), getWidth(), getHeight());

So, you should use actor's own fields and getters and don't manage yours. 因此,您应该使用actor自己的字段和getter,而不要管理自己的字段和getter。

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

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