Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Pixelbox

Create 2D games in JavaScript, made easier · By Cedric Stoquer

How to detect collision

A topic by Dudeguy1 created Apr 17, 2020 Views: 617 Replies: 2
Viewing posts 1 to 2

I want a character (guide) to speak with you when you touch him

(+2)

Simple example of main.js for character interaction.


var tileW = settings.tileSize.width || settings.tileSize[0];
var tileH = settings.tileSize.height || settings.tileSize[1];

var mario = {
    x: 60,
    y: 60,
    w: tileW,
    h: tileH
};
var link = {
    x: 100,
    y: 100,
    w: tileW,
    h: tileH

}
var flipH = "";
var flipV = "";

function AABBcollision(rect1, rect2) {

    if (rect1.x < rect2.x + rect2.w &&
        rect1.x + rect1.w > rect2.x &&
        rect1.y < rect2.y + rect2.h &&
        rect1.y + rect1.h > rect2.y) {
        print("Hello, Mario", 10, 10);
    }

}

exports.update = function () {
    if (btn.right) {
        mario.x += 1;
        flipH = "";
    }
    if (btn.left) {
        mario.x -= 1;
        flipH = "flipH";
    }
    if (btn.up) {
        mario.y -= 1;
        flipV = "";
    }
    if (btn.down) {
        mario.y += 1;
        flipV = "flipV";
    }
    cls();

    sprite(153, mario.x, mario.y, flipH, flipV);
    sprite(142, link.x, link.y);
    AABBcollision(mario, link);
}

Thx for the help