Floating

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.
JRascall
Prole
Posts: 8
Joined: Fri Dec 03, 2010 9:12 pm

Floating

Post by JRascall »

Ok so coming from a c# background from college, I've tried LUA with love2d and so far successful results.

But I've been adding all my code to main.lua and running that currently heres my code

Code: Select all

-- Rascalls Game Source Code - Main.lua --
		
		function love.draw()	
		
		Title()
		MenuItems()
		
		end

		function love.update()

		love.graphics.setBackgroundColor(math.random(255),math.random(255),200)
			
		end
	
		function Title()
		
		TitleFont = love.graphics.newFont("WTD.ttf", 64)
		love.graphics.setFont(TitleFont)
		TitleText = love.graphics.print("Rascalls Game", love.graphics.getWidth() / 2 - 205, 80)
				
		end
		
		function MenuItems()		
		
		MenuFont = love.graphics.newFont("WTD.ttf", 32)
		love.graphics.setFont(MenuFont)
		love.graphics.print("Single Player", love.graphics.getWidth() / 2 - 205, 120) 
		
		end
I want to make a floating menu so the text will move as if its floating on the canvas, Can any body lend a hand and teach me how I would go about this with LUA.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Floating

Post by Ryne »

JRascall wrote:Ok so coming from a c# background from college, I've tried LUA with love2d and so far successful results.

But I've been adding all my code to main.lua and running that currently heres my code

Code: Select all

-- Rascalls Game Source Code - Main.lua --
		
		function love.draw()	
		
		Title()
		MenuItems()
		
		end

		function love.update()

		love.graphics.setBackgroundColor(math.random(255),math.random(255),200)
			
		end
	
		function Title()
		
		TitleFont = love.graphics.newFont("WTD.ttf", 64)
		love.graphics.setFont(TitleFont)
		TitleText = love.graphics.print("Rascalls Game", love.graphics.getWidth() / 2 - 205, 80)
				
		end
		
		function MenuItems()		
		
		MenuFont = love.graphics.newFont("WTD.ttf", 32)
		love.graphics.setFont(MenuFont)
		love.graphics.print("Single Player", love.graphics.getWidth() / 2 - 205, 120) 
		
		end
I want to make a floating menu so the text will move as if its floating on the canvas, Can any body lend a hand and teach me how I would go about this with LUA.
Basically you are saying you have windows, with text, like a title, that you want to move with the windows? If thats the case then just set your "Menu Items/ Menu Text" x/y variables to be whatever your window x/y variables are. Then just add or subtract what's needed to get the text in the right place.
@rynesaur
JRascall
Prole
Posts: 8
Joined: Fri Dec 03, 2010 9:12 pm

Re: Floating

Post by JRascall »

Oh sorry, No I want so the text items like moves alittle bit so its say unstable 'floating' if you get me?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Floating

Post by Robin »

JRascall wrote:Oh sorry, No I want so the text items like moves alittle bit so its say unstable 'floating' if you get me?
What Ryne suggested will still work, though.

If I may, your code contains several problems, I'll highlight them (apart from the fact that the code seems to be unindented):

Code: Select all

function love.update() -- this function can take an argument (dt), which you might find useful
	love.graphics.setBackgroundColor(math.random(255),math.random(255),200) -- this could be a problem for epileptic users
end
	
function Title()
	TitleFont = love.graphics.newFont("WTD.ttf", 64) -- creating a font every frame is extremely bad -- as if firefox would need to download the web page you're looking 60 times a second
	love.graphics.setFont(TitleFont)
	TitleText = love.graphics.print("Rascalls Game", love.graphics.getWidth() / 2 - 205, 80) -- love.graphics.print doesn't actually return anything, there's no need to do TitleText = nil.
end
		
function MenuItems()		
	MenuFont = love.graphics.newFont("WTD.ttf", 32) -- same as above
	love.graphics.setFont(MenuFont)
	love.graphics.print("Single Player", love.graphics.getWidth() / 2 - 205, 120) -- strangely, you do it right this time
end
Help us help you: attach a .love.
JRascall
Prole
Posts: 8
Joined: Fri Dec 03, 2010 9:12 pm

Re: Floating

Post by JRascall »

I only made a variable for TextTitle because I tried to use it like a instance.. and the random colours is for me because Its only a example..

also " TitleFont = love.graphics.newFont("WTD.ttf", 64) -- creating a font every frame is extremely bad -- as if firefox would need to download the web page you're looking 60 times a second" Its in the draw function? I never knew the draw function drew every second :| I only thought update function works like that.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Floating

Post by Robin »

JRascall wrote:I never knew the draw function drew every second :| I only thought update function works like that.
Nope. If it drew once, it would be more of a picture than a game, wouldn't it?

See http://love2d.org/wiki/love.run for the default game loop. (Yes, you can change it, but you'll rarely ever have to do that, unless your name is Jasoco.)
Help us help you: attach a .love.
JRascall
Prole
Posts: 8
Joined: Fri Dec 03, 2010 9:12 pm

Re: Floating

Post by JRascall »

Sorry, but am coming from a (limited) C# and XNA background and never used a draw function.
Ok well Ill work on it follow some tutorials or somin thing, any way thanks.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Floating

Post by Ryne »

JRascall wrote:Sorry, but am coming from a (limited) C# and XNA background and never used a draw function.
Ok well Ill work on it follow some tutorials or somin thing, any way thanks.
It's really quite basic. Bascially, any LOVE game will include love.load, love.update, and load.draw. All of which are pretty self explanatory.

Load is for anything you want loaded on startup, like images and sounds. Though images or any graphics related things arent actually drawn until you add them to love.draw. So a basic way to load an image and display it is this.

Code: Select all

function love.load()

    image = love.graphics.newImage("imagelocation")

end

function love.update(dt)

end

function love.draw()

    love.graphics.draw(image, 100, 100)

end
So in the end, if you want something to appear on screen, you will have to use draw.
@rynesaur
JRascall
Prole
Posts: 8
Joined: Fri Dec 03, 2010 9:12 pm

Re: Floating

Post by JRascall »

I get that but is text only static? or does text have to be images for em to move?
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Floating

Post by Ryne »

JRascall wrote:I get that but is text only static? or does text have to be images for em to move?
Text is text, if you would like to move text you could just do it the same way you would move an image like so.

Code: Select all

function love.load()

text = {}  -- here we create a table called "text"
text.words = "hello world!" -- here we add a string entry to that table, with the string "hello World"
text.x = 100                     -- an x variable for text
text.y = 100                     -- a y variable for text

end

function love.update(dt)

    if love.keyboard.isDown("p") then  -- this will take text.x and add "1" to it if the "p" button is pressed
   text.x = text.x + 1 * dt
   end

end

function love.draw()

   love.graphics.print(text.words, text.x, text.y) -- this prints the text at "text.x" and "text.y"

end
Thats basic code that will take the current "X" variable and add 1 to it. Since the text is being drawn at "text.x" and "text.y" the text will move whenever you change those variables. Hope this helps.

Sorry the code is sloppy, I wrote it in the browser so I had no access to "tab". Thus it was difficult to indent.

Ryne.
@rynesaur
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 207 guests