Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

GOT IT! whith the help of https://jonathanwhiting.com/tutorial/collision/

Here's my final code:

var tileW = settings.tileSize.width || settings.tileSize[0];
var tileH = settings.tileSize.height || settings.tileSize[1];
var TileMap  = require('pixelbox/TileMap');
var oldY
var oldX
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 * 8) + (8) &&
        rect1.x + rect1.w > (rect2.x * 8) &&
        rect1.y < (rect2.y * 8) + (8) &&
        rect1.y + rect1.h > (rect2.y * 8)) {        
        return true
    }
}
var walls = []
var map = getMap("mymap"); 
draw(map,0,0)
print(walls.length);

exports.update = function () {
    cls();
    draw(map,0,0)
    oldY = mario.y
    oldX = mario.x
    if (btn.right) {
        mario.x += 1;
        flipH = "";
    }
    if (btn.left) {
        mario.x -= 1;
        flipH = "flipH";
    }    
    walls = [...map.find(3)]; 
    sprite(142, link.x, link.y);
    var i;
    for (i = 0; i < walls.length; i++) {
        
        if (AABBcollision(mario, walls[i])==true){                    
            
            print("Hello, Mario", 12, 10);
                mario.y = oldY
                mario.x = oldX     
        }     
    }
    oldX = mario.x   
    if (btn.up) {
        mario.y -= 1;        
        //flipV = "";        
    }
    if (btn.down) {
        mario.y += 1;
        //flipV = "flipV";
    } 
    for (i = 0; i < walls.length; i++) {        
        if (AABBcollision(mario, walls[i])==true){                  
            
            print("Hello, Mario", 12, 10);
                mario.y = oldY
                mario.x = oldX 
        }     
    } 
    sprite(153, mario.x, mario.y, flipH, flipV);    
}


Definitely not the most efficient code. I'll have to work on that later. But it is buttery smooth!