2017-04-25 52 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