- 10 mars 2016, 19:53
#3244
Hi, After a long time of elbowing a mini test on this subject, I meet do not understand well how to collisions ...

I mean, using Onelua V4, I'm testing this code ..
Really I do not understand how to do the calculations for the index in the array of map, and whether the tile is solid or nothing .. in this case the solid with which would crash walking to the right I would be the tile 20: P
Complete Code if like to try
Download!
I really hope the community helps, of course, these coders in the shadows give me a hand xD
This post is for educational purposes simply! xDDD

I mean, using Onelua V4, I'm testing this code ..
Code : Tout sélectionner
--Tables (Maps)
dofile("Tile_Maps.lua")
BKG=image.load("BKG.png")
Map0=image.load("BKG_Back_Tileset.png")
if Map0 then maps0=map.new(Map0,tabla0,16,16) end
Map1=image.load("BKG_Front_Tileset.png")
if Map1 then maps1=map.new(Map1,tabla1,16,16) end
function newPlayer()
local obj = {
x = 0,
y = 0,
h = 0,
s = 0,
dir = false,
-- size of tile player
ws = 20,
hs = 36,
data = image.load("sprites.png",20,36),
crono = timer.new(),
jump_enable = true,
map_sizeW = 0,
map_sizeH = 0,
}
obj.crono:start()
function obj:MapSize(w,h)
obj.map_sizeW = w
obj.map_sizeH = h
end
function obj:xy(x,y)
obj.x = (x or obj.x)
obj.y = (y or obj.y)
end
function obj:walking(xm,ym)
if buttons.released.right then
obj.s = 0
end
if buttons.cross and obj.jump_enable then
obj.jump_enable = false
end
if buttons.held.right then
if obj.dir then
image.flipv(obj.data)
obj.dir = not obj.dir
end
local mtw = 16 -- tile size map
local mth = 16 -- tile size map
tx = math.floor(((obj.x+obj.ws) + xm) / (obj.map_sizeW/16))
ty = math.floor(((obj.y+obj.hs) + ym) / (obj.map_sizeH/16))
if tabla1[tx][ty] == 20 then -- todo el problema radica en estos index :P :-/
return nil
end
if obj.crono:time()> 16 then
obj.crono:reset();
obj.crono:start();
obj.s = obj.s + 1
if obj.s > 2 then
obj.s = 0
end
obj.x = obj.x + 4
if obj.x > 240 then
obj.x = 240
return 2
end
return 1
end
end
if buttons.held.left then
if not obj.dir then
image.flipv(obj.data)
obj.dir = not obj.dir
end
if obj.crono:time()> 32 then
obj.crono:reset();
obj.crono:start();
obj.s = obj.s + 1
if obj.s > 2 then
obj.s = 0
end
obj.x = obj.x - 4
if obj.x < 10 then
obj.x = 10
return -2
end
return -1
end
end
end
--obj.data:resize(17*1.5,31*1.5)
function obj:draw()
obj.data:blitsprite(obj.x,obj.y+obj.h,obj.s)
if not obj.jump_enable then
obj.h = obj.h - 4
if obj.h < -75 then obj.jump_enable = true end
else
if obj.h < 0 then
obj.h = obj.h + 4
end
end
end
return obj
end
Luigui = newPlayer()
Luigui:xy(10,187)
Luigui:MapSize(map.sizex(maps1),map.sizey(maps1))
x,y=0,0
xx,yy=0,0
while true do
buttons.read()
--blitear el fondo
image.blit(BKG,0,0)
--Blitear el mapa de detras
map.blit(maps0,x,160)
--Blitear el mapa de delante
map.blit(maps1,xx,160)
local move_back = Luigui:walking(xx,160)
if move_back then
x=x+(1*move_back)
xx=xx+(2*move_back)
end
Luigui:draw()
screen.flip()
end
Really I do not understand how to do the calculations for the index in the array of map, and whether the tile is solid or nothing .. in this case the solid with which would crash walking to the right I would be the tile 20: P
Complete Code if like to try

Download!
I really hope the community helps, of course, these coders in the shadows give me a hand xD
This post is for educational purposes simply! xDDD