My footsteps
자바스크립트 / 14 본문
728x90
this.#speed = Math.floor(Math.random()*5)+1;
//1부터~5까지의 속도를 원하니까 랜덤값을 주는데,랜덤은 0부터 시작하니까,5를 일단 곱하고
//0부터 시작이 아닌 1부터 시작을 해야 하니까 +1도 해주고
//랜덤값은 실수이기 때문에 Math.floor를 통해 정수로 바꿔준다
- 사건이 일어난것 = 이벤트
- 받아놓고 있다가 나중에 호출하는것을 콜백함수 라고 한다(callback function)
//애너미
this.#onOutOfScreen = null;
//영역을 벗어나는 이벤트(=outofScreen)가 발생했다
if(this.#onOutOfScreen) //null일때
this.#onOutOfScreen(this);
//enemy의this
set onOutOfScreen(callback){
//이 콜백은 게임켄버스에서 받아온 콜백함수.(아래껄 받아온것)
// enemy.onOutOfScreen = function(target){ //target이 enemy의 this가됨
// console.log(target+"범위를 벗어났다");
// }
this.#onOutOfScreen = callback;
}
//캔버스
enemy.onOutOfScreen = function(target){ //target이 enemy의 this가됨
console.log(target+"범위를 벗어났다");
}
- 니가 못한걸 내가 할테니까 넌 나중에 알려줘 = 콜백
enemy.onOutOfScreen = this.onOutOfScreenHandler.bind(this);
//함수가 중첩되면 코드가 복잡해지니, 밑에처럼 따로 빼서 여기서 호출만 해주면 된다. 근데 ()붙이면 오류남
//오류나는 이유는 함수 그대로의 자체를 넘겨줘야하는데 ( )하면 함수가 실행이 되어버리니까?
onOutOfScreenHandler(target){
//funtion을 쓰면 중첩이 깊어진다
console.log(target+"범위를 벗어났다");
console.log(this.#enemies);
}
- findIndex : 인덱스를 찾아줌
let idxToRemove = this.#enemies.indexOf(target);
//target은 에너미 객체다..indexof가 500이넘은 그 enemy만 찾아서 idxToRemove에 담아준다
this.#enemies.splice(idxToRemove, 1);
//인덱스 하나만 삭제해라
//indexOf로 target의 인덱스를 찾으면서 한개씩 자른다
728x90
'국비수업 > 수업정리' 카테고리의 다른 글
자바스크립트 / 16 (0) | 2023.04.12 |
---|---|
자바스크립트 / 15 (0) | 2023.04.11 |
자바스크립트 / 13 (0) | 2023.04.07 |
git / 2 (0) | 2023.04.07 |
자바스크립트 / 12 (0) | 2023.04.05 |