에 작업 코드에 따라, 그것에 대해 좀 도와 줄래 나의 탁구 게임에서 샘플 코드입니다. 각 객체는 위치 (x, y)와 속도를가집니다. 움직임은 위치에 속도가 적용됩니다. 한 가지 추가 정보는 화면 좌표계가 왼쪽 상단 구석에서 시작한다는 것입니다. Y 축은 아래로 내려가지만 증가합니다. 화면 좌표는 0부터 시작하기 때문에, 경계는 screen.width 될 -1 screen.height -1
@Override
public void onDraw(Canvas c){
p.setStyle(Style.FILL_AND_STROKE);
p.setColor(0xff00ff00);
// If ball's current x location plus the ball diameter is greater than screen width - 1, then make speed on x axis negative. Because you hit the right side and will bounce back to left.
if((Px+ballDiameter) > width - 1){
Vx = -Vx;
}
// If ball's current y location plus the ball diameter is greater than screen height -1, then make speed on y axis negative. Because you hit the bottom and you will go back up.
if((Py + ballDiameter) > height - 1){
Vy = -Vy;
}
// If current x location of the ball minus ball diameter is less than 1, then make the speed on x axis nagative. Because you hit to the left side of the screen and the ball would bounce back to the right side
if((Px - ballDiameter) < 1){
Vx = -Vx;
}
// If current y location of the ball minus ball diameter is less than 1, then make the speed on y axis negative. Because the ball hit the top of the screen and it should go down.
if((Py - ballDiameter) <1){
Dy = 1;
Vy = -Vy;
}
Px += Vx; // increase x position by speed
Py += Vy; // increase y position by speed
c.drawCircle(Px, Py, ballDiameter, p);
invalidate();
}
출처
2013-06-17 14:58:29
neo
감사 그것이 정말 나에게 – koraxis
도움을 내가 듣고 기뻐요. – neo