2017-04-25 1 views
1

스프라이트를 0보다 작게 만들면 화면 가장자리에서 튀게 만들려고합니다. 지금은 그냥 화면을 가로 질러 보이드로 확대합니다. CentipedeBody는 Sprite를 확장 한 클래스입니다. render 메서드에서 나는 클래스의 개체 인 ex.update();을 호출합니다. 그 다음에 나는 당신의 자식 클래스의 경계, 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

가 다른 물체와의 충돌에 관심이 있다면 그. batch.begin() 사이 및 batch.end() 위치 및 크기와 동일하게 Child 클래스에 이러한 추가 데이터 멤버가 필요하다고 생각하지 않습니다. 위치는 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

관련 문제