nfey wrote:I understand you added the first code block to the newMap() function. Where does the newMap() function get called?
Until you reply, I'll chime in with some general advice:
The love.graphics.whatever calls are used to draw stuff. You should only use these in the love.draw() function. But in order to call these methods in love.draw() you need to have the information for what you're trying to draw (in your case, the pickups and the rectangles representing them), right? The instinct is to calculate these parameters (location for pickup, etc.) on the spot and then draw them. But the problem is that the draw function is called dozens of times per second. If you have a math.random() call inside the love.draw() call, calculating the chance of something being drawn, this means that you will get a different result for every screen redraw (i.e. every frame).
The solution is to separate your initial game state loading (love.load()), your game state changing logic(love.update()) and your drawing logic (love.draw()). In the case of your pickup, the flow would be like this: you calculate the chance for the pickup to be spawned in love.load() and save the result in a variable (or on the array representing the world map); you update this information from the world map in love.update(), if the player walks over the pickup for example you need to delete it from the world and change the player's state somehow - his health or power or whatever; then, in love.draw(), you use the information from the world map to draw the pickup, and the pickup's state is being updated already in love.update() so you will always draw the correct representation.
I don't know where you're calling your newMap() method, but it seems to me like you're mixing up these 3 phases.
Hey there I solved that problem I had. I now can add different items and pick them up. Now I wanted to add enemies that get spawned randomly on the map. I got pretty close I think, but somehow it only draws one enemy instead of all and then when I press space again for a new enemy it just overwrites the old one.
I am first randomly selecting a tilex and y and then check if they are ' ' and yea that part doesnt really work. Then I make a table with the x and y.
When space is pressed it inserts into a empty table and writes the enemy table we just created. To draw then I iterate through the enemies table and draw them.
Code: Select all
--enemies.lua
require'map'
function createEnemy()
local maxtilesx = 50
local maxtilesy = 30
local tileheight = 12
local enemyx = math.random(1,maxtilesx)
local enemyy = math.random(1,maxtilesy)
--[[while checkEnemy(enemyx,enemyy) == false do
enemyx = math.random(1,maxtilesx)
enemyy = math.random(1,maxtilesy)
end
enemy = {}
enemy.x = enemyx
enemy.y = enemyy
return enemy]]
if checkEnemy(enemyx,enemyy) == true then
createEnemy()
elseif checkEnemy(enemyx,enemyy) == false then
enemy = {}
enemyx = math.random(1,maxtilesx)
enemyy = math.random(1,maxtilesy)
--enemy.x = 12*(enemyx-1)+200
--enemy.y = 12*(enemyy-1)
enemy.x = enemyx
enemy.y = enemyy
end
return enemy
end
Code: Select all
--map.lua has more in it with the map generation etc.
function checkEnemy(columnIndex,rowIndex)
if tileTable[columnIndex][rowIndex] ~= ' ' then
return true
end
return false
end
Code: Select all
--output.lua
-- gets called in love.draw()
function output.enemies()
local tileW, tileH = 12,12
local tilesetW, tilesetH = tilesheet:getWidth(), tilesheet:getHeight()
enemyquad = love.graphics.newQuad(12, 0, tileW, tileH, tilesetW, tilesetH)
for i, v in ipairs(enemies) do
local x,y = (enemy.x-1)*tileW+200, (enemy.y-1)*tileH+0
love.graphics.draw(tilesheet,enemyquad,x,y) -- Then draw them.
end
end
Also they seem to spawn in places where the floor isnt a space ' '.