Java 使精灵向向量的相反方向移动
我有一个从屏幕左下角移动到右上角的精灵。我想知道的是我如何让它转过来,朝相反的方向走,然后继续这个循环 我试着否定向量的方向,但那不起作用 这就是我现在拥有的:Java 使精灵向向量的相反方向移动,java,libgdx,Java,Libgdx,我有一个从屏幕左下角移动到右上角的精灵。我想知道的是我如何让它转过来,朝相反的方向走,然后继续这个循环 我试着否定向量的方向,但那不起作用 这就是我现在拥有的: public void create() { // Game Initialization v = new Vector2(Gdx.graphics.getWidth() - 0, Gdx.graphics.getHeight() - 0); v.nor(); v.scl(100); sp
public void create() {
// Game Initialization
v = new Vector2(Gdx.graphics.getWidth() - 0, Gdx.graphics.getHeight() - 0);
v.nor();
v.scl(100);
spriteBatch = new SpriteBatch();
bug = new Sprite(new Texture("EnemyBug.png"));
bug.setSize(50, 85);
bug.setOrigin(0,0);//Gdx.graphics.getHeight() / 5, Gdx.graphics.getHeight() / 5);
bug.setPosition(1,1);//Gdx.graphics.getWidth() - 50, Gdx.graphics.getHeight() - 50);
bug.rotate(v.angle());
rotDeg = 5;
}
@Override
public void render() {
// Game Loop
Gdx.gl.glClearColor(0.7f, 0.7f, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
if(bug.getX() >= (int)(Gdx.graphics.getWidth() - 100) && bug.getY() >= (int)(Gdx.graphics.getHeight() - 100)){
turn = !turn;
}
else if(bug.getX() <= 50 && bug.getY() <= 50){
turn = !turn;
}
if(!turn){
bug.translate(v.x * Gdx.graphics.getDeltaTime(), v.y * Gdx.graphics.getDeltaTime());
}
else{
bug.translate(-(v.x * Gdx.graphics.getDeltaTime()), -(v.y * Gdx.graphics.getDeltaTime()));
}
bug.translate(v.x * Gdx.graphics.getDeltaTime(), v.y * Gdx.graphics.getDeltaTime());
bug.draw(spriteBatch);
spriteBatch.end();
}
public void create(){
//游戏初始化
v=新矢量2(Gdx.graphics.getWidth()-0,Gdx.graphics.getHeight()-0);
v、 nor();
v、 症状自评量表(100);
spriteBatch=新spriteBatch();
bug=新精灵(新纹理(“EnemyBug.png”);
bug.setSize(50,85);
setOrigin(0,0);//Gdx.graphics.getHeight()/5,Gdx.graphics.getHeight()/5);
bug.setPosition(1,1);//Gdx.graphics.getWidth()-50,Gdx.graphics.getHeight()-50);
旋转(v.angle());
rotDeg=5;
}
@凌驾
公共无效呈现(){
//游戏循环
Gdx.gl.glClearColor(0.7f,0.7f,0.2f,1);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
spriteBatch.begin();
if(bug.getX()>=(int)(Gdx.graphics.getWidth()-100)和&bug.getY()>=(int)(Gdx.graphics.getHeight()-100)){
转身=!转身;
}
否则,如果(bug.getX()每帧翻译精灵两次:
if(!turn){
bug.translate(v.x * Gdx.graphics.getDeltaTime(), v.y * Gdx.graphics.getDeltaTime());
}
else{
bug.translate(-(v.x * Gdx.graphics.getDeltaTime()), -(v.y * Gdx.graphics.getDeltaTime()));
}
//REMOVE THIS LINE
bug.translate(v.x * Gdx.graphics.getDeltaTime(), v.y * Gdx.graphics.getDeltaTime());
这条额外的线可能会导致您的问题,使您的负面翻译无效,并使正面翻译加倍。这是一条if语句,每帧只执行其中一条。您要我删除的线会使精灵移动到右上角,带有负面的那条应该使其从右上角移动到下角左。if上的translate将使其朝正方向移动。else上的另一个translate将使其朝负方向移动。额外的一个translate将始终执行,将正方向加倍,并将负方向置零。那么我将如何实现它?我是否删除整个if/else语句?哦,好的,谢谢,我不知道我有一个translate函数outsi取消if语句。顺便问一下,我如何使它以相反的方向平稳旋转。当我执行bug.rotate(180)
时,它会突然旋转。你必须每帧旋转一点。就像你每帧平移一点一样。创建一个angularVelocity变量并执行类似bug.rotate的操作(angularVelocity*Gdx.graphics.getDeltaTime());