@TinkerSmith is absolutely right. Indeed, it is necessary to take away the camera position not only for the player but also for the stars. I understand you are using the lesson from @mrLman to implement the camera? Unfortunately, I can't see your code now, it's already closed :-)
You also want to place stars randomly with the ability to collect them and use a counter, am I right?
As @TinkerSmith said, this can be done in many ways. I have sketched an example of how this can be implemented.
I'll send the whole code here. I will also open access to the project so that you can see it in action. I'll probably explain how it works a little later. :-) In the meantime, you can disassemble this code. If you have any questions, then ask them here, I will definitely answer as soon as I'm not busy :-)
init = function()
initCamera()
level = object
width = 1024
height = 512
end
player = object
name ="player"
x = 0
y = 0
width = 16
height = 16
speed = 2
totalStars = 0
end
Stars = class
name = "star"
display = true
width = 16
height = 16
drawStars = function(list)
for star in list
screen.setAlpha(star.display)
screen.drawSprite(star.name,star.x-camera.x,star.y-camera.y,star.width,star.height)
screen.setAlpha(1)
end
end
end
ListRandomPositionX = []
for x = -level.width/2+Stars.width/2 to level.width/2-Stars.width/2 by Stars.width
ListRandomPositionX.push(x)
end
ListRandomPositionY = []
for y = -level.height/2+Stars.height/2 to level.height/2-Stars.height/2 by Stars.height
ListRandomPositionY.push(y)
end
starsList = []
for i = 0 to 49 //set the number of stars as desired (now there are 50)
star = new Stars
star.x = ListRandomPositionX[random.nextInt(ListRandomPositionX.length+1)]
star.y = ListRandomPositionY[random.nextInt(ListRandomPositionY.length+1)]
starsList.push(star)
end
end
update = function()
updateCamera()
if keyboard.LEFT then player.x -= player.speed end
if keyboard.RIGHT then player.x += player.speed end
if keyboard.UP then player.y += player.speed end
if keyboard.DOWN then player.y -= player.speed end
player.x = clamp(player.x,-level.width/2+8,level.width/2-8)
player.y = clamp(player.y,-level.height/2+8,level.height/2-8)
for star in starsList
if distance(player.x,player.y,star.x,star.y) < 16 then
if star.display == 1 then
player.totalStars += 1
end
star.display = false
end
end
end
draw = function()
screen.clear("rgb(170,227,255)")
screen.drawMap("map1",-camera.x,-camera.y,level.width,level.height)
Stars.drawStars(starsList)
screen.drawSprite(player.name,player.x-camera.x,player.y-camera.y,player.width,player.height)
screen.drawText("stars: "+player.totalStars+"/"+starsList.length,80,80,20,"rgb(255,255,255)")
end
//function
clamp = function(value, lower_limit, upper_limit)
local val = max(value, lower_limit)
val = min(val, upper_limit)
return val
end
distance = function(x1, y1, x2, y2)
local a = x2 - x1
local b = y2 - y1
local c = sqrt(pow(a, 2) + pow(b, 2))
return c
end
initCamera = 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