Hi!
I'm pretty new to lua and I would need a few pointers how to spawn an object randomly within an area of 800,600.
Thank you for your help.
Help out a beginner (spawning objects)
Help out a beginner (spawning objects)
Have a good day!
Re: Help out a beginner (spawning objects)
Well, I would do something like this:
This code presumes that you have an image called enemyImage and that you have a global table used for storing enemies called enemies. Also, this thread is meant to be in the Support and Development section. Feel free to ask questions.
Code: Select all
function spawnEnemy(id)
local enemy = {}
enemy.health = 10
enemy.id = id
enemy.image = enemyImage
enemy.x = math.random(0, love.graphics.getWidth() - enemyImage:getWidth()
enemy.y = math.random(0, love.graphics.getHeight() - enemyImage:getHeight()
table.insert(enemies, enemy)
end
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
personal page and a raycaster
- ThatTreeOverThere
- Prole
- Posts: 2
- Joined: Sat Jul 27, 2013 8:49 pm
Re: Help out a beginner (spawning objects)
In Love 0.9.0, you can use love.math.random to generate a random number.
In other versions, you'll have to use something like this: http://stackoverflow.com/questions/1868 ... -generator
In other versions, you'll have to use something like this: http://stackoverflow.com/questions/1868 ... -generator
Re: Help out a beginner (spawning objects)
You can just use math.random?ThatTreeOverThere wrote:In Love 0.9.0, you can use love.math.random to generate a random number.
In other versions, you'll have to use something like this: http://stackoverflow.com/questions/1868 ... -generator
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
personal page and a raycaster
Re: Help out a beginner (spawning objects)
What I want is that the object I want to spawn by clicking the left mouse button on the screen (800,600) and the object (an upside down triangle) to appear randomly across the screen.Davidobot wrote:Well, I would do something like this:This code presumes that you have an image called enemyImage and that you have a global table used for storing enemies called enemies. Also, this thread is meant to be in the Support and Development section. Feel free to ask questions.Code: Select all
function spawnEnemy(id) local enemy = {} enemy.health = 10 enemy.id = id enemy.image = enemyImage enemy.x = math.random(0, love.graphics.getWidth() - enemyImage:getWidth() enemy.y = math.random(0, love.graphics.getHeight() - enemyImage:getHeight() table.insert(enemies, enemy) end
And my objects doesn't have any health so what you suggested is not exactly what I want.
Have a good day!
Re: Help out a beginner (spawning objects)
You should have been more specific then, plus, I was just making an example.Knodd wrote: What I want is that the object I want to spawn by clicking the left mouse button on the screen (800,600) and the object (an upside down triangle) to appear randomly across the screen.
And my objects doesn't have any health so what you suggested is not exactly what I want.
Code: Select all
function love.mousepressed(x, y, button)
local object = {}
object.image = triangleImage
object.x = math.random(0, love.graphics.getWidth() - enemyImage:getWidth())
object.y = math.random(0, love.graphics.getHeight() - enemyImage:getHeight())
table.insert(objects, object)
end
Last edited by Davidobot on Sun Sep 15, 2013 6:57 pm, edited 1 time in total.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
personal page and a raycaster
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Help out a beginner (spawning objects)
Well, it should be better if you close the parentheses.Davidobot wrote:Knodd wrote: There, is that better?
Code: Select all
function love.mousepressed(x, y, button)
local object = {}
object.image = triangleImage
object.x = math.random(0, love.graphics.getWidth() - enemyImage:getWidth())
object.y = math.random(0, love.graphics.getHeight() - enemyImage:getHeight())
table.insert(objects, object)
end
Re: Help out a beginner (spawning objects)
Sorry, I was typing on a mobile device.Roland_Yonaba wrote:Well, it should be better if you close the parentheses.Davidobot wrote:Knodd wrote: There, is that better?
Code: Select all
function love.mousepressed(x, y, button) local object = {} object.image = triangleImage object.x = math.random(0, love.graphics.getWidth() - enemyImage:getWidth()) object.y = math.random(0, love.graphics.getHeight() - enemyImage:getHeight()) table.insert(objects, object) end
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
personal page and a raycaster
Re: Help out a beginner (spawning objects)
https://www.dropbox.com/s/hboo1ppxa4ph3 ... roblem.pngRoland_Yonaba wrote:Well, it should be better if you close the parentheses.Davidobot wrote:Knodd wrote: There, is that better?
Code: Select all
function love.mousepressed(x, y, button) local object = {} object.image = triangleImage object.x = math.random(0, love.graphics.getWidth() - enemyImage:getWidth()) object.y = math.random(0, love.graphics.getHeight() - enemyImage:getHeight()) table.insert(objects, object) end
https://www.dropbox.com/s/z315tvvlue4sj ... .51.57.png
This is what I get then I try to run it, it runs well then I start it but then I press the mouse button I get an error message
Have a good day!
Re: Help out a beginner (spawning objects)
Looking at your code, I noticed that you need to change triangleImage to triangle. And you forgot to declare an objects tale, plus the drawing code should look something like this:
Code: Select all
love.draw()
for i,v in ipairs(objects)
if v then
love.graphcis.draw(v.image, v.x, v.y)
end
end
end
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
personal page and a raycaster
Who is online
Users browsing this forum: Semrush [Bot] and 3 guests