2025년, 코딩은 선택이 아닌 필수!

2025년 모든 학교에서 코딩이 시작 됩니다. 먼저 준비하는 사람만이 기술을 선도해 갑니다~

메타버스 프로그래밍/레드브릭(redbrick)

레드브릭 사용법

파아란기쁨1 2023. 11. 13. 16:16
반응형

https://threejs.org/docs/#api/en/audio/Audio

 

three.js docs

 

threejs.org

https://redbrick.gitbook.io/wiki-main/snippet/undefined-7

 

타이머, 카운트다운 만들기 - REDBRICK WIKI

타이머, 카운트다운을 보여 줄 GUI 선택합니다.

redbrick.gitbook.io

시작할때 최초 한번 실행

const box = WORLD.getObject("BOX");

function Start() {
    PLAYER.onCollide(box, () => {
        box.kill();
    });
}

 

키보드 이벤트

function OnKeyDown(event) {
    switch (event.code) {
        case "KeyQ":
        	//Q키 입력 되었을때
            break;
        case "KeyR":
        	//R키 눌렸을 때
            break;
        default:
            break;
    }
}

function OnKeyUp(event) {
    // Similar to OnKeyDown
}

마우스 이벤트

function OnPointerDown(event) { //event.pointerType 을 통해 마우스,터치,터치펜 등을 알 수 있다
    if (event.button === 0) {
        fire(event.clientX, event.clientY);
    }
}

const fire = (x, y) => {...};
function OnPointerMove(event) { //event.clientX,event.clientY 를 통해 이동 좌표를 알 수 있다.
    // Similar to OnPointerDown
}

function OnPointerUp(event) {
    // Similar to OnPointerDown
}

매 프레임 반복 실행

function Update(dt) { //dt는 deltaTime
    this.position.y += dt * 2;
    if (this.position.y >= 10) {
        this.position.y = 0;
    }
}

 

사운드 플레이

const sound = WORLD.getObject("오브젝트명")
sound.getAudio().play();
sound.getAudio().stop();

 

플레이어 정지 및 속도 조절

PLAYER.changePlayerSpeed(0) // 플레이어 정지

PLAYER.changePlayerSpeed(2) //플레이어 속도 2로 변경 후
setTimeout(() => {
            PLAYER.changePlayerSpeed(1)
    }, 3000) //3초 후에 속도를 1로 변경

 

객체 크기 변경 코드

const gameover = GUI.getObject("die");
gameover.size.y.value=800;

단 Maintain AspectRatio 를 꺼 줘야 한다.

 

좌우로 반전

const char = getObject("char");
char.rotateY(-180);

 

물체를 반복적으로 왔다갔다 이동하는 방법

const char = getObject("char");
let dir = "R";

setInterval(() => {
    const pos = char.getPosition();
    if (pos.x >= 40 && dir === "R") {
        char.rotateY(-180);
        char.moveX(-80, 10);
        dir = "L";
    }
    else if (pos.x <= -40 && dir === "L") {
        char.rotateY(-180);
        char.moveX(80, 10);
        dir = "R";
    }
}, 100);

 

부딪혔는지 판단하기

const spawnbox = WORLD.getObject("BOX(b5d)");
spawnbox.onCollide(PLAYER, () => {
    //플레이어와 부딪혔을때 이벤트 발생
}, "collision");

 

원래 위치로 돌아오기

PLAYER.spawn();

 

아이템 획득

const coins = []; //리스트 생성
const showpoint = GUI.getObject("board_ao_f(f78)"); //텍스트박스(GUI)
const door = WORLD.getObject("furniture_dgdoor_003(5ab)");

for (let i = 0; i < 3; i++) {
    coins[i] = WORLD.getObject("coin_0" + (i + 1)); //coin_01,coin_02,coin_03 객체 배열에 삽입
}

coins.forEach(coin => {
   PLAYER.onCollide(coin, () => {
       coin.kill(); //PLAYER 에 coin 이 닿으면 삭제
       showpoint.setText(`모은 코인의 개수 : ${coinpoint} / 3`); //텍스트 박스에 출력
       if (coinpoint === 3) {
            door.move(0, 8, 0, 2);
        }
   });
});

 

버튼 클릭 이벤트

const startbtn = GUI.getObject("casual_a_button_d(dc0)");

function Start () {
    startbtn.onClick(() => {
        startbtn.hide();
    })
}
반응형