Discord
Login
Community
DARK THEME

I need very help

I need help with a game because when the player spawns, even if he has blocks below him, he goes into the void

the code is the next:

COLLISION_UP = 1001 COLLISION_DOWN = 2002 COLLISION_LEFT = 3003 COLLISION_RIGHT = 4004 COLLISION_NONE = 5005

initPlayer = function() // simple player object - class not really needed player = object x = 0 y = 0 spawn_x = 0 spawn_y = 0 x_velocity = 0 y_velocity = 0 facing = 1 spawn_facing = 1 top_speed = 2 width = 16 height = 16 landed = true jump_velocity = 5 launch_velocity = 7.5 gravity = 0.25 max_fall_speed = 8 acceleration = 0.4 climbing_speed = 1 climbing = false touching_ladder = false holding_up = false y_draw_offset = 6 end end

updatePlayer = function() // don't update until visible during fade in if overlay_alpha > 0.95 then return end // detect whether climbing or not player.touching_ladder = checkCollision(player.x, player.y, "current_map", level.map_width, level.map_height).startsWith("climb") player.climbing = (player.touching_ladder) and (not player.landed)

// process button input if player.climbing then player.x_velocity = 0 if keyboard.LEFT or gamepad.LEFT then player.x_velocity -= player.climbing_speed / 3 player.facing = -1 end

if keyboard.RIGHT or gamepad.RIGHT then
  player.x_velocity += player.climbing_speed / 3
  player.facing = 1
end

player.y_velocity = 0
if keyboard.UP or gamepad.UP then
  player.y_velocity = player.climbing_speed
end
if keyboard.DOWN or gamepad.DOWN then
  player.y_velocity = -player.climbing_speed
end
// set the direction player is facing based on position on ladder
local ladder_x_traversal = ((player.x + level.map_width / 2) / (level.map_width / level.map.width)) % 1
if ladder_x_traversal < 0.5 then
  player.facing = 1
else
  player.facing = -1
end

else if keyboard.LEFT or gamepad.LEFT then if keyboard.SHIFT then player.x_velocity -= player.acceleration else player.facing = -1 player.x_velocity -= player.acceleration end end if keyboard.RIGHT or gamepad.RIGHT then player.facing = 1 player.x_velocity += player.acceleration end

// limit player to it's own top speed
player.x_velocity = clamp(player.x_velocity, -player.top_speed, player.top_speed)

// detect jump request
if (keyboard.SPACE or keyboard.UP or gamepad.A) then
  player.holding_up = true
  if player.landed then
    player.y_velocity = player.jump_velocity
  end
else
  player.holding_up = false
end

end playerPhysics()

// clamp player to the map area (on the x axis) player.x = clamp(player.x, -level.map_width / 2 + 8, level.map_width / 2 - 8)

// handle player falling off screen (always into the ocean and treated as a "death") if player.y < (-level.map_height/2 - 100) then respawnPlayer() end

local tile_touched = checkCollision(player.x, player.y, "current_map", level.map_width, level.map_height)

// check for level completion level_complete = tile_touched.startsWith("sprites/objects/goal")

// handle touching hazards if tile_touched.startsWith("sprites/enemies/goomba") then if player.y_velocity < 0 then score += 1 player.landed = true changeMapTile(player.x, player.y, "current_map", level.map_width, level.map_height, "") print("goomba dead!") else respawnPlayer() game_lives -= 1 return end end

// handle touching collectibles local collecting = tile_touched.startsWith("sprites/objects/1coin") if collecting then changeMapTile(player.x, player.y, "current_map", level.map_width, level.map_height, "") score += 1 end // handle touching collectibles local collecting = tile_touched.startsWith("sprites/power_up/mushrooms/1up") if collecting then changeMapTile(player.x, player.y, "current_map", level.map_width, level.map_height, "") game_lives += 1 end

local checkpoint = tile_touched.startsWith("sprites/objects/checkpoint_evil") if checkpoint then changeMapTile(player.x, player.y, "current_map", level.map_width, level.map_height, "sprites/objects/checkpoint_luigi") player.spawn_x = player.x player.spawn_y = player.y player.spawn_facing = player.facing end end

drawPlayer = function() // flip sprite if facing left screen.setDrawScale(player.facing, 1) // written out with separte draw calls to allow for experimentation with draw sizes etc. if player.climbing then if player.y_velocity == 0 then sprites["player_climb"].setFrame(0) end screen.drawSprite("sprites/rb/escalando", player.x - camera.x, (player.y - 3) + player.y_draw_offset/2 - camera.y, player.width, player.height) elsif player.y_velocity > 0 then screen.drawSprite("sprites/rb/jump", player.x - camera.x, (player.y - 3) + player.y_draw_offset - camera.y, player.width, player.height) elsif player.y_velocity < 0 then screen.drawSprite("sprites/rb/jump", player.x - camera.x, (player.y - 3) + player.y_draw_offset - camera.y, player.width, player.height) elsif abs(player.x_velocity) > 0.5 then screen.drawSprite("sprites/rb/run", player.x - camera.x, (player.y - 3) + player.y_draw_offset - camera.y, player.width, player.height) else screen.drawSprite("sprites/rb/idle", player.x - camera.x, (player.y - 3) + player.y_draw_offset - camera.y, player.width, player.height) end // reset to avoid flipping other draw calls screen.setDrawScale(1, 1) //screen.fillRound(player.x-camera.x, player.y-camera.y, 2, 2, "rgb(255,0,0)") end

