Discord
Login
Community
DARK THEME

need help with camera stuff

Hello, i'm making a game with a friend of mine, and it was all going smoothly until the world just simply didn't work. then the player just wouldn't appear on screen, or maybe he wouldn't appear where the camera was focused, which i believe was in 0, 0. can someone help me? the code is: init = function() player = object x = -490 y = 218 speed = 2 facing = 1 end

racer1 = object x = -490 y = 210 speed = 2 facing = 1 end end

update = function() if keyboard.LEFT then player.x -= player.speed player.facing = -1 end if keyboard.RIGHT then player.x += player.speed player.facing = 1 end if keyboard.UP then player.y += player.speed end if keyboard.DOWN then player.y -= player.speed end if keyboard.SHIFT then player.speed = 3.5 end if keyboard.release.SHIFT then player.speed = 2 end player.x = clamp (player.x, -1024/2 + 20,1024/2 - 20) player.y = clamp (player.y, -512/2 + 20,512/2 - 20) end draw = function() screen.fillRect(0,0,1024,512,"rgb(124,197,87") screen.drawMap("world" , 0-camera.x,0-camera.y,1024+100,512) screen.drawMap("world" , 0-camera.x,0-camera.y,1024,512) screen.drawSprite( "cookie_shop", 0 -camera.x ,50 -camera.y, 50, 50 ) screen.setDrawScale(player.facing,1) screen.drawSprite("player2",player.x - camera.x,player.y - camera.y, 30,30)

end init = function() end

update = function() end

draw = function()

screen.fillRect(0,0,1024,512,"rgb(124,197,87") screen.drawMap("world" , 0-camera.x,0-camera.y,1024+100,512) screen.drawMap("world" , 0-camera.x,0-camera.y,1024,512) screen.drawSprite( "cookie_shop", 0 -camera.x ,50 -camera.y, 50, 50 )

end init = function() camera = object x = 0 y = 0 end end

update = function() camera.x = player1.x camera.y = player1.y

camera.x = clamp(camera.x,-300,300) camera.y = clamp(camera.y,-156,156) end

and then there mrLman's helpers file.

Okay, I really don't understand the code you gave me, but what I do know is that you're using too many init, update, and draw statements, which are causing the game to crash. If you want a second init statement, you can say:

initPlayer = function()
doSomwthinkHere()
end

init = function()
initPlayer()
end

As you can see, init is the main initializer of your code. If you want to create another function, just write init, update, or draw with something to specify to the computer what you want to initialize. This will prevent bugs.

Then the camera is just an object that says: move the world in the direction of its direction. It's as if your player is static, but it's the world that moves around you. Here is an example of code I wrote based on what I understood from your documentation.

// This function runs once when the game starts to set everything up.
init = function() 
    // We create the player and give them "properties" (like coordinates and speed).
    player = object 
        x = 0        // Starting horizontal position
        y = 0        // Starting vertical position
        speed = 2    // How many pixels the player moves per frame
        facing = 0   // Stores the direction the player is looking
        image = "player1" // The look or asset name of the player
    end 
    
    // We create a camera object to follow the player around the map.
    camera = object 
        x = 0 
        y = 0 
    end 
end 

// This helper function keeps a value between a minimum and a maximum limit.
// Think of it like invisible walls that stop the player from leaving the map!
clamp = function(value, lower_limit, upper_limit)
  // First, make sure the value doesn't go below the lowest allowed limit
  local val = max(value, lower_limit)
  // Then, make sure it doesn't go above the highest allowed limit
  val = min(val, upper_limit)
  return val
end

// This function checks if you are pressing keys and moves the player accordingly.
Move = function() 
    // If you press the UP arrow, increase the player's Y position
    if keyboard.UP then 
        player.y += player.speed 
        player.facing = 1 // 1 represents facing UP
    end 
    // If you press the DOWN arrow, decrease the player's Y position
    if keyboard.DOWN then 
        player.y -= player.speed 
        player.facing = -1 // -1 represents facing DOWN
    end 
    // If you press the LEFT arrow, decrease the player's X position
    if keyboard.LEFT then 
        player.x -= player.speed 
        player.facing = 2 // 2 represents facing LEFT
    end 
    // If you press the RIGHT arrow, increase the player's X position
    if keyboard.RIGHT then 
        player.x += player.speed 
        player.facing = -2 // -2 represents facing RIGHT
    end 
end 

// This function runs continuously (usually 60 times a second) to update the game logic.
update = function() 
    Move() // Check for keyboard inputs and move the player
    
    // Make the camera follow the player's exact coordinates
    camera.x = player.x 
    camera.y = player.y 
    
    // Use our clamp function to trap the player inside a 600x600 box (-300 to 300)
    player.x = clamp(player.x, -300, 300)
    player.y = clamp(player.y, -300, 300)
end 

// This function also runs continuously, right after 'update', to draw everything on the screen.
draw = function() 
    // Clear the previous frame with a clean white background
    screen.clear("white") 
    
    // Shift the screen view in the opposite direction of the camera.
    // This creates the illusion that the camera is moving with the player!
    screen.setTranslation(-camera.x, -camera.y) 
    
    // Draw the green background map (a simple large rectangle for now)
    screen.fillRect(0, 0, 660, 630, "rgb(87,197,87)")
    
    // Draw the player (currently an orange square with a dark outline).
    // You can replace this later with: screen.drawImage(player.image, player.x, player.y)
    screen.fillRect(player.x - 15, player.y, 30, 30, "rgb(255,189,57)")
    screen.drawRect(player.x - 15, player.y, 30, 30, "rgb(147,109,33)")
    
    // Draw a simple gray shop on the map so there is an obstacle/destination
    screen.fillRect(-100, 100, 100, 100, "rgb(118,118,118)") // Shop body
    screen.setLineWidth(3)
    screen.drawRect(-100, 100, 100, 100, "rgb(97,97,97)")   // Shop border
    screen.setLineWidth(1)
    screen.fillRect(-100, 100, 104, 50, "rgb(147,114,98)")   // Shop roof
    screen.drawText("shop", -100, 100, 20, "white")         // Shop text label
    
    // CRITICAL: Reset the translation back to (0,0) at the end of drawing.
    // This ensures UI elements (like health bars) stay fixed to the screen later!
    screen.setTranslation(0, 0) 
end

Post a reply

Progress

Status

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