Discord
Login
Community
DARK THEME

Side collision

How do I add side collision to this platformer?


init = function()
  ct.init()
  cx = 0
  cy = 0
  cz = 1
  map = ["map1"]
  level = 0
  velocy = 0
  velocx = 0  
end

update = function()
  if checkCollision(cx,cy,map[level],1536,1536) == false then
    velocy += .25
  end
  if checkCollision(cx,cy,map[level],1536,1536) == true then
    velocy = 0 
  end
  if keyboard.RIGHT and velocx < 3 then
    velocx += .25
  elsif keyboard.LEFT and velocx > -3then
    velocx -= .25
  elsif velocx < 0 then velocx += 1
  elsif velocx > 0 then velocx -= 1 end
    
  if keyboard.UP and checkCollision(cx,cy,map[level],1536,1536) == true or keyboard.SPACE and checkCollision(cx,cy,map[level],1536,1536) == true then 
    velocy = - 5
  end
  cx += velocx
  cy -= velocy
  ct.camera.glideTo(cx,cy,cz)
end

draw = function()
  screen.clear("rgb(0,170,255)")
  ct.drawMap(map[level],0,0,1536,1536)
  screen.drawSprite("hero",0,0,24,24)
end

Para agregar colisión lateral a tu juego de plataformas, necesitas verificar si el jugador está chocando con un obstáculo antes de moverlo horizontalmente. Actualmente, tu código solo verifica colisiones verticales (gravedad y salto), pero no impide que el jugador atraviese paredes al moverse lateralmente.

Aquí te muestro cómo puedes modificar la lógica en update para incluir colisiones laterales:

🛠️ Paso 1: Separar colisiones por dirección Agrega dos funciones de chequeo de colisión: una para la derecha y otra para la izquierda. Por ejemplo:

lua -- Verifica colisión hacia la derecha if keyboard.RIGHT then if checkCollision(cx + velocx + 1, cy, map[level], 1536, 1536) == false then if velocx < 3 then velocx += 0.25 end else velocx = 0 end end

-- Verifica colisión hacia la izquierda if keyboard.LEFT then if checkCollision(cx + velocx - 1, cy, map[level], 1536, 1536) == false then if velocx > -3 then velocx -= 0.25 end else velocx = 0 end end

Paso 2: Mover solo si no hay colisión Después de calcular velocx, verifica si el movimiento horizontal es seguro:

lua -- Movimiento horizontal con colisión lateral local nextX = cx + velocx if checkCollision(nextX, cy, map[level], 1536, 1536) == false then cx = nextX else velocx = 0 end

🛠️ Paso 3: Ajuste visual del sprite Tu draw usa coordenadas fijas (0,0) para el sprite. Deberías dibujarlo en la posición del jugador:

lua screen.drawSprite("hero", cx, cy, 24, 24)

espero que esto funcione. estare atento si debo corregir algo

Post a reply

Progress

Status

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