Discord
Login
Community
DARK THEME

map.get Problems

Hello, i am trying to do some logics to get the Name or the Sizes of a Map or Tile-Names. But i don't get anything. The shell always says:

Warning: map.get("level1") is not a function, in file "main" at line 5, column 33

Where do i have to copy the code?

And can i print a value out of a function? Like print (map.get.x) ?

I know, that there are some librarys, but i wanted to understand how they work. Would be realy kind, if you could help me :)

Thx

map = maps["level1"]

print( map.get(x,y))

Thank you Loginus, ok - wow. That was easy. I always forgot to set the variabel map. If i am looking now right, i haven't set x or y to a value, so it's 0. So my coordinates of the map 0 | 0 are in the left/down corner. That helped :)

thank for the reply, i needed this too LOL

Ok, i have coded now a lot, tried many things and now i have a working version. You can see it here: https://microstudio.io/mrp_codes/schimmerwelt3/KAND7U5N/

But it's not perfect. I think the coordinates from the map and the player are one small problem. My solution is not bad, but it could be improved. Another thing, tiles are positioned from centre if i understand it, but my collision-calculation is a bit "glitchy" if you press e.g. LEFT AND UP or DOWN AND RIGHT near a block element. Just try it and have a look :)

I will think about some improvements. It's hard for my brain to observe 4 coordinates (or even 8) to check, if there "could" be a collision :D

You need to convert the player's coordinates to coordinates on Map. In QuickEngine it looks like this


Quick.mapCollision = function(sprite,map)
  local blockw = map.width/map.map.width
  local blockh = map.height/map.map.height
  local cx = floor((sprite.x-(map.x-map.width/2))/blockw)
  local cy = floor((sprite.y-(map.y-map.height/2))/blockh)
  return map.map.get(cx,cy)
end

If you are not using QuickEngine, this code modification should result in correct collisions with tiles.

mapCollision = function(sprite,map)
  local blockw = map.width/map.map.width
  local blockh = map.height/map.map.height
  local cx = floor((sprite.x-(map.x-map.width/2))/blockw)
  local cy = floor((sprite.y-(map.y-map.height/2))/blockh)
  return map.map.get(cx,cy)
end

player = object 
  x = 0
  y = 0
  width = 16
  height = 16
end

level_collision = object
  map = maps["level1_collision"]
  x = 0
  y = 0 
  width = 300
  height = 400 
end

if not mapCollision(player,level_collision) then 
   player_move()
end

Thank you very much. Sorry if my codes are not well formated, how do i do this? < C0de> BLABLA < / c0de> <- this seems to be not the whole magic...

Back to my program/game.

I do the following in my player movement. Please regard my comments to understand whats going on there.

if keyboard.UP then player.old_y = player.y //save the old Y coordinate

player.new_y = player.y //not elegant, but for later calculations
//player.new_x = player.x // i commented it out, because if i go up i don't need it

player.y += player.speed // the "real" movement in Y direction
player.pic ="player_move_up" // change the sprite of the walking player
actTile = map.get(floor((player.new_x)/10+20),floor((player.new_y+5)/10+10)) // calculation for the next map-tile

end

My Map is 40x20 tiles big, with 16 Pixel-Sprites. (The code is not perfect. I didn't knew for instance the code for width.screen)

After some arrow-key is pressed i check the following:

if keyboard.UP and actTile.startsWith("block") then

  player.y = player.old_y
end

All my sprites/tiles who should block the player are in this notation: block_rock, block_tree, block_water ...

I am working without any librarys until now, because i want to understand everything. But i appreciate your codes from above, i will try to understand them and use them for my game :) Thanks again!

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community