java LibGDX场景2D:对单独类中的参与者应用操作
当演员在另一个班级时,我很难让MoveToAction在演员身上工作(menuBackground)。我在下面附上了相关代码——这根本不会让演员动起来
我成功地将其他操作应用于MainMenuScreen类中的根阶段,以及MainMenuScreen类中的其他参与者(按钮),但没有成功地将操作应用于单独类中的参与者
我曾尝试将MoveToAction放在MenuBackground类中的act(float delta)方法中,但也没有成功。也没有从MainMenuScreen类中将MoveToAction分配给menuBackground
请注意,我正在给super打电话。act(德尔塔);在我的MenuBackground课堂上
我最终希望将MoveToAction的代码放在Actor MenuBackground类中,使事情变得整洁
干杯
包含阶段的类:
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();
}
...
}
演员类:
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);
}
...
}
# 1 楼答案
问题出在
draw()
方法内部看看代码绘制纹理,它使用
actorX
和actorY
这两个字段实际上不会改变它们的值正确的方法是:
所以,你应该使用演员自己的字段和getter,而不是管理你自己的字段和getter