• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 JavaScript, TS 게시판 js es6 마지막 강의 질문입니다.

js es6 마지막 강의 질문입니다.

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 글쓴이
  • #15642

    김병태
    참가자

     

     

    html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <canvas id = "canvas"></canvas>
        <script src = "main.js"></script>
    </body>
    </html>

     

     

     

    main.js
    <blockquote>var canvas = document.getElementById('canvas');

    var ctx = canvas.getContext('2d');

    canvas.width = window.innerWidth-100;
    canvas.height = window.innerHeight-100;

    var dino = {
        x:10,
        y:200,
        width:50,
        height:50,
        draw(){
            ctx.fillStyle='green';
            ctx.fillRect(this.x, this.y, this.width, this.height);

        }
    }

    // dino.x += 1; 이거보다 라이브러리 쓰는게 더 좋다.

    //dino.draw();  밑의 함수로 이동

    class Cactus{
        constructor(){
            this.x = 500;
            this.y = 200;
            this.width=50;
            this.height=50;
        }
        draw(){
            ctx.fillStyle='red';
            ctx.fillRect(this.x, this.y, this.width, this.height);
        } 
    }

    // var cactus = new Cactus();
    // cactus.draw()

    var timer = 0;
    var cactus여러개 = [];
    var 점프timer =0;
    var animation;

    function 프레임마다실행할거(){
        animation = requestAnimationFrame(프레임마다실행할거);
        timer++;

        ctx.clearRect(0,0,canvas.width, canvas.height);

        // dino.x++;
        if(timer % 144 ===0){       //cactus가 120프레임마다 1개씩 생긴다.
            var cactus = new Cactus();
            cactus여러개.push(cactus);  
            
        }
        cactus여러개.forEach((a,i,o)=>{
            
            //x좌표가 0미만이면 제거
            if(a.x<0){
                o.splice(i,1)
            }
            a.x--;

            충돌하냐(dino,a);   //충돌체크는 여기서 해야. 공룡 vs 모든 선인장 충돌체크 해야하므로
           
            a.draw();
        })
        // cactus.draw();
        // dino.y -=3; //점프 속도

        //점프기능
        if(점프중 == true){
            dino.y-=3;
            점프timer++;
        }

        if(점프중 == false){
            if(dino.y<200){
                dino.y+=3;
            }
        }

        if(점프timer>40){
            점프중 = false;
            점프timer = 0;
        }
        dino.draw();
    }

    프레임마다실행할거();   

    //충돌확인

    function 충돌하냐(dino, cactus){
        var x축차이 = cactus.x - (dino.x + dino.width);
        var y축차이 = cactus.y - (dino.y + dino.heigth);
        if((x축차이 <0) && (y축차이 <0 )){
            ctx.clearRect(0,0, canvas.width, canvas.height);
            cancelAnimationFrame(animation)
        }
    }

    var 점프중 = false;
    document.addEventListener('keydown', function(e){
        if(e.code ==='Space'){
            점프중 =true;
        }
    });</blockquote>
     

    강의 보고 한번 따라했는데 충돌에서 아무런 반응이 안 일어나네요. 뭘 잘못 쓴건지 못 찾겠습니다..

    #15647

    codingapple
    키 마스터

    var y축차이 = cactus.y - (dino.y + dino.heigth); 

    height 오타같은데요 

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