Page 1 of 2
Floating
Posted: Fri Dec 03, 2010 9:33 pm
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.
Re: Floating
Posted: Fri Dec 03, 2010 9:37 pm
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.
Re: Floating
Posted: Fri Dec 03, 2010 10:14 pm
by JRascall
Oh sorry, No I want so the text items like moves alittle bit so its say unstable 'floating' if you get me?
Re: Floating
Posted: Fri Dec 03, 2010 10:43 pm
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
Re: Floating
Posted: Fri Dec 03, 2010 10:47 pm
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.
Re: Floating
Posted: Fri Dec 03, 2010 11:11 pm
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.)
Re: Floating
Posted: Fri Dec 03, 2010 11:15 pm
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.
Re: Floating
Posted: Fri Dec 03, 2010 11:46 pm
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.
Re: Floating
Posted: Fri Dec 03, 2010 11:52 pm
by JRascall
I get that but is text only static? or does text have to be images for em to move?
Re: Floating
Posted: Sat Dec 04, 2010 12:47 am
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.