Making all buttons spawn with love.load()

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.
Post Reply
User avatar
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

Making all buttons spawn with love.load()

Post by BoopDeePoop »

I'm curious if it's possible to make all of the buttons in my game spawn when love.load() is ran. I have buttons in gamestates other than menu which is what you start in upon loading the game. At the moment this works:

Code: Select all

function button_IsSpawned()
if gamestate == "settingstate" then
	if b_spawned_set == false then
		button_spawn(12,630,"back",back_button_img)
		button_spawn(25,gheight/2-165,"v_left",leftarrow_button_img)
		button_spawn(375,gheight/2-165,"v_right",rightarrow_button_img)
		button_spawn(25,gheight/2-10,"b_left",leftarrow_button_img)
		button_spawn(375,gheight/2-10,"b_right",rightarrow_button_img)
		button_spawn(25,gheight/2+125,"controls",controls_button_img)
		button_spawn(830,gheight/2-165,"s_left",leftarrow_button_img)
		button_spawn(gwidth-100,gheight/2-165,"s_right",rightarrow_button_img)
		b_spawned_set = true
	end
elseif gamestate == "lost" then
	if b_spawned_lost == false then
		button_spawn(gwidth/2-237,gheight/2+79,"yes",yes_button_img)
		button_spawn(gwidth/2+79,gheight/2+79,"no",no_button_img)
		b_spawned_lost = true
	end
elseif gamestate == "localscores" then
	if b_spawned_scores == false then
		button_spawn(12,630,"back",back_button_img)
		b_spawned_scores = true
	end
elseif gamestate == "controlstate" then
	if b_spawned_controls == false then
		button_spawn(12,630,"backcontrol",back_button_img)
		b_spawned_controls = true
	end
elseif paused == true then
	if b_spawned_pause == false then
		button_spawn(gwidth/2+79,gheight/2+79,"return",back_button_img)
		button_spawn(gwidth/2-237,gheight/2+79,"resume",resume_button_img)
		b_spawned_pause = true
		end
	end
end
I feel like this is far too much code for what I'm trying to do. I have this function running in love.update(dt) which I feel like I shouldn't be doing.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Making all buttons spawn with love.load()

Post by davisdude »

You could spawn them all at the beginning- yes. Then, depending on the state, set them to draw or not draw using a boolean. Whenever the states change, update the booleans (you could do this via having a table with strings containing the gamestates during which it should be drawn).
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

Re: Making all buttons spawn with love.load()

Post by BoopDeePoop »

Could I see an example of that? I'm still trying to grasp onto coding. I'm having lots of trouble but slowly learning stuff.
lachlaan
Prole
Posts: 30
Joined: Sun Jun 30, 2013 7:23 pm

Re: Making all buttons spawn with love.load()

Post by lachlaan »

http://pastebin.com/3LuuJRMz

Could try something like that. As the title of the paste suggests, i bet i broke the syntax somewhere in there, but you get the general gist of where i was going with it :D Wether it's a good way to do it or not i'll leave up to the more experienced people around if they'd be so kind as to have a look at it :) Hope you can troubleshoot that idea into something functional.

Were the buttons meant to just tell the player what buttons to push? If you're actually meant to click them, you should also include width and height for checking if they're clicked, otherwise if they're just indicators as to which keys to press, then it's fine as it is i guess. All you'd need to do is also pass a function for what each button is supposed to do.
Post Reply

Who is online

Users browsing this forum: keharriso and 219 guests