Page 1 of 1
Math.random help :S
Posted: Sat Dec 14, 2013 9:43 am
by Puzzelism
YO:
I'm having a problem with my code in math.random where it keeps giving me the same integer value every single time, i know it has to be seeded but i don't know how in these circumstances
Code: Select all
enemy = {}
enemyX = math.random(0, 300)
enemyY = math.random(0, 500)
enemyWidth = math.random(32, 50)
enemyHeigth = math.random(32, 50)
function enemy_spawn()
love.graphics.setColor(255,0,0)
love.graphics.rectangle("fill", enemyX, enemyY, enemyWidth, enemyHeight)
end
Also, in the part where i have
Code: Select all
love.graphics.rectangle("fill", enemyX, enemyY, enemyWidth, enemyHeight)
it gives me an error where the enemyWidth/Height isnt being recognized as an integer...
ALL HELP APPRECIATED, THANKS!
Re: Math.random help :S
Posted: Sat Dec 14, 2013 12:19 pm
by veethree
If you post your .love we you can help us help you.
First the error, Are you sure the enemyX, enemyY etc. variables aren't inside the enemy table? If not you don't really need that table.
As far as the math.random problem,Since 0.8.0 the randomseed is randomized right before love.load() is called, So i'm not sure what the problem is. If you post your code, I can look through it and test it myself to try and figure out what's wrong.
Re: Math.random help :S
Posted: Sat Dec 14, 2013 2:17 pm
by Robin
Okay: here's the deal: you have a function called
enemy_spawn(), but you should really call it
enemy_draw, because that's what it does: it draws an enemy. Your code as-is creates a single enemy at the start of the game, and that's it. Here's a suggestion that you can start from:
Code: Select all
enemies = {}
function spawn_enemy()
local e = {}
e.x = math.random(0, 300)
e.y = math.random(0, 500)
e.width = math.random(32, 50)
e.height = math.random(32, 50)
table.insert(enemies, e)
end
function draw_enemies()
love.graphics.setColor(255,0,0)
for i, enemy in ipairs(enemies) do
love.graphics.rectangle("fill", enemy.x, enemy.y, enemy.width, enemy.height)
end
end
Can you take it from here or do you want more explanation?
Re: Math.random help :S
Posted: Sat Dec 14, 2013 3:58 pm
by NightKawata
Code: Select all
love.graphics.rectangle("fill", enemyX, enemyY, enemyWidth, enemyHeight)
Fix the typo. You can criticise his coding methods later.
As to the seeding part, LOVE should do that automatically in love.run. Since 0.9.0 is also out, you can use
this for a little bit more consistency.
Re: Math.random help :S
Posted: Sat Dec 14, 2013 6:55 pm
by Puzzelism
Thanks for the responses, also thank you for finding the typo, i guess i should have looked it over more first... I've also found that I have an older LOVE version, because when installing the newest version and trying to run my games, LOVE just stops responding, i think that's why it didn't automatically seed it. So far though it seems that if i just put
at the top of my code it temporarily fixes it until i can get the newest version to work...
Anyways, thanks for the help!
Re: Math.random help :S
Posted: Sat Dec 14, 2013 11:39 pm
by NightKawata
Wait, stops responding as in it actually crashes, or do you just get some kind of blue error screen?
If it's the latter, you can just update a bunch of functions to fix that up. (I'd assume some love.graphics functions got renamed to love.window functions in that, but eh) If it's the former, uh
Re: Math.random help :S
Posted: Sun Dec 15, 2013 1:08 am
by Puzzelism
Crashes as in goes to the stupid white "LOVE.exe is not responding" screen.