
캐릭터가 점프 했을 시 파란 바닥에 착지 하거나, 공중일 때는 바닥으로 떨어져야 한다.
지금은 캐릭터의 상단 좌, 우 의 컬러를 감지해 벽과 충돌했는지 감지하는 leftColor 와 rightColor 가 있다. 바닥 충돌 감지를 위해서는 X,Y 값의 계산을 수정해야한다.

int bottomColor = image.getRGB(player.getX()+25, player.getY()+50 + 5);
			System.out.println("바닥 색상"+bottomColor);이렇게 코드를 추가한 뒤 실행해 보면
	int bottomColor = image.getRGB(player.getX(), player.getY()+50 + 5) // 왼쪽 하단
					+ image.getRGB(player.getX()+50, player.getY()+50 + 5); // 오른쪽 하단이 코드를 추가 하고 player 의 Down 메서드를 수정한다.
	if(bottomColor != -1) {
				System.out.println("바닥에 충돌함");
				player.setDown(false);
			}현재 for 문이 들어있기 때문에 false 로 상태가 변경 되더라도 메서드에 영향을 미치지 않기 때문에
while 로 변경하여 상태를 확인해야 한다.@Override
	public void down() {
		System.out.println("down");
		down = true;
		new Thread(()->{
			for(int i=0; i<130/JUMPSPEED; i++) {
				y = y + JUMPSPEED;
				setLocation(x, y);
				try {
					Thread.sleep(3);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			down = false;
			
		}).start();
	}


그렇다면 이번엔 바닥이 없을 때 하강을 시켜야 한다.
int bottomColor = image.getRGB(player.getX() + 25, player.getY()+50 + 5);
if(bottomColor != -1) {
				//System.out.println("바닥에 충돌함");
				player.setDown(false);
			}현재 바닥이 존재할 때 -1 이라는 bottomColor 를 추적하여 플레이어를 바닥에 착지 시키고 있다.
그렇다면 바닥이 존재하지 않는다면 down 을 시키면 될 것이다.
up 상태가 아닐 때 다운을 시켜야 캐릭터가 빈 바닥을 만났을 때 하강하게 된다.
(if문이 빠진다면 아무 변화가 없음)
else {
				if(!player.isUp()) {
					player.down();
				}
			}이렇게 설정하니 무한으로 down 이 실행되어 캐릭터가 프레임을 뚫고 추락해 버린다..
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
	at java.desktop/sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:314)
	at java.desktop/java.awt.image.BufferedImage.getRGB(BufferedImage.java:918)
	at bubble.test.ex09.BackGroundPlayerService.run(BackGroundPlayerService.java:31)
	at java.base/java.lang.Thread.run(Thread.java:1583)
움직임을 관리해주는 Player 클래스에 true, false 상태를 확인하지 않고
계속해서 실행되기 때문이다.
@Override
	public void down() {
		//System.out.println("down");
		down = true;
		new Thread(()->{
			while(down) {
				y = y + JUMPSPEED;
				setLocation(x, y);
				try {
					Thread.sleep(3);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			down = false;
			
		}).start();
	}	@Override
	public void down() {
		if(down==false) {
			//System.out.println("down");
			down = true;
			new Thread(()->{
				while(down) {
					y = y + JUMPSPEED;
					setLocation(x, y);
					try {
						Thread.sleep(3);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				down = false;
			}).start();
		}
		
	}이렇게 down 메서드에서 상태 체크를 하거나 if 문 자체에서 조건을 하나 더 추가해준다.
else {
				if(!player.isUp() && !player.isDown()) {
					System.out.println("down");
					player.down();
				}
			}
이렇게 움직임이 완성 되었다.
- 게임 맵과 캐릭터 추가 https://inblog.ai/hj/sts-버블버블-게임-맵과-캐릭터-추가하기-33126
- 스레드 사용하여 이동하기 https://inblog.ai/hj/sts-버블버블-스레드-사용이동-33335
- 외벽 충돌 시 캐릭터 정지 https://inblog.ai/hj/sts-버블버블-외벽-충돌-시-캐릭터-정지-36946
- 캐릭터 업 다운 시키기 https://inblog.ai/hj/sts-버블버블-캐릭터-업-다운-38143
- 물방울 생성하기(기본셋팅)
Share article

