2017-04-25 51 views
1

我試圖讓雪碧在小於0時從屏幕邊緣反彈。現在它只是將屏幕放大到虛空。這裏是我的代碼注意,CentipedeBody是一個擴展Sprite的類。在render方法中,我稱之爲ex.update();,它是該類的一個對象。然後在batch.begin()batch.end()之間我有batch.draw(ex,ex.getPosition().x,ex.getPosition().y,ex.getSize().x,ex.getSize().y);讓雪碧從邊緣反彈

public class CentipedeBody extends Sprite 
{ 
    public CentipedeBody(TextureRegion image,Vector2 position,Vector2 size) { 

     super(new TextureRegion(image)); 

     this.position = position; 
     this.size=size; 
     bounds=new Rectangle(position.x,position.y,8,8); 
     left=true; 
    } 

    public void update() { 

     bounds.set(getPosition().x,getPosition().y,8,8); 

     if (left==true) { 
      position.x-=(.5f); 
      up=false; 
      down=false; 
      right=false; 
      left=true; 
     } 

     if (right==true) { 
      position.x+=.5f; 
      left=false; 
      right=true; 
      down=false; 
      up=false; 
     } 
     if (down==true) { 

      position.y-=(.5f); 
      right=false; 
      left=false; 
      down=true; 
      up=false; 

      if(position.x<0) 
      { 
       left=false; 
       right=true; 
      } 
     } 
    } 
+0

根據你的代碼片段只有'if(left){}'執行,所以你的精靈只向左移動 – Aryan

+0

那麼我會如何讓if(right)'execute?對於最後一場比賽,我希望精靈向左移動,擊中屏幕邊緣,稍微向下,然後向右移動到另一個邊緣,然後重複。現在我只想至少讓它從邊緣反彈。 – retchers

+0

我們可以通過屏幕邊緣與瓦片進行某種類型的瓦片碰撞嗎? – retchers

回答

1

爲什麼界限在你的子類,在已經界限Sprite,使用,如果你有興趣與其他物體碰撞。相同的位置和大小,我不認爲你需要這些額外的數據成員在您的孩子類,使用家長x,y位置和widthheight維。

public class CentipedeBody extends Sprite { 

    enum State{ 
     LEFT,RIGHT,DOWN 
    } 

    State currentState,previousState ; 

    public static final float DOWN_MOVEMENT=50; 
    public float downMovCounter; 
    public float speed; 

    public CentipedeBody(TextureRegion image, Vector2 position, Vector2 size) { 
     super(new TextureRegion(image)); 
     setPosition(position.x,position.y); 
     setSize(size.x,size.y); 
     currentState=State.LEFT; 
     previousState=State.LEFT; 
     speed=50; 
    } 

    public void update() { 

     float delta=Gdx.graphics.getDeltaTime(); 
     if(currentState ==State.LEFT){ 
      setPosition(getX()-speed*delta,getY()); 
      if(getX()<0) { 
       previousState=currentState; 
       currentState = State.DOWN; 
      } 
     } 

     if(currentState ==State.RIGHT){ 
      setPosition(getX()+speed*delta,getY()); 
      if(getX()> Gdx.graphics.getWidth()-getWidth()) { 
       previousState=currentState; 
       currentState = State.DOWN; 
      } 
     } 

     if(currentState ==State.DOWN){ 
      setPosition(getX(),getY()+speed*delta); 
      downMovCounter++; 
      if(downMovCounter>DOWN_MOVEMENT){ 
       downMovCounter=0; 
       currentState =previousState==State.LEFT?State.RIGHT:State.LEFT; 
      } 

     } 
    } 

} 

在渲染方法

batch.begin(); 
batch.draw(centipedeBody,centipedeBody.getX(),centipedeBody.getY(),centipedeBody.getWidth(),centipedeBody.getHeight()); 
batch.end(); 
centipedeBody.update(); 

可能是你需要在CentipedeBody界限,大小和位置,我無法判斷一個類代碼你的遊戲要求,這樣你就可以輕鬆地將您的變量我碼。

+0

謝謝你的幫助,但你知道我的精靈爲什麼對角線移動嗎?請記住我的座標系基於單元格的x和y。 – retchers

+0

對角線移動?怎麼樣 ?可能是你錯過了一些東西。 – Aryan

+1

你真了不起!我的問題是,我在我的瓷磚地圖中意識到y軸向上,而不是向下。 – retchers