2014-11-04 3 views
0

게임에서 캐릭터의 움직임을 움직이는 데 문제가 있습니다. 16 요소의 이미지 배열을 사용하고 있습니다.이미지 배열을 사용하는 캐릭터 애니메이션

첫 번째 사이클이 잘 진행되지만 그 이후에는 캐릭터가 움직이지 않습니다. 그것은 이동하지만 그 사이클의 첫 번째 요소를 보여주는 이미지를 변경하지는 않습니다. 방향을 바꾸면 한 싸이클이 다시 잘 돌아가고 같은 방향으로 멈 춥니 다.

아이디어가 있으십니까? 어떤 방향이 변경되지 않은 경우 당신은 스프라이트를 업데이트하지 않는

public void paintHero(Graphics g) { 
    // paint character 
    offG.drawImage(heroCurrent, hero.getHeroX(), hero.getHeroY(), this); 
    // show first image of the cycle where the character stands 
    if (hero.getMoveX() == 0 && hero.getMoveY() == 0) 
     heroFrame = 0; 
    else { 
     heroFrame++; 
     if (heroFrame == 16) 
      heroFrame = 0; 
     // I'm dividing heroFrame by 4 to slow down the animation 
     if (hero.isMovingUp() == true) 
      heroCurrent = heroSprites[4 + heroFrame/4]; 
     if (hero.isMovingDown() == true) 
      heroCurrent = heroSprites[0 + heroFrame/4]; 
     if (hero.isMovingLeft() == true) 
      heroCurrent = heroSprites[8 + heroFrame/4]; 
     if (hero.isMovingRight() == true) 
      heroCurrent = heroSprites[12 + heroFrame/4]; 
    } 
} 
+0

은 작동 방법을 변경하지 않지만 'hero.isMovingUp() == true'를 수행 할 필요는 없습니다. 'if (hero.isMovingUp())'도 마찬가지로 작동합니다. – DoubleDouble

+0

thats true :) thx – Kokufuu

답변

0

나는 일하게 만들었다. 그러나 그것이 가장 잘하는 것은 정말로 그것이 지금 일하는 이유를 모른다. 나는 각각의 if 문 다음에 draw를 추가하고 첫 번째 문장을 유지했다. 누군가가 크게 감사 할 것이라고 설명 할 수 있다면.

public void paintHero(Graphics g) { 

    offG.drawImage(heroCurrent, hero.getHeroX(), hero.getHeroY(), this); 

    if (hero.getMoveX() == 0 && hero.getMoveY() == 0) 
     heroFrame = 0; 
    else { 
     heroFrame++; 
     if (heroFrame == 16) 
      heroFrame = 0; 
    } 

    if (hero.isMovingUp()) { 
     heroCurrent = heroSprites[4 + heroFrame/4]; 
     offG.drawImage(heroCurrent, hero.getHeroX(), hero.getHeroY(), this); 
    } 
    if (hero.isMovingDown()) { 
     heroCurrent = heroSprites[0 + heroFrame/4]; 
     offG.drawImage(heroCurrent, hero.getHeroX(), hero.getHeroY(), this); 
    } 
    if (hero.isMovingLeft()) { 
     heroCurrent = heroSprites[8 + heroFrame/4]; 
     offG.drawImage(heroCurrent, hero.getHeroX(), hero.getHeroY(), this); 
    } 
    if (hero.isMovingRight()) { 
     heroCurrent = heroSprites[12 + heroFrame/4]; 
     offG.drawImage(heroCurrent, hero.getHeroX(), hero.getHeroY(), this); 
    } 
} 
0

:

여기 내 코드입니다. 그것은이어야한다 :

public void paintHero(Graphics g) { 
    // paint character 
    offG.drawImage(heroCurrent, hero.getHeroX(), hero.getHeroY(), this); 

    //always update frame 
    heroFrame++; 
    if (heroFrame == 16) 
     heroFrame = 0; 

    // show first image of the cycle where the character stands 
    if (hero.getMoveX() == 0 && hero.getMoveY() == 0) 
     heroCurrent = heroSprites[heroFrame];//update even if character isnt moving 
    else { 
     // I'm dividing heroFrame by 4 to slow down the animation 
     if (hero.isMovingUp()) 
      heroCurrent = heroSprites[4 + heroFrame/4]; 
     else if (hero.isMovingDown()) 
      heroCurrent = heroSprites[0 + heroFrame/4]; 
     else if (hero.isMovingLeft()) 
      heroCurrent = heroSprites[8 + heroFrame/4]; 
     else if (hero.isMovingRight()) 
      heroCurrent = heroSprites[12 + heroFrame/4]; 
     else//stand still 
      heroCurrent = heroSprites[heroFrame]; 
    } 
} 
+0

편집하기 전에 답을 써도 안타깝게도 여전히 동일합니다. 한 사이클 만지나 갔다. 이제 편집 후 heroSprites [16]에 액세스하려고하기 때문에 오른쪽으로 이동할 때 IndexOutOfBound를 얻습니다. – Kokufuu

+0

@Kokufuu 어때? –

+0

이제는 그것이 멈춰야 할 때 주위를 계속해서 뛰어 다니고 있습니다. : D 그리고 움직일 때 그것은 여전히 ​​똑같은 문제입니다. – Kokufuu

관련 문제