2014-10-31 73 views
0

我有一个精灵从屏幕的左下角移动到右上角。我想知道的是,我将如何让它转向反方向,并继续这个循环。制作精灵在矢量的相反方向移动

我试图否定矢量的方向,但不工作。

这是我现在所拥有的:

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()); 

这额外的行可能会造成您的问题,抵消你的负面翻译和加倍:

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(); 
} 

回答

2

你每帧两次转换精灵积极的一个。

+0

这是一个if语句,它只会在每个帧中执行其中的一个。你告诉我要删除的行使精灵进入右上角,带负向的精灵应该让它从右上角走到左下角。 – User765876 2014-10-31 22:37:14

+0

翻译如果会使它走向积极的方向。另一个翻译将使其消极。多余的一个将永远执行加倍积极和消极否定。 – MadEqua 2014-10-31 22:39:48

+0

那么我会如何实现它?我是否删除整个if/else语句? – User765876 2014-10-31 22:42:44