简体   繁体   English

LibGDX Scene2D:如何将动作应用于不在左下角的actor?

[英]LibGDX Scene2D: How to apply actions to actors which are not located at bottom left corner?

Here is the code from the Main class: 这是Main类的代码:

MyActor myActor = new MyActor;
moveAction = new MoveToAction();
moveAction.setPosition(600f, 750f);
myActor.addAction(moveAction);

And here is the code from the MyActor class 这是MyActor类的代码

@Override
public void draw(Batch batch, float alpha){
    batch.draw(texture,getX(),getY());
}

If it is written like this, the action will work but the texture starting position is in the bottom left corner and if I replace getX() and getY() with other coordinates, the actions will not work and the texture will just stay at the same position. 如果这样写,则动作将起作用,但纹理的起始位置在左下角,如果我用其他坐标替换getX()和getY(),则该动作将不起作用,纹理将仅停留在相同的位置。 So how exactly I set starting position for the actor? 那么我如何准确地设置演员的起始位置呢?

Actors have their own position values. 演员有自己的位置值。 You probably know this considering you use them to draw their texture at the correct location. 考虑到要使用它们在正确的位置绘制纹理,您可能知道这一点。 Therefore, what you need to do is set the Actor's initial position when you create it. 因此,您需要做的是在创建Actor时设置其初始位置。 Something like this: 像这样:

MyActor myActor = new MyActor;
myActor.setPosition(100, 100);

Now, if you want to give the actor an action to move somewhere else, instead of creating a new MoveToAction , use the Actions convenience methods like this: 现在,如果要给actor一个动作以移动到其他地方,而不是创建一个新的MoveToAction ,请使用如下所示的Actions便捷方法:

myActor.addAction(Actions.moveTo(600, 750));

This will move the Actor to that position instantaneously, so if you want the Actor to move there in a certain period of time you would have to write that line like this: 这会将Actor立即移动到该位置,因此,如果您希望Actor在一定时间内移动到该位置,则必须这样编写该行:

myActor.addAction(Actions.moveTo(600, 750, duration));

duration is a float that holds the number of seconds you want the actor to take to get to that specified location. duration是一个浮点数,其中包含您希望角色到达指定位置所花费的秒数。

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

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