Map Collision Blocks
Hello! Just asking does anyone know how to have tiles that are in a map be solid objects that players and project tiles can't got through? Let me know please. Thanks.
Hello! Just asking does anyone know how to have tiles that are in a map be solid objects that players and project tiles can't got through? Let me know please. Thanks.
Hey! Here's how you can make tiles in your map solid so the player and projectiles can't go through them:
// List of solid tile IDs solidTiles = [1] // Add other solid tile numbers if needed
// Function to check if a tile is solid function isSolid(x, y) tx = floor(x / map.tileWidth) ty = floor(y / map.tileHeight) tile = map.getTile("layer", tx, ty) return solidTiles.indexOf(tile) >= 0 end
Then before you move the player or projectile, just check like this:
// For player movement if not isSolid(player.x + dx, player.y) then player.x += dx end
if not isSolid(player.x, player.y + dy) then player.y += dy end
// For projectile collision if isSolid(projectile.x, projectile.y) then projectile.active = false end
Just make sure to replace "layer" with the name of your tile layer (like "ground" or "walls"). Let me know if you want help with anything else! :)
So should I put this in a separate file or the main file?
Good question! You can just put the code in your main file (like main or whatever you're using for game logic).
Here’s how to organize it:
Put the solidTiles = [...] and function isSolid(x, y) at the top of your main file, maybe under your global variables.
Use the isSolid(...) checks inside your player or projectile update code, wherever you're handling movement or collisions.
No need for a separate file unless you want to organize your project more later.
Let me know if you want help placing it in your existing code! :)
Yeah I might need a bit help here, Imma bit confused
No problem at all! Just let me know what part you're stuck on — like where to put the function, how your player movement works, or what tile layer you're using — and I can help you piece it together.
If you want, you can copy and paste part of your code and I’ll help fix it up with the solid tile logic added in! :)
It's a bit weird but here it is:
init = function()
mode = "intro"
initPainter()
initSprinter()
initShielder()
end
update = function()
if mode == "intro" then
updateIntroScreen()
end
if mode == "title" then
updateTiltleScreen()
elsif mode == "select" then
updateSelectScreen()
end
if mode == "painterplay" then
updatePainter()
end
if mode == "sprinterplay" then
updateSprinter()
end
if mode == "shielderplay" then
updateShielder()
end
end
draw = function()
screen.clear()
if mode == "intro" then
drawIntroScreen()
end
if mode == "title" then
drawTitleScreen()
elsif mode == "select" then
drawSelectScreen()
end
if mode == "painterplay" then
drawPainter()
end
if mode == "sprinterplay" then
drawSprinter()
end
if mode == "shielderplay" then
drawShielder()
end
end
Here's the link:
https://microstudio.io/i/P1X3L_2012/splat/