2017-04-25 2 views
1

スプライトが0より小さい場合は、スプライトが画面の端からはね返るようにしようとしています。今は画面全体を空白にズームするだけです。 CentipedeBodyはSpriteを拡張するクラスです。 renderメソッドでは、クラスのオブジェクトであるex.update();を呼び出します。その後batch.begin()batch.end()の間に私はあなたの子供のクラスの境界、Spriteはすでに限界を持つが、あなたが他のオブジェクトとの衝突に興味を持っている場合は、その使用する理由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)'を実行できますか?最後の試合では、スプライトが左に移動し、画面の端にぶつかり、ちょっと下り、もう一方の端に右に移動し、繰り返します。今私はちょうどそれが端から跳ね返る得る少なくともほしいと思う。 – retchers

+0

画面の端でタイルと何らかのタイプのタイル衝突を実行できますか? – retchers

答えて

1

を持っています。位置やサイズと同じですが、私はあなたのChildクラスでこれらの余分なデータメンバーが必要ではないと考えています。xy、位置は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で境界、サイズと位置を必要とするかもしれませあなたは簡単に私の中にあなたの変数を統合することができますので、私は1つのクラスのコードによって、あなたのゲームの要件を判断できないレンダリングコード。

+0

あなたの助けてくれてありがとう、私のスプライトが斜めに動いている理由を知っていますか?私の座標系がセルのxとyに基づいていることを思い出してください。 – retchers

+0

は斜めに動いていますか?どうやって ?あなたは何かを逃しているかもしれない。 – Aryan

+1

あなたは素晴らしいです!私の問題は、私のタイルマップでy軸が上向きで、下向きではないことに気づいたことでした。 – retchers

関連する問題