2014-06-18 3 views
2

플레이어를 왼쪽 및 오른쪽으로 움직이는 코드를 작성하고 플레이어가 왼쪽으로 이동하면 하나의 애니메이션이 있고 오른쪽으로 이동하면 다른 애니메이션이 생깁니다. LibGDX 가속도계 - Android

은 (DT가 deltaTime입니다) 코드입니다 :

// Moving player desktop 
    if (Gdx.input.isKeyPressed(Keys.A)) // Left 
    { 
     position.x -= speed * dt; 
     currentFrame = animation.getKeyFrame(4 + (int) stateTime); 
    } 
    else if (Gdx.input.isKeyPressed(Keys.D)) // Right 
    { 
     position.x += speed * dt; 
     currentFrame = animation.getKeyFrame(8 + (int) stateTime); 
    } 
    else // Normal 
    { 
     currentFrame = animation.getKeyFrame(12); 
    } 

그래서 나는 또한 가속도계 (모바일 기기) 플레이어를 이동하려는. 나는 그 일을하지만 지금은 플레이어가 왼쪽으로 이동 또는 오른쪽으로 그에게 다른 애니메이션

을주고 있는지 확인하는 방법을 모르는

내 코드 :

// Moving player android 
    // position.x -= Gdx.input.getAccelerometerX() * speed * dt; 

    if(PLAYER MOVING LEFT) 
    { 
     // Player moving left.... 
     currentFrame = animation.getKeyFrame(4 + (int) stateTime); 
    } 
    else if (PLAYER MOVING RIGHT) 
    { 
     // Player moving right.... 
     currentFrame = animation.getKeyFrame(8 + (int) stateTime); 
    } 
    else 
     currentFrame = animation.getKeyFrame(12); 

답변

2

게임 경우는 방향의 따라 달라집니다.

position.x -= Gdx.input.getAccelerometerX() * speed * dt; 

getAccelerometerX 값이 [-10,10]의 값을 반환하기 때문에이 구현이 좋게 보입니다.

그러나 휴대 전화가 테이블에 있다면, 그 값은 정확히 0이 아닙니다. 가속도가 0.3f 이상일 때 플레이어를 움직이게한다고 가정 해 봅시다.

float acceleration=Gdx.input.getAccelerometerX(); // you can change it later to Y or Z, depending of the axis you want. 


     if (Math.abs(acceleration) > 0.3f) // the accelerometer value is < -0.3 and > 0.3 , this means that is not really stable and the position should move 
     { 
      position.x -= acceleration * speed * dt; // we move it 
      // now check for the animations 


      if (acceleration < 0) // if the acceleration is negative 
       currentFrame = animation.getKeyFrame(4 + (int) stateTime); 
      else 
       currentFrame = animation.getKeyFrame(8 + (int) stateTime); 
      // this might be exactly backwards, you'll check for it 
     } else { 
      // the sensor has some small movements, probably the device is not moving so we want to put the idle animation 
      currentFrame = animation.getKeyFrame(12); 
     } 
+1

고마워,이 작품은 다음과 같습니다 : D 그냥 달 산책처럼 가고 있기 때문에 애니메이션 (8-4)을 변경하십시오 : D – KiKo