Why do they keep going to the same position
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Why do they keep going to the same position
So right now I'm trying to work on an enemy spawner I want the y to be the same but the x needs to be random. It is random and they are being spawned... But at the same spot. I've tried changing the seed but it still doesn't work. can anyone please help here is my love file
- Attachments
-
- conf.love
- (4.56 KiB) Downloaded 69 times
Re: Why do they keep going to the same position
The lib src/enemy.lua must have such structure:
and the main.lua:
You can call the math random as https://love2d.org/wiki/love.math.random
Code: Select all
local enemy = {} -- it is local!
function enemy:load()
end
function enemy:update(dt)
end
function enemy:draw()
end
return enemy -- return the local table
Code: Select all
local enemy = require("src/enemy")
You can call the math random as https://love2d.org/wiki/love.math.random
Re: Why do they keep going to the same position
i updated it with
and it does the same thing as before
Code: Select all
function enemy:load()
enemy.spawnTime = 0.1
enemy.x = love.math.random(0, 800)
enemy.y = 50
enemy.w = 20
enemy.h = 20
table.insert(enemies, enemy)
table.insert(enemies, enemy)
end
Re: Why do they keep going to the same position
oh wait actually it makes the enemies freak out and glitch all over the screen how can i make it so that they just stay where their random number picked
Re: Why do they keep going to the same position
There are several problems in the code, one of which is there seems to be confusion about what the global 'enemy' variable represents. Is it supposed to be a kind of manager for multiple enemies, or is the (one and only) enemy. (Same with the global 'player' variable.) 'enemy' currently references the exact same table at all times. You also assign a new table to the global variable 'bullet', and another different new table to player.bullet (which is not referenced anywhere else in the code), in the player.shoot function. The global variable 'bullet' will thus always reference the last "spawned" bullet.
Another issue is that pairs() is used in loops when ipairs() should be used. The former doesn't care about the order of the items in the table (and is slower). Note that both ipairs() and pairs() may/will skip one element in the two places you call table.remove(player.bullets,i) inside the loops. Removals needs to happen something like this:
You should read up on local variables, modules, and classes.
Another issue is that pairs() is used in loops when ipairs() should be used. The former doesn't care about the order of the items in the table (and is slower). Note that both ipairs() and pairs() may/will skip one element in the two places you call table.remove(player.bullets,i) inside the loops. Removals needs to happen something like this:
Code: Select all
for i = #player.bullets, 1, -1 do -- Loop backwards.
if whatever then
table.remove(player.bullets, i) -- Changes position of all bullets after 'i' (that we have already looped through as we loop backwards, so we don't care that their position changed).
end
end
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
"If each mistake being made is a new one, then progress is being made."
Re: Why do they keep going to the same position
thank you for helping me make it better but how can I do random generation? I just want it to spawn at a fixed y axis but always have the x value be random and i want it to spawn every 0.1 seconds. I got the actual generation working with a random value but no matter what happens the enemies get spawned at the same position when I want it to be diffrent
Re: Why do they keep going to the same position
You are need:
So you can draw it as
Code: Select all
local function makeNewEnemy ()
local enemy = {} -- new table, not your library
enemy.spawnTime = 0.1
enemy.x = love.math.random(0, 800)
enemy.y = 50
enemy.w = 20
enemy.h = 20
return enemy
end
function enemy:load()
-- self is your library enemy, it has methods self:load, self:update and self:draw;
self.enemies = {} -- also your library has now a new empty list of enemies
local newEnemy = makeNewEnemy ()
table.insert (self.enemies, newEnemy) -- adding new enemy to the list of enemies
end
Code: Select all
function enemy:draw()
for i, enemy in ipairs (self.enemies) do -- iterate your list of enemies
love.graphics.rectangle ('line', enemy.x, enemy.y, enemy.w, enemy.h)
end
end
Re: Why do they keep going to the same position
Here's one way to spawn enemies dynamically at a fixed interval:
Code: Select all
local enemies = {}
local function spawnNewEnemy()
local enemy = {
x = math.random(0, 800),
y = 50,
w = 20,
h = 20,
}
function enemy:update(dt)
self.y = self.y + 40 * dt
end
function enemy:draw()
love.graphics.rectangle("line", self.x,self.y, self.w,self.h)
end
table.insert(enemies, enemy)
end
local enemySpawnTimer = 0
local function updateEnemySpawner(dt)
enemySpawnTimer = enemySpawnTimer - dt
if enemySpawnTimer < 0 then
enemySpawnTimer = 0.1
spawnNewEnemy()
end
end
function love.update(dt)
-- Update enemies and other stuff.
updateEnemySpawner(dt)
for _, enemy in ipairs(enemies) do
enemy:update(dt)
end
-- Despawn off-screen enemies and other stuff.
for i = #enemies, 1, -1 do
local enemy = enemies[i]
if enemy.y > love.graphics.getHeight() then
table.remove(enemies, i)
end
end
end
function love.draw()
for _, enemy in ipairs(enemies) do
enemy:draw()
end
love.graphics.print("Enemies: " .. #enemies)
end
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
"If each mistake being made is a new one, then progress is being made."
Who is online
Users browsing this forum: Ahrefs [Bot] and 4 guests