mouse_check_button_pressed 함수 설명
mouse_check_button_pressed
함수는 특정 마우스 버튼이 눌렸는지를 확인하는 기능을 제공합니다. 이 함수는 마우스 버튼이 처음 눌렸을 때만 true
를 반환하며, 버튼이 다시 눌리기 위해서는 먼저 버튼이 해제되어야 합니다. 이 함수는 마우스 이벤트가 발생하는 모든 인스턴스에서 사용될 수 있습니다.
함수 구문
mouse_check_button_pressed(numb);
인자 설명
인자명 | 타입 | 설명 |
---|---|---|
numb | 마우스 버튼 상수 | 확인할 마우스 버튼 상수 |
반환값
- Boolean: 버튼이 눌렸다면
true
, 그렇지 않다면false
를 반환합니다.
예제 코드
아래 코드는 왼쪽 마우스 버튼이 눌렸는지를 확인하고, 눌렸다면 점수에 50을 추가합니다.
if (mouse_check_button_pressed(mb_left)) {
score += 50;
}
활용 및 응용 예제
- 게임에서 아이템 수집
- 사용자가 아이템을 클릭할 때마다 점수를 추가하는 기능을 구현할 수 있습니다.
gml if (mouse_check_button_pressed(mb_left)) { if (position_meeting(mouse_x, mouse_y, item)) { score += 100; instance_destroy(item); } }
- 버튼 클릭 이벤트
- UI 버튼을 클릭했을 때 특정 기능을 실행하는 예제입니다.
gml if (mouse_check_button_pressed(mb_left)) { if (mouse_x > button_x && mouse_x < button_x + button_width && mouse_y > button_y && mouse_y < button_y + button_height) { start_game(); } }
- 드래그 앤 드롭 기능
- 마우스 버튼을 눌렀을 때 오브젝트를 드래그하는 기능을 구현할 수 있습니다.
gml if (mouse_check_button_pressed(mb_left)) { dragging = true; } if (dragging) { x = mouse_x; y = mouse_y; } if (mouse_check_button_released(mb_left)) { dragging = false; }
- 게임 내 상호작용
- 특정 오브젝트와의 상호작용을 처리하는 예제입니다.
gml if (mouse_check_button_pressed(mb_left)) { if (instance_exists(npc)) { npc.talk(); } }
- 마우스 클릭으로 화면 전환
- 마우스 클릭으로 다른 방으로 전환하는 기능을 구현할 수 있습니다.
gml if (mouse_check_button_pressed(mb_left)) { room_goto(next_room); }