Page 2 of 3

Re: Inserting into tables

Posted: Sat Aug 18, 2012 6:53 pm
by Jakemason
Yes, here is the main.lua

Code: Select all

require "player"
require "world"
require "objects"

function love.load()
	player.load()
	world.load()
	objects.load()
end

function love.draw()
	player.draw()
	objects.draw()
end

function love.update(dt)
	player.update(dt)
	objects.update()
end

Re: Inserting into tables

Posted: Sat Aug 18, 2012 6:57 pm
by Nixola
Could you upload a .love?

Re: Inserting into tables

Posted: Sat Aug 18, 2012 7:06 pm
by Roland_Yonaba
Jakemason wrote:

Code: Select all

function objects.update()
	x, y = love.mouse.getPosition()
	if love.keyboard.isDown("space") then objects.ball.shouldDraw = true else objects.ball.shouldDraw = false end
end
and still nothing appears :/
The key check is " " and not "space", if you want to use the space button.
See keyConstants

Code: Select all

function objects.update()
	x, y = love.mouse.getPosition()
	if love.keyboard.isDown(" ") then ... end
end

Re: Inserting into tables

Posted: Sat Aug 18, 2012 7:10 pm
by Jakemason
One other thing, I compressed my folder into a zip on Mac and then changed the extension to .love and it told me there was no code.

Re: Inserting into tables

Posted: Sat Aug 18, 2012 7:12 pm
by Jakemason
Oh and, how do I make multiple instances of said ball? All it does at the moment is appear and follow the mouse, I want it to appear and stay at the x and y position and allow me to create more balls.

Re: Inserting into tables

Posted: Sat Aug 18, 2012 8:18 pm
by Qcode
To make a .love on a Mac you need to open your game folder highlight all of your game's contents (main.lua ect.) right click and select compress items. Change the name from archive.zip to gamename.love. Then that should work.

Re: Inserting into tables

Posted: Sat Aug 18, 2012 8:23 pm
by Roland_Yonaba
Know what ? Searching on the wiki sometimes helps fixing most of your problems by your own.
Jakemason wrote:One other thing, I compressed my folder into a zip on Mac and then changed the extension to .love and it told me there was no code.
See Game_distribution.
Don't try to zip the project folder, but its contents. So that when you open the zip file, you should directly see main.lua, conf.lua, etc.
Jakemason wrote:Oh and, how do I make multiple instances of said ball?
That's a game logic issue.
You just have to create multiple balls. Populate your objects table.
Then loop through the table to display them.

Code: Select all

function love.load()
   objects = {}
   objects.balls = {}
   for i =  1,10 do
      objects.balls[i] = {x = ..., y = ..., shouldDraw = true}
   end
   objects.ballImage = love.graphics.newImage(...)
end

...

function love.draw()
   ...
   for i,ball in ipairs(objects.balls) do
      if ball.shouldDraw then love.graphics.draw(objects.ballImage, ball.x, ball.y) end
   end
   ...
end
...
But have to fully understand how tables and control structures works in Lua.
You will find nice materials reading PiL: at least tables, Numeric for, Arrays, Multidimensional arrays chapters.
Jakemason wrote: All it does at the moment is appear and follow the mouse, I want it to appear and stay at the x and y position and allow me to create more balls.
That is because you used love.keyboard.isDown, meaning "as long as a certain key is down".
Maybe what you need is using callbacks.
See love.keypressed and love.keyreleased, and try to mix them with the base code provided before. Shouldn't be that hard to have it working.

Re: Inserting into tables

Posted: Sun Aug 19, 2012 12:38 am
by Jakemason
Ok, so after reading through the resources you kindly suggested I understand the concepts better. I am now able to place the ball at my mouse cursor and keep it there until the next time I click, but I haven't got it down how multiple objects works, so if anyone could help with that that would be great :)

Code: Select all

ball = {}
ball.x = 0 - 25
ball.y = 0 - 25
ball.alive = false

function ball.load()
	ball.image = love.graphics.newImage("images/ball.png")
end

function ball.draw()
	if ball.alive == true then
		for i,v in pairs(ball) do
			love.graphics.draw(ball.image,ball.x,ball.y)
		end
	end
end

function ball.update()
	
end

function love.mousepressed(x, y, button)
	if button == "l" then
		ball.x, ball.y = x -25, y -25
		ball.alive = true
	else
		ball.alive = false
	end
end

Re: Inserting into tables

Posted: Sun Aug 19, 2012 1:09 am
by Roland_Yonaba
Jakemason wrote:[...]but I haven't got it down how multiple objects works, so if anyone could help with that that would be great :)
Fair enough.
Jakemason wrote:

Code: Select all

ball = {}
ball.x = 0 - 25
ball.y = 0 - 25
ball.alive = false

...

function ball.draw()
	if ball.alive == true then
		for i,v in pairs(ball) do
			love.graphics.draw(ball.image,ball.x,ball.y)
		end
	end
end

...
You're not using pairs/ipairs the good way. :awesome:
See pairs/ipairs.
Lua_tables tutorial gives also great examples.

In your implementation, each step, i and v will go through differents states:
1. i = "x", v = -25
2. i = "y", v = -25
3. i = "alive", v = false
4. i = "image", v = __userdata__
That is, just because you're looping inside table ball. Got it know ?
You have to define another table where you will list every different ball you need to create.
This second table can be placed inside the original table named ball.

This means:

Code: Select all

-- as before
ball = {}
ball.x = 0 - 25
ball.y = 0 - 25
ball.balls = {} -- will hold all my new balls

-- So that you can populate it this way.
ball.balls[1] = { x = .., y = ..., alive = false}
ball.balls[2] = { x = .., y = ..., alive = false}
ball.balls[3] = { x = .., y = ..., alive = false}
...
As you like.

Then looping on table ball.balls outputs:

Code: Select all

for i,thisBall in ipairs(ball.balls) do
   if thisBall.alive then love.graphics.draw(ball.image,thisBall.x,thisBall.y)
end
Next time, please provide a *.love file.

Re: Inserting into tables

Posted: Sun Aug 19, 2012 1:38 am
by Jakemason
Thanks for that :)

So is this the idea?

Code: Select all

ball = {}
ball.balls = {}
ball.x = 0 
ball.y = 0 
ball.alive = false

function ball.load()
	ball.image = love.graphics.newImage("images/ball.png")
end

function ball.draw()
	for i,v in ipairs(ball.balls) do
   		if ball.alive then 
   			table.insert(ball.balls[i]{x = ball.x, y = ball.y})
   			love.graphics.draw(ball.image,x,y)
   		end
   	end
end

function ball.update()
	
end

function love.mousepressed(x, y, button)
	if button == "l" then
		ball.x, ball.y = x -25, y -25
		ball.alive = true
	else
		ball.alive = false
	end
end