Discord
Login
Community
DARK THEME

Camera

Good morning, i have problem on my game why the camera don't follow the player and he go out the screen, this is the full code init = function() player = object x = 0 y = 0 speed = 1 end end

update = function() if keyboard.UP then player.y += player.speed end if keyboard.DOWN then player.y -= player.speed end if keyboard.RIGHT then player.x += player.speed end if keyboard.LEFT then player.x -= player.speed end end

draw = function() screen.fillRect(0,0,screen.width,screen.height,"rgb(57,0,587)") screen.drawMap("polonuovo", 0,0,2000,2000) screen.clear screen.drawSprite("alessandro", player.x - camera.x, player.y - camera.y 40, 40) end initCamer = function() camera = object x = 0 y = 0 end end

updateCamera = function() camera.x = player.x camera.y = player.y

camera.x = clamp(camera,x, -334, 334) camera.y = clamp(camera.y, -156, 156)

end checkCollisions = function() if distance(player.x, player.y, coin1.x, coin1.y) < 25 then score += 1 randomizeCoinPos() end end

randomizeCoinPos = function() coin1.x = randRange(-150, 150) coin1.y = randRange(-80, 80) end thanks for the attencions

Hi Aquila :)

As a reference for future questions. You can surround your source code with 3 punctuation marks ``` at the beginning and end. Makes it look much nicer :)

There are several things in your code that need to be fixed. Let me try one by one and I hope it makes sense.
The draw loop, your code:

draw = function() 
  screen.fillRect(0,0,screen.width,screen.height,"rgb(57,0,587)") 
  screen.drawMap("polonuovo", 0,0,2000,2000) 
  screen.clear 
  screen.drawSprite("alessandro", player.x-camera.x, player.y-camera.y 40, 40)
end  
  • screen.fillRect(...) will fill the whole screen with a blueish color. I guess that is the background.
  • screen.drawMap(...) here you draw your map
  • screen.clear() !!! This will clear the whole screen ... and your map will disappear!
  • screen.drawSprite(...) and now you draw your player

The screen can be cleared and it's background color defined by using the screen.clear("color") command. Also, the drawMap also needs to incorporate the camera shift to scroll along with it. The whole thing would now look like this:

draw = function() 
  screen.clear("rgb(57,0,587)") 
  screen.drawMap("polonuovo", 0-camera.x,0-camera.y,2000,2000) 
  screen.drawSprite("alessandro", player.x-camera.x, player.y-camera.y 40, 40)
end  

Uff, running out of time, I continue later, sorry. Other things you forgot were that you actually have to call your initCamera and updateCamera function to make them do some work. You only defined them.

I attach a quick and dirty example for now... gotta run.

https://microstudio.io/i/TinkerSmith/_help/

// some values are changed to suit this example
init = function() 
  player = object 
    x = 0 
    y = 0 
    speed = 1 
  end 
  initCamera()   // needs to be called once to initialize it
end

update = function() 
  if keyboard.UP then player.y += player.speed end 
  if keyboard.DOWN then player.y -= player.speed end 
  if keyboard.RIGHT then player.x += player.speed end 
  if keyboard.LEFT then player.x -= player.speed end
  updateCamera() // call on each update
end

draw = function() 
  screen.clear("rgb(57,0,587)") // clears the screen and sets the background color
  screen.drawMap("map", 0-camera.x,0-camera.y,480,320) 
  screen.drawSprite("icon", player.x-camera.x, player.y-camera.y, 30, 30) 
end 

//==================== function list =======================

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

updateCamera = function() 
  camera.x = clamp(player.x, -200, 200) 
  camera.y = clamp(player.y, -156, 156)
end

clamp = function(value, lower_limit, upper_limit)
  local val = max(value, lower_limit)
  val = min(val, upper_limit)
  return val
end

Post a reply

Progress

Status

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