For some reason, my randomized mob spawning(randomizes the mob's X and Y coordinates) keeps spamming the mob, not just spawning it once. How can I fix this? main
require "core/player"
require "core/physics"
require "core/font"
require "core/entity"
function love.load()
player_create() --Creates the player
entity_mob() --Spawns the entity "mob"
love.graphics.setBackgroundColor(104, 136, 248)
end
function love.update(dt)
player_move(dt)
end
function love.draw()
player_draw()
entity_mobDraw()
end
function love.mousepressed(x, y, button)
end
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
function spawn_random()
x = math.random(1, width)
y = math.random(1, height)
entX = x
entY = y
if x - 10 < objects.player.body:getX() then
entX = x
elseif y - 10 < objects.player.body:getY() then
entY = y
end
end
Here it is. I have removed require "core/spawn" and spawn_random() from entity_mobDraw() in "entity.lua" and included spawn script into "main.lua". Also I've made first spawning for mob. And ability to respawn it with pressing "m" key.
local time_since_last_spawn = 0
local seconds_until_next_spawn = 0
function love.update(dt)
-- If time_since_last_spawn is equal or greater to
-- seconds_until_next_spawn then that means it is time to spawn
-- another mob. Then we reset time_since_last_spawn to zero and
-- assign seconds_until_next_spawn a random number between one and
-- ten seconds, which is how long we will wait before creating
-- another mob.
if time_since_last_spawn >= seconds_until_next_spawn then
spawn_random()
time_since_last_spawn = 0
seconds_until_next_spawn = math.random(10)
else
-- If we did not spawn anything then keep adding up the amount
-- of time that has passed since each call to love.update()
-- until we meet the condition above.
time_since_last_spawn = time_since_last_spawn + dt
end
end
local time_since_last_spawn = 0
local seconds_until_next_spawn = 0
function love.update(dt)
-- If time_since_last_spawn is equal or greater to
-- seconds_until_next_spawn then that means it is time to spawn
-- another mob. Then we reset time_since_last_spawn to zero and
-- assign seconds_until_next_spawn a random number between one and
-- ten seconds, which is how long we will wait before creating
-- another mob.
if time_since_last_spawn >= seconds_until_next_spawn then
spawn_random()
time_since_last_spawn = 0
seconds_until_next_spawn = math.random(10)
else
-- If we did not spawn anything then keep adding up the amount
-- of time that has passed since each call to love.update()
-- until we meet the condition above.
time_since_last_spawn = time_since_last_spawn + dt
end
end
IndieKid wrote: This will respawn a mob, not spawn a new one.
You mean the spawn_random() function respawns a mob? If that’s the case then couldn’t you replace that with a call to the function to create a new one? Or I may have just misunderstood the mob-spawning code when I glanced through the Love file you posted, in which case my apologies.