1

I know how to put where my collision function is now but as you can see my function is wrong and I would like some help, please.

Here's my collison function

this.cd = function(){ ctx.clearRect(x,y1,width,y1 - y2); if (myPlayer.x >= myObstacles.x || myPlayer.x <= myObstacles.x + myObstacles.width || myPlayer.y >= 0 || myPlayer.y <= myObstacles.yStart){ alert('GAME OVER'); clearInterval(interval); }else if (myRectangle.x >= myObstacles.x || myPlayer.x <= myObstacles.x + myObstacles.width || myPlayer.y >= myObstacles.yEnd || myPlayer.y <= myObstacles.height){ alert('GAME OVER'); clearInterval(interval); }else{ } }

And here's my full code

    let b1 = document.getElementById('button1');
let c = document.getElementById('myCanvas');
let ctx = c.getContext('2d');


//Control movements of the player
// document.addEventListener('keydown', keyDownHandler, false);
// document.addEventListener('keyup', keyUpHandler, false);
// var rightPressed = false;
// var leftPressed = false;
// var upPressed = false;
// var downPressed = false;
// function keyDownHandler(e) {
//     //Right arrow key 
//     if(e.keyCode == 39) {
//         rightPressed = true;
//     }
//     //Left arrow key
//     else if(e.keyCode == 37) {
//         leftPressed = true;
//     }
//     //Down arrow key
//     if(e.keyCode == 40) {
//       downPressed = true;
//     }
//     //Up arrow key
//     else if(e.keyCode == 38) {
//       upPressed = true;
//     }
// }

//make a component for rectangle and obstacle
let myPlayer;
let timerID;
let H_gap = 250;
class Obstacles {
    constructor(x, y, width, height, color) {
        // vars
        this.x = x;
        this.y = y;
        this.width = width;
        this.color = color;
        this.gap = Math.floor(Math.random() * 30 + 1) + 50;            //50 - 150 random generated
        this.y1 = Math.floor(Math.random() * (c.height - 40 - 40 + 1)) + 40;   // prevent gap is too high or too low
        this.y2 = this.y1 + this.gap;                               // define y2 (start point of lower rect)
        // functions of obstacles
        this.draw = function () {
            ctx.beginPath();
            ctx.fillStyle = this.color;
            ctx.fillRect(this.x, 0, this.width, this.y1);
            ctx.fillStyle = this.color;
            ctx.fillRect(this.x, this.y2, this.width, c.height);
        }
        this.move = function () {
            ctx.clearRect(this.x, 0, this.width, this.y1);
            ctx.clearRect(this.x, this.y2, this.width, c.height);
            this.x -= 10;
            ctx.beginPath();
            ctx.fillStyle = this.color;
            ctx.fillRect(this.x, 0, this.width, this.y1);
            ctx.fillStyle = this.color;
            ctx.fillRect(this.x, this.y2, this.width, c.height);
        }
        // this.cd = function(){
        //     ctx.clearRect(x,y1,width,y1 - y2);
        //         if (myPlayer.x >= myObstacles.x || myPlayer.x <= myObstacles.x + myObstacles.width || myPlayer.y >= 0 || myPlayer.y <= myObstacles.yStart){
        //             alert('GAME OVER');
        //             clearInterval(interval);
        //         }else if (myRectangle.x >= myObstacles.x || myPlayer.x <= myObstacles.x + myObstacles.width || myPlayer.y >= myObstacles.yEnd || myPlayer.y <= myObstacles.height){
        //             alert('GAME OVER');
        //             clearInterval(interval);
        //         }else{
        //         }
        // }

    }
}
class Player {
    constructor(x, y, width, height, color) {
        // vars
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;

        // functions of player
        this.draw = function () {
            // ctx.clearRect(0,0,480,320);
            //     if(rightPressed) {
            //         playerX += 5;
            //     }
            //     else if(leftPressed) {
            //         playerX -= 5;
            //     }
            //     if(downPressed) {
            //         playerY += 5;
            //     }
            //     else if(upPressed) {
            //         playerY -= 5;
            //     }
            ctx.beginPath();
            ctx.fillStyle = this.color;
            ctx.fillRect(this.x, this.y, this.width, this.height);

        }
    }
}

b1.addEventListener('click', gameStart)

function gameStart() {

    // pre load stuffs onto the screen
    myPlayer = new Player(30, 240, 30, 30, "red")
    myPlayer.draw();
    myObstacles = [];
    myObstacles.push(new Obstacles(350, 0, 50, 320, "green"));
    myObstacles.push(new Obstacles(350 + H_gap, 0, 50, 320, "green"));
    myObstacles.push(new Obstacles(350 + 2 * H_gap, 0, 50, 320, "green"));
    myObstacles.push(new Obstacles(350 + 3 * H_gap, 0, 50, 320, "green"));

    for (let i = 0; i < myObstacles.length; i++) {
        myObstacles[i].draw();
    }
    clearInterval(updateGame);
    timerID = setInterval(updateGame, 100) // set updategame timer
}


function updateGame() {
    // update the rect
    for (let i = 0; i < myObstacles.length; i++) {
        myObstacles[i].move();
    }
    // check if obstacles outside of canvas
    if (myObstacles[0].x < -50) {
        myObstacles.shift();
        n_x = myObstacles[myObstacles.length - 1].x
        myObstacles.push(new Obstacles(n_x + H_gap, 0, 50, 320, "green"));
    }
}
Zen Skyro
  • 11
  • 2
  • It needs to be in the "game loop". From your code "image of code", collision detection (and anything that affects the game's logic/response) needs to be along side where the action happens. So in this case, inside your "draw" logic. – mardubbles Apr 09 '22 at 00:51
  • Show your full code, it will be easier to offer advice on logical errors. – mardubbles Apr 09 '22 at 00:54
  • Thanks @mardubbles! I did some changes but it won't be that different for the collision function. Also how do I show you my full code? – Zen Skyro Apr 09 '22 at 05:01
  • By "show full code" I meant for you to edit your question. If you followed my advice though your collision code can at least apply to the "draw" thread and work. But if you still have logical errors, is why to post "full code" here. I personally know what you mean but without full code, could be an easier fix for someone else to solve. – mardubbles Apr 09 '22 at 05:06
  • @mardubbles It said my post is mostly code and tell me to add more details. So I don't know what should I do here because I don't think there are any more details I can add. – Zen Skyro Apr 09 '22 at 06:33

0 Answers0