sprite_exists 함수 설명 및 활용 예제
sprite_exists 함수는 지정된 인덱스의 스프라이트가 현재 실행 중인 프로젝트에 존재하는지를 확인하는 기능을 제공합니다.
문법
sprite_exists(index);
인수 설명
| 인수 | 유형 | 설명 |
|---|---|---|
| index | Sprite Asset | 확인할 스프라이트의 인덱스입니다. |
반환값
- Boolean: 스프라이트가 존재하면
true, 존재하지 않으면false를 반환합니다.
예제
다음 코드는 배열을 확인하여 유효한 스프라이트 인덱스가 있는지 검사하고, 유효한 경우 해당 스프라이트를 인스턴스에 할당합니다. 유효하지 않은 경우 포함된 리소스에서 기본 스프라이트를 할당합니다.
if sprite_exists(spr_array[0]) {
sprite_index = spr_array[0];
} else {
sprite_index = spr_BaseSprite;
}
활용 예제
예제 1: 스프라이트 선택
var selected_sprite_index = 1; // 선택한 스프라이트 인덱스
if sprite_exists(selected_sprite_index) {
sprite_index = selected_sprite_index; // 선택한 스프라이트가 존재하면 할당
} else {
sprite_index = spr_Default; // 기본 스프라이트로 대체
}
예제 2: 스프라이트 배열에서 유효한 스프라이트 찾기
for (var i = 0; i < array_length(spr_array); i++) {
if sprite_exists(spr_array[i]) {
sprite_index = spr_array[i]; // 유효한 스프라이트를 찾으면 할당
break; // 첫 번째 유효한 스프라이트를 찾으면 루프 종료
}
}
예제 3: 스프라이트 로딩 및 확인
var new_sprite_index = sprite_add("path/to/sprite.png", 1, false, false, 0, 0);
if sprite_exists(new_sprite_index) {
sprite_index = new_sprite_index; // 새로 로드한 스프라이트가 존재하면 할당
} else {
sprite_index = spr_Error; // 오류 스프라이트로 대체
}
예제 4: 게임 시작 시 스프라이트 초기화
if sprite_exists(spr_Player) {
sprite_index = spr_Player; // 플레이어 스프라이트가 존재하면 할당
} else {
show_error("플레이어 스프라이트가 존재하지 않습니다!", true); // 오류 메시지 표시
}
이와 같이 sprite_exists 함수를 활용하여 스프라이트의 존재 여부를 확인하고, 적절한 스프라이트를 인스턴스에 할당하는 다양한 방법을 사용할 수 있습니다.