Love Remove Image command Request

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
walesmd
Prole
Posts: 22
Joined: Fri Oct 08, 2010 3:11 am
Location: Augusta, GA
Contact:

Re: Love Remove Image command Request

Post 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
User avatar
weilies
Prole
Posts: 28
Joined: Sat Oct 09, 2010 6:16 am

Re: Love Remove Image command Request

Post 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
User avatar
walesmd
Prole
Posts: 22
Joined: Fri Oct 08, 2010 3:11 am
Location: Augusta, GA
Contact:

Re: Love Remove Image command Request

Post 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)})
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Love Remove Image command Request

Post 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
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: No registered users and 100 guests