2010-07-11 4 views
1

저는 여러 곳에서 해결책을 모색하고 있습니다. 텍스트 파일에 애니메이션을 저장하는 Nick Gravelyn의 스타일을 사용했고 단순히 프레임을 변경하기 위해 색인을 증가시킵니다. 내가 가지고있는 문제는 단 한 번만 애니메이션을 반복하는 것이지만 어떤 이유로 작동해야한다고 생각하는 방식이 내가 생각했던대로 작동하지 않습니다. 왜 XNA가 작동하는지에 대해 구체적이지 않은 한, 내 인생에서 그 이유를 알아낼 수는 없습니다.XNA 파이팅 게임 스타일 애니메이션 반복 문제

private void UpdateAttack(KeyboardState current, KeyboardState last, GameTime gameTime) 
     { 
      if (current.IsKeyDown(Keys.S) && last.IsKeyUp(Keys.S)) 
      { 
       neutralStandingKick(gameTime); 
      } 
     } 

     private void neutralStandingKick(GameTime gameTime) 
     { 
      //timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds; //Framerate control 
      //if (timeSinceLastFrame > millisecondsPerFrame) //Framerate control 
      //{ 
       //timeSinceLastFrame -= millisecondsPerFrame; //Framerate control 
       if (mCurrentState != State.Kicking) 
       { 
        mCurrentState = State.Kicking; 

        Position.Y = 200; 

        loopOnce(25, 30); //Starts at frame 25, ends at 30 
       } 
      //} 
     } 

     private void loopOnce(int min, int max) 
     { 
      if (currentImageIndex > max || currentImageIndex < min) //Checks to see if index out of range of current animation 
       currentImageIndex = min; //Starts at the beginning of the animation 
      for (int i = min; i < max; i++) //Uses the range to determine when to stop 
      { currentImageIndex++; } //Increments index each iteration that passes 
     } 

편집 : 여기

내 코드입니다 여기에이 특정 클래스

public void Draw(SpriteBatch spriteBatch) 
{ 
    //Get the name of the current sprite to draw 
    string spriteName = possibleImages[currentImageIndex]; 

    //Use that to get the source rectangle 
    Rectangle source = spriteSourceRectangles[spriteName]; 

    //Send rectangle to a function to set the framesize for bounds checking 
    getFrameSize(source); 

    spriteBatch.Draw(theSpriteSheet, Position, source, Color.White); 
} 

private void getFrameSize(Rectangle frame) 
{ 
    frameSize = frame; //For bounds checking 
} 

의 Draw 메서드 왜 것없는이 작품?

새로운 코드 (개빈의 제안) : UpdateAttack를 호출

private void UpdateAttack(KeyboardState current, KeyboardState last, GameTime gameTime) 
{ 
    const int min = 22; 
    const int max = 30; 

    timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds; //Framerate control 

      if (current.IsKeyDown(Keys.S) && mCurrentState != State.Kicking) 
      { 
       mCurrentState = State.Kicking; 
       currentImageIndex = min; 
      } 
      if (mCurrentState == State.Kicking) 
      { 
       if (timeSinceLastFrame > millisecondsPerFrame) //Framerate control 
       { 
        timeSinceLastFrame -= millisecondsPerFrame; //Framerate control 
        currentImageIndex++; 
       } 
      } 
      if (currentImageIndex == max) 
       mCurrentState = State.Idle; 
} 

방법 :

그것은 "S"아래로 키보드 키를 누른 상태에서 루프 애니메이션을 것이다
public void Update(GameTime theGameTime, Game game) 
{ 
    KeyboardState aCurrentKeyboardState = Keyboard.GetState(); 

    UpdateMovement(aCurrentKeyboardState); 
    UpdateJump(aCurrentKeyboardState); 
    UpdateAttack(aCurrentKeyboardState, mPreviousKeyboardState, theGameTime); 
    UpdateStageBounds(game); 

    mPreviousKeyboardState = aCurrentKeyboardState; 
} 

. 그러나 그것이 예정되어있는 것처럼 1 키 프레스에서 모든 7 프레임을 반복하지 않습니다.

+0

효과가 없습니까? 그것은 한 번 이상 루핑되었거나 루핑되지 않았습니까? – Gavin

+0

지정하지 않은 점 사과드립니다. 문제는 키를 누를 때 for 루프를 사용하여 프레임을 반복하지 않는다는 것입니다. 대신, 그것은 고정 된 애니메이션의 단지 1 프레임만을 보여줍니다. 아마도, 당신이 아래에서 말했듯이 마지막 프레임 일 것입니다. –

답변

0

게시 된 코드에서 루프 애니메이션의 for 루프가 모든 프레임에서 반복되고 마지막 프레임 만 그려지기 때문에 kick 애니메이션의 마지막 프레임을 그릴 것입니다. 이는 각 업데이트마다 draw 메서드가 한 번 호출되기 때문입니다.

XNA는 먼저 업데이트 메서드를 호출 한 다음 draw 메서드를 호출합니다. 따라서 업데이트 방법에서 애니메이션의 현재 프레임을 최대 프레임으로 설정 한 다음 그리기 방법으로 이동하여 현재 프레임을 마지막 프레임으로 그립니다.

이 기능을 사용하려면 업데이트 할 각 호출마다 하나씩 그리려는 프레임 만 증가시켜야합니다.

나는 그래서이 테스트 기운이 컴퓨터에 XNA이 없어 아마 습관이 그것을

private void UpdateAttack(KeyboardState current, KeyboardState last, GameTime gameTime) 
     { 
      if (current.IsKeyDown(Keys.S) && last.IsKeyUp(Keys.S)) 
      { 
      mCurrentState =State.Kicking; 
      currentImageIndex=25; 
      } 
      if(mCurrentState ==State.Kicking) 
       neutralStandingKick(gameTime);    
     } 

    private void neutralStandingKick(GameTime gameTime) 
    { 
      currentImageIndex++; 
      if(currentImageIndex==30) mCurrentStart = State.SomeOtherState; 
    } 

드로우를 제거해야처럼 당신에게 아이디어 뭔가를 제공 할 수 있지만 구문 오류를 수정하지 않고 컴파일 한번 방법. State.SomeOtherState를 객체가 차기 상태가 아닌 상태로 바꿔야합니다. 아마도 하드 코딩 된 프레임 번호를 상수로 바꾸기를 원할 것입니다. 이 애니메이션이 작동하려면 무승부 방법을 변경하지 않아야합니다. 희망이 도움이됩니다.

+0

정말 빨리이 클래스의 일부인 그리기 메서드를 게시 해 보겠습니다. 나는 XNA 관련 질문을하는 것에 조금 새로운 것이다. 더블 엑스; –

+0

네, 무슨 뜻인지 이해합니다. 나는 이것을 놓친다 고 나는 믿을 수 없다. for 루프를 사용하여 발길질 애니메이션의 X 프레임 양을 사용할 수 있다고 생각했습니다. 업데이트 메서드는 루프마다 한 번만 호출되므로 for 루프의 한 프레임 만 그리기 메서드에 전달된다는 사실을 잊어 버렸습니다. 도움을 주셔서 감사합니다.이 문제를 해결하기위한 다른 접근 방식을 찾아 보겠습니다. –

+0

개빈 감사합니다. 나는 지금 당장 이것을 시도 할 것이다. –