Randomized Mob Spawning spamming mobs

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
n1ghtk1n9
Prole
Posts: 10
Joined: Sat Dec 22, 2012 12:23 am

Randomized Mob Spawning spamming mobs

Post 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
Attachments
Roughdraft Game.love
(480.46 KiB) Downloaded 79 times
User avatar
SiENcE
Party member
Posts: 792
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: Randomized Mob Spawning spamming mobs

Post by SiENcE »

Put spawn_random() in love.load

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

cheers
User avatar
IndieKid
Citizen
Posts: 80
Joined: Sat Dec 22, 2012 7:05 pm
Contact:

Re: Randomized Mob Spawning spamming mobs

Post 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 \/
Attachments
Roughdraft Game fixed spawning.love
Press "m" to respawn mob.
(571.55 KiB) Downloaded 77 times
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Randomized Mob Spawning spamming mobs

Post 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
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
IndieKid
Citizen
Posts: 80
Joined: Sat Dec 22, 2012 7:05 pm
Contact:

Re: Randomized Mob Spawning spamming mobs

Post 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.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Randomized Mob Spawning spamming mobs

Post 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.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
n1ghtk1n9
Prole
Posts: 10
Joined: Sat Dec 22, 2012 12:23 am

Re: Randomized Mob Spawning spamming mobs

Post 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?
Attachments
Roughdraft2 Help.7z
(480.61 KiB) Downloaded 63 times
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 30 guests