playerPhysics = function() if not player.climbing then // x_velocity smoothing player.x_velocity *= 0.85 if player.x_velocity > -0.05 and player.x_velocity < 0.05 then player.x_velocity = 0 end

// gravity (jumps higher if holding jump button)
if player.y_velocity > 0 then
  if player.holding_up then
    player.y_velocity -= player.gravity * 0.8
  else
    player.y_velocity -= player.gravity * 1.6
  end
else 
  player.y_velocity -= player.gravity
end
// limit falling to max fall speed
if player.y_velocity < -player.max_fall_speed then player.y_velocity = -player.max_fall_speed end

end

local map = maps["current_map"] local hw = level.map_width / 2 // half level map width in pixels local hh = level.map_height / 2 // half level map height

local hph = (player.height / 2) * 0.5 // half the player collision box height local hpw = (player.width / 1.5) * 0.4 // half the player collision box width

// define the map grid squares where each of the four corners of the invisible // hit box for the player is - both for current (old) position and // desired new position based on velocity

local old_top = floor((player.y + hph + hh) / 16) local old_bottom = floor((player.y - hph + hh) / 16) local old_right = floor((player.x + hpw + hw) / 16 - 0.001) local old_left = floor((player.x - hpw + hw) / 16)

local new_top = floor((player.y + player.y_velocity + hph + hh) / 16) local new_bottom = floor((player.y + player.y_velocity - hph + hh) / 16) local new_right = floor((player.x + player.x_velocity + hpw + hw) / 16) local new_left = floor((player.x + player.x_velocity - hpw + hw) / 16)

// falling if player.y_velocity < 0 and (map.get(old_left, new_bottom).startsWith("sprites/objects/ground") or map.get(old_right, new_bottom).startsWith("ground")) then player.y = -hh + (new_bottom + 1) * 16 + hph player.y_velocity = 0 player.landed = 1 // jumping elsif player.y_velocity > 0 and (map.get(old_left, new_top).startsWith("sprites/objects/ground") or map.get(old_right, new_top).startsWith("ground")) then player.y_velocity = 0 else if not player.climbing then player.landed = 0 end end

// moving left if player.x_velocity < 0 and (map.get(new_left, old_bottom).startsWith("sprites/objects/ground") or map.get(new_left, old_top).startsWith("ground")) then player.x_velocity = 0 player.x = -hw + (new_left+1) * 16 + hpw // moving right elsif player.x_velocity > 0 and (map.get(new_right, old_bottom).startsWith("sprites/objects/ground") or map.get(new_right, old_top).startsWith("sprites/objects/ground")) then player.x_velocity = 0 player.x = -hw + (new_right) * 16 - hpw end

// actually move the player player.x += player.x_velocity player.y += player.y_velocity end

collisionWithGoomba = function( goombaList ) if goombaList then local i = 0 while i < goombaList.length local goomba = goombaList[ i ] local next = 1 local detect = detectCollisionType( player, goomba ) if detect == COLLISION_UP then goombaList.removeAt( i ) next = 0 print("remove") player.landed = true player.y_velocity = player.jump_velocity end if ( detect == COLLISION_LEFT or detect == COLLISION_RIGHT ) and player.landed then respawnPlayer() game_lives -= 1
end i += next end end end

// Function to check for AABB collision and determine its type detectCollisionType = function(obj1, obj2) // Calculate position differences local dx = abs(obj1.x - obj2.x) local dy = abs(obj1.y - obj2.y)

// Calculate allowable collision ranges
local overlap_x = (obj1.width + obj2.width) / 2 - dx
local overlap_y = (obj1.height + obj2.height) / 2 - dy

// If a collision occurs
if overlap_x > 0 and overlap_y > 0 then
    if overlap_x > overlap_y then
        if obj1.y > obj2.y then
            return COLLISION_UP
        else
            return COLLISION_DOWN
        end
    else
        if obj1.x > obj2.x then
            return COLLISION_LEFT
        else
            return COLLISION_RIGHT
        end
    end
end

return COLLISION_NONE

end

collide = function(a1, a2)

// repositionnement du sprite en haut à gauche local a1x = a1.x - a1.width/2 local a1y = a1.y - a1.height/2 local a2x = a2.x - a2.width/2 local a2y = a2.y - a2.height/2

// condition pour qu'il y ai collision local c1 = (a1x + a1.width) >= a2x local c2 = (a1x <= (a2x + a2.width)) local c3 = (a1y + a1.height) >= a2y local c4 = (a1y <= (a2y + a2.height))

if (c1 and c2 and c3 and c4) then return 1 // si toute les conditions sont remplis alors collision end

return 0 end

respawnPlayer = function() player.x = player.spawn_x player.y = player.spawn_y player.facing = player.spawn_facing startLevelFadeIn() end

Aún no tengo idea de como se traspasa el codigo a el cuadro oscuro

I investigate the code and I found this

if player.y_velocity < 0 and (map.get(old_left, new_bottom).startsWith("sprites/objects/ground") or map.get(old_right, new_bottom).startsWith("ground")) then player.y = -hh + (new_bottom + 1) * 16 + hph player.y_velocity = 0 player.landed = 1 // jumping elsif player.y_velocity > 0 and (map.get(old_left, new_top).startsWith("sprites/objects/ground") or map.get(old_right, new_top).startsWith("ground")) then player.y_velocity = 0 else if not player.climbing then player.landed = 0 end end

disculpas ya resolvi (fue estupido la manera en que resolvi el problema)

amm I can try to fix it. :)

thanks, but I fixed the problem

Post a reply

Progress

Status

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