2013-11-14 1 views
0

나는 stackoverflow에 새로운 오전, 당신이 내 질문에 대답 할 수 있기를 바랍니다. 다음 코드는 움직입니다.나는 조건을 곱한

public MovementView(Context context) { 
    super(context); 
    getHolder().addCallback(this); 
    circleRadius = circleRadius2 = 50; 
    circlePaint = new Paint(); 
    circlePaint2 = new Paint(); 
    circlePaint.setColor(Color.GREEN); 
    circlePaint2.setColor(Color.RED);   
    xVel = 10; 
    yVel = 10; 
    xVel2 = 20; 
    yVel2 = 20; 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawColor(Color.WHITE); 
    canvas.drawCircle(xPos, yPos, circleRadius, circlePaint); 
    canvas.drawCircle(xPos2, yPos2, circleRadius2, circlePaint2); 
} 
public void updatePhysics() { 
    xPos += xVel; 
    yPos += yVel; 
    if (yPos - circleRadius < 0 || yPos + circleRadius > height) { 
     if (yPos - circleRadius < 0) { 
      yPos = circleRadius; 
     }else{ 
      yPos = height - circleRadius; 
     } 
     yVel *= -1; 
    } 
    if (xPos - circleRadius < 0 || xPos + circleRadius > width) { 
     if (xPos - circleRadius < 0) { 
      xPos = circleRadius; 
     } else { 
      xPos = width - circleRadius; 
     } 
     xVel *= -1; 
    }   
    xPos2 += xVel2; 
    yPos2 += yVel2; 
    if (yPos2 - circleRadius2 < 0 || yPos2 + circleRadius2 > height) { 
     if (yPos2 - circleRadius2 < 0) { 
      yPos2 = circleRadius2; 
     }else{ 
      yPos2 = height - circleRadius2; 
     } 
     yVel2 *= -1; 
    } 
    if (xPos2 - circleRadius2 < 0 || xPos2 + circleRadius2> width) { 
     if (xPos2 - circleRadius2 < 0) { 
      xPos2 = circleRadius2; 
     } else { 
      xPos2 = width - circleRadius2; 
     } 
     xVel2 *= -1; 
    } 

} 

public void surfaceCreated(SurfaceHolder holder) { 
    Rect surfaceFrame = holder.getSurfaceFrame(); 
    width = surfaceFrame.width(); 
    height = surfaceFrame.height(); 
    xPos = xPos2 = width/2; 
    yPos = yPos2 = circleRadius; 
    updateThread = new UpdateThread(this); 
    updateThread.setRunning(true); 
    updateThread.start(); 
} 
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
} 
public void surfaceDestroyed(SurfaceHolder holder) { 
    boolean retry = true; 
    updateThread.setRunning(false); 
    while (retry) { 
     try { 
      updateThread.join(); 
      retry = false; 
     } catch (InterruptedException e) { 
     } 
    } 
} 

이렇게하면 특정 테두리가있는 두 개의 원이 움직이는 것을 정의합니다. 이제는 약 10-20 개 정도의 원을 추가하고 싶습니다. 코드를 복사하는 대신 변수를 제공하는 것이 가능합니까 (두 원의 경우처럼)?

답변

0

이렇게하는 방법에는 여러 가지가 있습니다. 일반적인 접근법은 World 객체를 만들고 하나의 Circle 클래스를 만드는 것입니다.

그러면 세계는 원하는만큼 원형 개체를 만들 수 있습니다.

원을 이동하려면 "Physics"와 같은 다른 컨트롤 개체가 있거나 각 원에 부여 된 Movement Controller를 만들 수 있습니다. 그러면 세계는 각 동그라미에게 세계 캔버스로 이동할 가능성이있는 것을 요구할 것입니다.

관련 문제