The loop seemed to have fixed it! I also did change the math.random() to love.math.random() as the others suggested. Thank you all for the helpReFreezed wrote: ↑Wed Jun 08, 2022 8:44 am When you choose a new position for the apple, do it in a loop that repeats until the new position isn't close to the player (i.e. brute force it), something like this:
Also move math.randomseed, as the others have commented on. The reason the score goes up by a lot is because math.randomseed makes math.random return the same values for up to a second, because os.time returns whole seconds (i.e. it returns the same value for a second).Code: Select all
local function isCloseToPlayer(extraSpace) return (P.y + P.h > T.y - extraSpace) and (P.y < T.y + T.h + extraSpace) and (P.x + P.w > T.x - extraSpace) and (P.x < T.x + T.w + extraSpace) end function T:CheckOverlap() if isCloseToPlayer(0) then -- math.randomseed(os.time()) -- Do this only once, in love.load instead! Score = Score + 1 repeat T.x = math.random(love.graphics:getWidth()) T.y = math.random(love.graphics:getHeight()) until not isCloseToPlayer(100) -- 100 is how close the apple and the player can be. end end
[SOLVED] Collectible Object Spawns On Player Which Increases Score More Than Once
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Collectible Object Spawns On Player Which Increases Score More Than Once
Re: [SOLVED] Collectible Object Spawns On Player Which Increases Score More Than Once
The real problem was using the same seed over and over during one second, as togFox first noted. The loop just makes it never spawn at the player; it's a design decision whether that's desirable or not - it seems to me that your problem was with the abnormal score increase and not with the fact that it was spawned at the player, but I may be wrong. Using love.math.setRandomSeed(os.time()) would have led to the very same outcome when using love.math.random() instead of math.random().
On the other hand, LuaJIT's default random number generator is better than Löve's built-in one for simple use cases like this one, where you don't have to care about e.g. producing independently reproducible sequences. The main advantage is that it accepts seeding with an arbitrary float, taking into account all the decimals, while love.math.setRandomSeed() only accepts 32-bit integers. You can use Löve's love.timer.getTime() for seeding it with microsecond precision; in this case you would not have gotten the same number multiple times in consecutive frames if you had used love.timer.getTime() instead of os.time(), because from one frame to the next you could be sure that the seed had changed.
On the other hand, LuaJIT's default random number generator is better than Löve's built-in one for simple use cases like this one, where you don't have to care about e.g. producing independently reproducible sequences. The main advantage is that it accepts seeding with an arbitrary float, taking into account all the decimals, while love.math.setRandomSeed() only accepts 32-bit integers. You can use Löve's love.timer.getTime() for seeding it with microsecond precision; in this case you would not have gotten the same number multiple times in consecutive frames if you had used love.timer.getTime() instead of os.time(), because from one frame to the next you could be sure that the seed had changed.
Re: [SOLVED] Collectible Object Spawns On Player Which Increases Score More Than Once
Why not my solution? It gives any random position except near the player without the loop.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot], zxretrosoft and 7 guests