Page 2 of 2

Re: Love Remove Image command Request

Posted: Wed Oct 13, 2010 1:37 pm
by walesmd
isn't that better to store the object to a variable (as an object), so that later can use object.scaleX, object.getPostX, object.RemoveMe
It appears as if you are coming from a more object-oriented background, Lua (and LOVE) are lower-level than this. Below is a simple code sample that explains what your object.removeMe() method above is actually doing at a lower level, which is how you would have to implement it in Lua. Maybe this can help get you on the right track:

Code: Select all

-- We're going to draw some balls on the screen
function love.load()
	-- Some basic variables about our game
	game = {}
	game.width = love.graphics.getWidth()
	game.height = love.graphics.getHeight()

	-- Some data about our balls
	max_balls = 10
	ball_radius = 20
	
	-- This table will store each ball that will be drawn on screen
	balls = {}
end


function love.draw()
	-- Draw all of the balls that are in the balls table
	for _, ball in pairs(balls) do
		love.graphics.circle("fill", ball.x, ball.y, ball_radius)
	end
end


function love.keypressed(key)
	-- Add and subtract balls, the subtraction part here is like your object.removeMe()
	if key == "kp+" and #balls < max_balls then
		table.insert(balls, 1, {x = math.random(game.width - ball_radius), y = math.random(game.height - ball_radius)})
	elseif key == "kp-" and #balls > 0 then
		table.remove(balls)
	end
	
	-- Quit the game
	if key == "escape" then
		love.event.push("q")
	end
end

Re: Love Remove Image command Request

Posted: Wed Oct 13, 2010 3:10 pm
by weilies
Yup walesmd,

That's what i am looking for! Thanks pal!

moreover, in order to chose which one to be remove, we can specify second optional param

Code: Select all

   elseif key == "b" and #balls > 0 then
      table.remove(balls, 2)
   end
where 2 is the object we wanna remove from screen

Re: Love Remove Image command Request

Posted: Wed Oct 13, 2010 3:38 pm
by walesmd
Yep - you'll obviously have to manage the objects better than I did in that example. Also, with the insertion the second parameter is the index within the table, so the code I had above always places the data at an index of 1 (and pushes all other data up an index).

Code: Select all

table.insert(balls, 1, {x = math.random(game.width - ball_radius), y = math.random(game.height - ball_radius)})

Re: Love Remove Image command Request

Posted: Wed Oct 13, 2010 5:53 pm
by vrld
walesmd wrote:Yep - you'll obviously have to manage the objects better than I did in that example.
I usually take the object as key (and value) to the table:

Code: Select all

local actor = Actor(stuff)
actors[actor] = actor
This way, removing of an actor is really simple (and safe):

Code: Select all

actors[actor] = nil
The drawing/update operations are done with a pairs loop:

Code: Select all

for _, actor in pairs(actors) do
    actor:draw()
end