Help out a beginner (spawning objects)

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Help out a beginner (spawning objects)

Post by nice »

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.
:awesome: Have a good day! :ultraglee:
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Help out a beginner (spawning objects)

Post by Davidobot »

Well, I would do something like this:

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
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.
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
User avatar
ThatTreeOverThere
Prole
Posts: 2
Joined: Sat Jul 27, 2013 8:49 pm

Re: Help out a beginner (spawning objects)

Post by ThatTreeOverThere »

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
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Help out a beginner (spawning objects)

Post by Davidobot »

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
You can just use math.random?
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
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Help out a beginner (spawning objects)

Post by nice »

Davidobot wrote:Well, I would do something like this:

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
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.
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.
:awesome: Have a good day! :ultraglee:
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Help out a beginner (spawning objects)

Post by Davidobot »

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.
You should have been more specific then, plus, I was just making an example.

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
There, is that better? :P
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
User avatar
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)

Post by Roland_Yonaba »

Davidobot wrote:
Knodd wrote: There, is that better? :P
Well, it should be better if you close the parentheses.

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
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Help out a beginner (spawning objects)

Post by Davidobot »

Roland_Yonaba wrote:
Davidobot wrote:
Knodd wrote: There, is that better? :P
Well, it should be better if you close the parentheses.

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
Sorry, I was typing on a mobile device.
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
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Help out a beginner (spawning objects)

Post by nice »

Roland_Yonaba wrote:
Davidobot wrote:
Knodd wrote: There, is that better? :P
Well, it should be better if you close the parentheses.

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/hboo1ppxa4ph3 ... roblem.png
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
:awesome: Have a good day! :ultraglee:
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Help out a beginner (spawning objects)

Post by Davidobot »

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
Post Reply

Who is online

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