英文:
Moving of Actor LibGDX to dynamic coordinates of another Actor
问题
请帮助我解决这个问题。我需要将一个角色移动到另一个角色的动态坐标上。如果我使用MoveToAction,角色会移动到另一个角色的GetX()和GetY()坐标,但不会跟随其移动。
以下是将角色移动到另一个角色的旧坐标的简单代码。我希望游戏元素能够跟随移动的玩家:
game_element = new GameContainer();
stage.addActor(game_element);
game_element.addAction(Actions.moveTo(player.getX(), player.getY(), 2));
但是,如果我在游戏元素移动到玩家位置时移动玩家,那么玩家会到达旧坐标,并且不会跟随他移动。在LibGDX中如何实现这一点?
英文:
Please, help me with my issue. I need to move an Actor to dynamic coordinates of another Actor. If I use MoveToAction, the Actor moves to the given GetX() and GetY() coordinates of another Actor, but does not follow it as it moves.
This simple code move Actor to old coordinates of another Actor. I want the game element to follow the moving player.:
game_element = new GameContainer();
stage.addActor(game_element);
game_element.addAction(Actions.moveTo(player.getX(), player.getY(), 2));
But if I move the player at the moment when the game element moves to him, then he arrives at the old coordinates, and does not follow him.
How can this be implemented in LibGDX?
答案1
得分: 2
根据你所需的细节,可以通过创建自己的Action
来实现。
这个例子将尝试以恒定的速度到达目标Actor
,当跟随者到达目标时,Action
将被禁用(但可以通过更改act
方法的行为来轻松更改)。
public class MoveToActorAction extends Action
{
private final Actor target;
private float speed = 140.0f; // 这是你希望它移动的速度
private float threshold = 1.0f; // 这是你认为它到达目标的距离阈值
public MoveToActorAction(Actor target) {
this.target = target;
}
@Override
public boolean act(float delta) {
Vector2 position = new Vector2(actor.getX(), actor.getY());
Vector2 targetPosition = new Vector2(target.getX(), target.getY());
Vector2 directionToTarget = targetPosition.sub(position);
float distance = directionToTarget.len();
if (distance < threshold) {
return true; // 已经到达目标
}
else {
float distanceToTravel = Math.min(distance, delta * speed);
position.add((directionToTarget.nor().scl(distanceToTravel)));
actor.setX(position.x);
actor.setY(position.y);
return false;
}
}
}
在下面的图像中,T是目标,F是跟随者,T有两个连续的动作将其移动到两个角落,而F只有MoveToActorAction
。
英文:
Depending on the details of what you're after this can be achieved by creating your own Action
.
This example will try to get to the target Actor
at a constant speed, and the Action
is disabled when the followed reaches the target (but this can easily be changed by changing the behaviour of the act
method)
public class MoveToActorAction extends Action
{
private final Actor target;
private float speed = 140.0f; // this is how fast you want it to move
private float threshold = 1.0f; // this is how close it needs to be for you to consider it to be at the target
public MoveToActorAction(Actor target) {
this.target = target;
}
@Override
public boolean act(float delta) {
Vector2 position = new Vector2(actor.getX(), actor.getY());
Vector2 targetPosition = new Vector2(target.getX(), target.getY());
Vector2 directionToTarget = targetPosition.sub(position);
float distance = directionToTarget.len();
if (distance < threshold) {
return true; // it has gotten to the target
}
else {
float distanceToTravel = Math.min(distance, delta * speed);
position.add((directionToTarget.nor().scl(distanceToTravel)));
actor.setX(position.x);
actor.setY(position.y);
return false;
}
}
}
In the image below the T is the target and the F is the follower and the T has two sequenced actions moving it to two corners, and the F has only the MoveToActorAction
.
答案2
得分: 0
- 创建一个动作,移动距离的三分之二。
- 重新计算目标。
- 将步骤1和2放入一个序列动作中,并将其添加到追逐的对象中。
- 重复步骤1-3,直到靠近目标。
void chasePlayer(){
xDistance = player.getX() - getX();
yDistance = player.getY() - getY();
int actualDistance = Mathutils.sqrt(xDistance^2 + yDistance^2);
if(actualDistance < distance_threshold){
停止追逐玩家
执行目标达到部分
}
timeForThisMove = actualDistance * 0.66f / mySpeed;
MoveByAction act1 = (Actions.moveBy(xDistance*0.66f, yDistance*0.66f, timeForThisMove));
Runnable reCalculateTarget = new Runnable() {
@Override
public void run() {
chasePlayer();
}
};
// 这将按顺序设置两个动作:追逐2/3并重新计算目标
game_element.addAction(Actions.sequence(act1, Actions.run(reCalculateTarget)));
}
英文:
-
create action to move two third of the distance,
-
Re calculate Target..
-
Put 1 and 2 in a sequence Action, add it to the one that chases..
-
Repeat 1-3 until you get closer.
void chasePlayer(){
xDistance = player.getX()-getX();
yDistance = player.getY()-getY();int actualDistance = Mathutils.sqrt(xDistance^2 + yDistance^2);
if(actualDistance < distance_threshold){
Stop chasing player
do target reached part
}timeForThisMove = actualDistance * 0.66f / mySpeed;
MoveByAction act1 = (Actions.moveBy(xDistance0.66f, yDistance0.66f, timeForThisMove));
Runnable reCalculateTarget = new Runnable() { @Override public void run() { chasePlayer() } }; // This will set two actions in sequence.. chase 2/3rd and recalculating game_element.addAction(Actions.sequence(act1, Actions.run(reCalculateTarget));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论