Hey, i am extremely new to programming in general (3 weeks) and after abit of research i chose .lua as my first language to learn so far i love LOVE and have spent every minute spare having a go at learning how to programme using it
From a mixture of you-tube tutorials and a fair bit of head scratching and figuring things out from what i had picked up i have managed to get together a very basic game concept not too far from a space invaders sort of genre.
The problem i have become stuck on is...
I have a "menu" gamestate from which you have two button options, play or quit.. both of these work respectively but i want to add a restart button, i gather that if i call love.load() it would reload everything as if i had just loaded up the game from a fresh?? but when i do this it seems to crash the whole game :/
Any light on how to code a restart button would be greatly appreciated (go easy on me though, i am new to this haha)
I hope i explained well enough too any more info needed just ask
thanks in advance,
ZoNe
How to make a restart button? :/
Re: How to make a restart button? :/
Hello! Welcome to LÖVE!ZoNe wrote:Hey, i am extremely new to programming in general (3 weeks) and after abit of research i chose .lua as my first language to learn so far i love LOVE and have spent every minute spare having a go at learning how to programme using it
I'll try!ZoNe wrote:Any light on how to code a restart button would be greatly appreciated (go easy on me though, i am new to this haha)
Okay, so here's how I see it:ZoNe wrote:I have a "menu" gamestate from which you have two button options, play or quit.. both of these work respectively but i want to add a restart button, i gather that if i call love.load() it would reload everything as if i had just loaded up the game from a fresh?? but when i do this it seems to crash the whole game :/
Make a start menu state, which resets all variables and stuff. This also brings up options, configurations, what ever.
Anyway, if the person clicks "Restart", just bring them to the start menu! That way, since you're already cleared all previous variables, you don't have to worry about anything like that!
Hope this helps, and welcome to LÖVE!
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
Re: How to make a restart button? :/
hi, and thank you
thanks for your response too
but it seams to crash the game when clicked :/
thanks for your input though much appreciated
thanks for your response too
im not sure how to do this though :/ i have a "menu" gamestate and a "playing gamestate already, if i press esc during gameplay it takes me from the "playing" state to the "menu" state and if i click my already made and working "play/continue" button then it carrys on the game as if it were paused.. thats fine but I'm just unsure how to code the "restart" button that i have created that is in the "menu" gamestate also so that instead of carrying on with the present game it starts a new one :/ i tried...davisdude wrote:Make a start menu state, which resets all variables and stuff. This also brings up options, configurations, what ever.
Anyway, if the person clicks "Restart", just bring them to the start menu! That way, since you're already cleared all previous variables, you don't have to worry about anything like that!
Code: Select all
function button.click(x,y)
for i,v in ipairs(button) do
if x > v.x and
x < v.x + button_font:getWidth(v.text) and
y > v.y and
y < v.y + button_font:getHeight() then
if v.id == "play" then
gamestate = "playing"
end
if v.id == "quit" then
love.quit()
end
if v.id == "restart" then
love.load()
gamestate = "playing"
end
end
end
end
thanks for your input though much appreciated
Re: How to make a restart button? :/
aha... did this...
and it works.. is that sort of what you meant?... i could make a separate function to hold it in then just call that function to keep things tidier in my script like..
function game.restart()
background.load()
player.load()
enemy.load()
end
then just call game.restart() instead?
only problem i have found with this is that any enemy that was left on the screen from the previous game are still there. i understand why but how do i remove them? they are inserted via table but obviously i don't want the command to interrupt the enemy being spawned for the new game?
Code: Select all
function button.click(x,y)
for i,v in ipairs(button) do
if x > v.x and
x < v.x + button_font:getWidth(v.text) and
y > v.y and
y < v.y + button_font:getHeight() then
if v.id == "play" then
gamestate = "playing"
end
if v.id == "quit" then
love.quit()
end
if v.id == "restart" then
background.load()
player.load()
enemy.load()
gamestate = "playing"
end
end
end
end
function game.restart()
background.load()
player.load()
enemy.load()
end
then just call game.restart() instead?
only problem i have found with this is that any enemy that was left on the screen from the previous game are still there. i understand why but how do i remove them? they are inserted via table but obviously i don't want the command to interrupt the enemy being spawned for the new game?
Re: How to make a restart button? :/
That's exactly what I meant!
For the enemies, just do
What I would recommend doing is setting a table where you store all the enemies you make. Then, reset that table.
For the enemies, just do
Code: Select all
EnemyTableName = {}
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
Re: How to make a restart button? :/
ahh cool thought you might of meant that after i figured it out
thanks
im not sure i know what you mean.. this is how i have spawned them...For the enemies, just do
What I would recommend doing is setting a table where you store all the enemies you make. Then, reset that table.Code: Select all
EnemyTableName = {}
Code: Select all
enemy = {}
function enemy.load()
screenWidth = love.graphics.getWidth()
enemy.width = 25
enemy.height = 25
enemy.friction = 10
enemy.speed = 50
enemy.xvel = 0
enemy.yvel = 0
enemy.timer = 0
enemy.timerLim = math.random(1,2)
enemy.spawnX = math.random(0, screenWidth - enemy.width)
enemyR = {}
enemyR[1] = love.graphics.newImage("images/enemy2.png")
enemyR[2] = love.graphics.newImage("images/enemy1.png")
enemy.animtimer = 0
enemy.pic = enemyR[1]
enemy.pnum = 1
end
--
function enemy.spawn(x,y)
table.insert(enemy, {x = x, y = y, xvel = enemy.xvel, yvel = enemy.yvel,width = enemy.width, height = enemy.height})
end
--
function enemy.spawningHandler(dt)
enemy.timer = enemy.timer + dt
if enemy.timer > enemy.timerLim then
enemy.spawn(enemy.spawnX, -enemy.height)
enemy.timer = 0
enemy.timerLim = math.random(1,2)
enemy.spawnX = math.random(0,screenWidth - enemy.width)
end
end
Re: How to make a restart button? :/
In the
Change "enemy" to something like "enemy.spawned", that way you don't reset all of your custom functions and stuff. It also makes it more organized.
Code: Select all
function enemy.spawn(x,y)
table.insert(enemy, {x = x, y = y, xvel = enemy.xvel, yvel = enemy.yvel,width = enemy.width, height = enemy.height})
end
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
Re: How to make a restart button? :/
okay...like this?...
I'm getting errors whichever way i try doing it :/ thanks
Code: Select all
enemy = {}
function enemy.load()
screenWidth = love.graphics.getWidth()
enemy.width = 25
enemy.height = 25
enemy.friction = 10
enemy.speed = 50
enemy.xvel = 0
enemy.yvel = 0
enemy.timer = 0
enemy.timerLim = math.random(1,2)
enemy.spawnX = math.random(0, screenWidth - enemy.width)
enemyR = {}
enemyR[1] = love.graphics.newImage("images/enemy2.png")
enemyR[2] = love.graphics.newImage("images/enemy1.png")
enemy.animtimer = 0
enemy.pic = enemyR[1]
enemy.pnum = 1
end
--
function enemy.spawn(x,y)
table.insert(enemy.spawning, {x = x, y = y, xvel = enemy.xvel, yvel = enemy.yvel,width = enemy.width, height = enemy.height})
end
--
function enemy.spawningHandler(dt)
enemy.timer = enemy.timer + dt
if enemy.timer > enemy.timerLim then
enemy.spawn(enemy.spawnX, -enemy.height)
enemy.timer = 0
enemy.timerLim = math.random(1,2)
enemy.spawnX = math.random(0,screenWidth - enemy.width)
end
end
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: How to make a restart button? :/
A line enemy.spawning = {} to enemy.load() (although using the past tense, like davisdude suggested, makes more sense here).
Help us help you: attach a .love.
Re: How to make a restart button? :/
sorry typo there.. meant "enemy.spawned" yeah, I've added...
but this disables the enemies spawning completely :/ I'm confused haha... i gather what I'm trying to do here is insert tables "enemy" into a separate table "enemy spawned" ?? i must be doing something wrong here...
Code: Select all
enemy = {}
function enemy.load()
enemy.spawned = {}
screenWidth = love.graphics.getWidth()
enemy.width = 25
enemy.height = 25
enemy.friction = 10
enemy.speed = 50
enemy.xvel = 0
enemy.yvel = 0
enemy.timer = 0
enemy.timerLim = math.random(1,2)
enemy.spawnX = math.random(0, screenWidth - enemy.width)
enemyR = {}
enemyR[1] = love.graphics.newImage("images/enemy1.png")
enemyR[2] = love.graphics.newImage("images/enemy2.png")
enemy.animtimer = 0
enemy.pic = enemyR[1]
enemy.pnum = 1
end
--
function enemy.spawn(x,y)
table.insert(enemy.spawned, {x = x, y = y, xvel = enemy.xvel, yvel = enemy.yvel,width = enemy.width, height = enemy.height})
end
Who is online
Users browsing this forum: miras and 9 guests