Page 1 of 1

Randomized Mob Spawning spamming mobs

Posted: Fri Jan 18, 2013 9:47 pm
by n1ghtk1n9
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

Code: Select all

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
entity

Code: Select all

require "core/spawn"

entity = {}
entity_limit = false

function entity_spawn(entity)
	
end

function entity_mob()
	entity.mob = {}
	entity.mob.body = love.physics.newBody(world, entX, entY, "dynamic")
	entity.mob.shape = love.physics.newRectangleShape(15, 20)
	entity.mob.fixture = love.physics.newFixture(entity.mob.body, entity.mob.shape, 1)
end

function entity_mobDraw()
	spawn_random()
	love.graphics.setColor(166, 166, 50)
	love.graphics.rectangle("fill", entX, entY, 15, 20)
	entity_limit = true
end

function entity_limit()
	
end
spawn

Code: Select all

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

Re: Randomized Mob Spawning spamming mobs

Posted: Fri Jan 18, 2013 11:21 pm
by SiENcE
Put spawn_random() in love.load

>> entity_mobDraw() is called in love.draw() and love.draw() is called every frame

cheers

Re: Randomized Mob Spawning spamming mobs

Posted: Sat Jan 19, 2013 2:02 pm
by IndieKid
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.

Code: Select all

function love.load()
	<...>
	spawn_random()  --Spawns first mob
end

Code: Select all

function love.keypressed(key) -- I have added this to respawn mobs
	if key == "m" then
		spawn_random()
	end
end
.love of your game \/

Re: Randomized Mob Spawning spamming mobs

Posted: Sat Jan 19, 2013 7:02 pm
by ejmr
If you want to spawn mobs at random time intervals then this code may be helpful, although I will admit I have not tested it.

Code: Select all

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

Re: Randomized Mob Spawning spamming mobs

Posted: Sat Jan 19, 2013 7:19 pm
by IndieKid
ejmr wrote:If you want to spawn mobs at random time intervals then this code may be helpful, although I will admit I have not tested it.

Code: Select all

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
This will respawn a mob, not spawn a new one.

Re: Randomized Mob Spawning spamming mobs

Posted: Sat Jan 19, 2013 8:22 pm
by ejmr
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.

Re: Randomized Mob Spawning spamming mobs

Posted: Sun Jan 20, 2013 10:08 pm
by n1ghtk1n9
SiENcE wrote:Put spawn_random() in love.load

>> entity_mobDraw() is called in love.draw() and love.draw() is called every frame

cheers
I did what you told me(or at least how I interpreted it) and it doesn't even spawn the mob anymore
Here is all of my code, what have I done wrong?