[英]How to make a sprite move diagonally?
I have a sprite that needs to follow the command of an arrow sprite that rotates, right now I can only make my sprite move up,down and left,right.我有一个精灵需要跟随一个旋转的箭头精灵的命令,现在我只能让我的精灵上下左右移动。 How about diagonally?对角线呢? I have no Idea how to do that, the sprite also needs to move always towards outside the screen.我不知道该怎么做,精灵还需要始终向屏幕外移动。
EDIT: So this is what I did:编辑:所以这就是我所做的:
Vector2 position=new Vector2();
public void update(){
//getAngle() is the return value from another class
position.set(MathUtils.cosDeg(cannonObj.getAngle()),MathUtils.sinDeg(cannonObj.getAngle()));
sprite.setPosition(position.x,position.y);
}
What happens is that when I rotate the arrow the main sprite is just moving like the arrow(moving in a circular motion).发生的情况是,当我旋转箭头时,主精灵只是像箭头一样移动(以圆周运动移动)。
//I also tried this
position.scl(2,2);
or
position.translate(2,2);
sprite.x += sin(angle) * amount;
sprite.y += cos(angle) * amount;
I forget whether you have to enter your angle in radians or degrees.我忘记您是否必须以弧度或度数输入角度。 In Java you can use the Math class to do the calculations though.在 Java 中,您可以使用 Math 类进行计算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.