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 make collision, below is the script i made that just makes a blank screen.

A topic by Dudeguy1 created Jan 24, 2021 Views: 415 Replies: 5
Viewing posts 1 to 2

var character = {

    x: 60,
    y: 60
};

var oldpos = {
oldx: 60,
oldy: 60
};

var background = getMap("map");
paper(4);

exports.update = function () {
if (map.get(character.x, character.y) != null) character.x == oldpos.oldx;
if (map.get(character.x, character.y) != null) character.y == oldpos.oldy;
oldpos.oldx == character.x;
oldpos.oldy == character.y;
    if (btn.right) character.x += 1;
    if (btn.left)  character.x -= 1;
    if (btn.up)    character.y -= 1;
    if (btn.down)  character.y += 1;

    cls();
    draw(background, 0, 0);
    sprite(206, character.x, character.y);
};

nvm just realized i already asked a question similar

NVM NVM it only works for specific sprites not the map

ok so apparently it was because i didnt set map as a variable but now it just doesnt do anything

here is the new one

var character = {

    x: 60,
    y: 60
};

var oldpos = {
oldx: 60,
oldy: 60
};

var background = getMap("map");
paper(4);

exports.update = function () {
if (background.get(character.x, character.y) ==! null) character.x = oldpos.oldx;
if (background.get(character.x, character.y) ==! null) character.y = oldpos.oldy;
oldpos.oldx = character.x;
oldpos.oldy = character.y;
    if (btn.right) character.x += 1;
    if (btn.left)  character.x -= 1;
    if (btn.up)    character.y -= 1;
    if (btn.down)  character.y += 1;

    cls();
    draw(background, 0, 0);
    sprite(206, character.x, character.y);
};

I think you are mixing pixel position (sprite and draw) and tile position (background.get). You need to convert pixel to tile by dividing the pixel coordinates by the tile sizes (available from settings.tileSize). Or, inversely, if you want your character to move in tile increments, modify the last line as follow:|

sprite(206, character.x * settings.tileSize.width, character.y * settings.tileSize.height);