2016-07-19 3 views
-1

누군가가 나를 깨달을 수 있습니까?화면에서 물체를 움직이는 데 문제가 발생했습니다.

나는 공을 자동으로 만들어서 한 모서리에서 다른 모서리로 이동해야하는 게임을하고 있습니다.

나는 공을 만들고 오른쪽 아래 모서리에서 왼쪽 위 모퉁이로 움직일 수 있었지만, 왼쪽 모서리에서 오른쪽 상단 모서리로하는 방법을 알아낼 수 없었다.

첨부 된 코드 중 일부는 내 논리의 그래프입니다.

case 1: // From one side to the other, in two axes 

     switch (subScenario) { 
     case 0: // Bottom right corner to top left corner. 
      posX = panelWidth; 
      posY = panelHeight; 
      directionX = (rnd.nextInt(2) + 1)*-1; 
      directionY = directionX; 
     case 1: // Top left corner to bottom right 
      posX = 0; 
      posY = 0; 
      directionX = (rnd.nextInt(2) + 1)*1; 
      directionY = directionX; 
     case 2: // Bottom left corner to top right corner 
      posX = 0; 
      posY = panelHeight; 
      directionX = directionY; 
      directionY = (rnd.nextInt(2) + 1)*-1; 
     } 
     break; 
    } 
+0

그렇다면 오류는 어디에서 발생합니까? –

+0

케이스 2에서 directionX와 directionY 라인을 전환 해보십시오. – 17slim

+0

마지막 코멘트를 잊어 버리 셨습니다. 잠시 생각하지 못했습니다. 'directionX = (rnd.nextInt (2) + 1) * 1'을 설정하고'directionY'을 그대로 두십시오. – 17slim

답변

1

당신이 원하는 경우 Logic of how the balls will movecase 2 같이 directionX는 양수 여야, 왼쪽에서 오른쪽으로 이동합니다. 따라서 :

directionX = (rnd.nextInt(2) + 1)*1; 

directionY 때문에, 부정적인해야한다 :

directionY = directionX*-1; 

을 또한, 그들은 모두 활성화하지 않도록 각 casebreak을 넣어 기억합니다.

+0

아아, 고마워요. :) – Aaron

관련 문제