Page 1 of 2

Help out a beginner (spawning objects)

Posted: Sun Sep 15, 2013 4:02 pm
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.

Re: Help out a beginner (spawning objects)

Posted: Sun Sep 15, 2013 4:33 pm
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.

Re: Help out a beginner (spawning objects)

Posted: Sun Sep 15, 2013 5:25 pm
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

Re: Help out a beginner (spawning objects)

Posted: Sun Sep 15, 2013 5:49 pm
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?

Re: Help out a beginner (spawning objects)

Posted: Sun Sep 15, 2013 6:28 pm
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.

Re: Help out a beginner (spawning objects)

Posted: Sun Sep 15, 2013 6:36 pm
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

Re: Help out a beginner (spawning objects)

Posted: Sun Sep 15, 2013 6:53 pm
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

Re: Help out a beginner (spawning objects)

Posted: Sun Sep 15, 2013 6:57 pm
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.

Re: Help out a beginner (spawning objects)

Posted: Sun Sep 15, 2013 8:10 pm
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

Re: Help out a beginner (spawning objects)

Posted: Mon Sep 16, 2013 3:28 am
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