게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
[안드로이드] OnTouch 작동이 왜 안할까요..?
게시물ID : programmer_14833짧은주소 복사하기
작성자 : 세레니드
추천 : 0
조회수 : 565회
댓글수 : 1개
등록시간 : 2015/12/07 05:45:13
옵션
  • 본인삭제금지

public class GameView extends View implements View.OnTouchListener {

int width, height; // 화면의 폭과 높이
static int x, y; // 캐릭터의 현재 좌표
int dx, dy; // 캐릭터가 이동할 방향과 거리
int charaterWidth, charaterHeight; // 캐릭터의 폭과 높이
int counter; // 루프 카운터
Bitmap character[] = new Bitmap[2]; // 캐릭터의 비트맵 이미지

//-----------------------------------
// Constructor - 게임 초기화
//-----------------------------------
public GameView(Context context) {
super(context);

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
width = display.getWidth(); // 화면의 가로폭
height = display.getHeight(); // 화면의 세로폭
x = 100; // 캐릭터의 현재 x위치
y = 100; // 캐릭터의 현재 y위치
dx = 4; // 캐릭터가 x축으로 이동할 거리
dy = 6; // 캐릭터가 y축으로 이동할 거리


// 캐릭터의 비트맵 읽기
character[0] = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
character[1] = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

charaterWidth = character[0].getWidth() / 2; // 캐릭터의 폭/2
charaterHeight = character[0].getHeight() / 2; // 캐릭터의 높이/2

mHandler.sendEmptyMessageDelayed(0, 10);
}

//-----------------------------------
// 실제 그림을 그리는 부분
//-----------------------------------
public void onDraw(Canvas canvas) {
x += dx; // 가로 방향으로 이동
y = height - charaterHeight; // 세로 위치 세팅
// y += dy; // 세로 방향으로 이동


if (x < charaterWidth || x > width - charaterWidth) dx = 0; // 좌우의 벽이면 방향을 바꿈
//if (y < charaterHeight || y > height - charaterHeight) dy = -dy; // 천정이거나 바닥이면 방향을 바꿈

counter++;
int n = counter % 20 / 10;



canvas.drawBitmap(character[n], x - charaterWidth, y - charaterHeight, null);
} // onDraw 끝

//------------------------------------
// Timer Handler
//------------------------------------
Handler mHandler = new Handler() { // 타이머로 사용할 Handler
public void handleMessage(Message msg) {
invalidate(); // onDraw() 다시 실행
mHandler.sendEmptyMessageDelayed(0, 10); // 10/1000초마다 실행
}
}; // Handler

@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {

x = x - 1000 ;
invalidate();
}
return true;
}
} // GameView 끝

// 프로그램 끝
 
 
 X 의 위치가 변화 되어야 할 거 같은데 인식이 안되네요..


전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호