조이스틱이 있으면 큰 원 안에 작은 원이 있어서 그걸 터치해서 움직이면 큰 원을 벗어나지 않는 범위에서 손가락의 움직임을 따라가잖아요? 그거 만들려고 하는데,
다음은 그 소스입니다.
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
e_x=event.getX();
e_y= event.getY();
switch(action){
case MotionEvent.ACTION_MOVE:
if (e_x > this.x && e_x < this.x + cont.getWidth()
&& e_y > this.y && e_y < this.y + cont.getHeight()) {
a=1;//터치가 작은 원의 범위애서 이루어짐
}
if(a==1){
this.x = e_x ; //터치점의 x좌표
this.y = e_y ; //터치점의 y좌표
}
if((e_x-45)*(e_x-45)>r*r-(e_y-(y_b+cont_b.getHeight()/2))*(e_y-(y_b+cont_b.getHeight()/2))){
this.x=(float) Math.sqrt(r*r-(this.y-(y_b+cont_b.getHeight()/2))*(this.y-(y_b+cont_b.getHeight()/2)))+45;
} //(x-a)^2+(y-b)^2=r^2 응용한 것임. this.x는 비트맵 작은 원 이 그려질 x좌표, this.y는 비트맵 cont가 그려질 y좌표이다. r는 큰 원의 반지름.y_b는큰 원의 중심의 y좌표임. cont_b는 비트맵 큰 원임.
if((e_y-(y_b+cont_b.getHeight()/2))*(e_y-(y_b+cont_b.getHeight()/2))>r*r-(e_x-45)*(e_x-45)){
this.y=(float) Math.sqrt(r*r-(this.x-45)*(this.x-45))+(y_b+cont_b.getHeight()/2);
}
invalidate();
break;
case MotionEvent.ACTION_UP:
this.x=cont_b.getWidth()/2-cont.getWidth()/2;
this.y=y_b+cont_b.getHeight()/2-cont.getHeight()/2;
e_x=0;
e_y=0;
a=0;
break;
};
return true;
이렇게 하면 제대로 안 움직이던데 무엇이 잘못된 걸까요???