Page 1 of 2
How to make a restart button? :/
Posted: Fri Feb 07, 2014 12:20 am
by ZoNe
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
Re: How to make a restart button? :/
Posted: Fri Feb 07, 2014 1:36 am
by davisdude
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
Hello! Welcome to LÖVE!
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)
I'll try!
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 :/
Okay, so here's how I see it:
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!
Re: How to make a restart button? :/
Posted: Fri Feb 07, 2014 8:15 pm
by ZoNe
hi, and thank you
thanks for your response too
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!
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...
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
but it seams to crash the game when clicked :/
thanks for your input though much appreciated
Re: How to make a restart button? :/
Posted: Fri Feb 07, 2014 8:39 pm
by ZoNe
aha... did this...
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
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?
Re: How to make a restart button? :/
Posted: Fri Feb 07, 2014 9:36 pm
by davisdude
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.
Re: How to make a restart button? :/
Posted: Sat Feb 08, 2014 12:26 am
by ZoNe
ahh cool thought you might of meant that after i figured it out
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.
im not sure i know what you mean.. this is how i have spawned them...
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
thanks
Re: How to make a restart button? :/
Posted: Sat Feb 08, 2014 2:14 am
by davisdude
In the
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
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.
Re: How to make a restart button? :/
Posted: Sat Feb 08, 2014 8:18 pm
by ZoNe
okay...like this?...
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
I'm getting errors whichever way i try doing it :/ thanks
Re: How to make a restart button? :/
Posted: Sat Feb 08, 2014 9:00 pm
by Robin
A line enemy.spawning = {} to enemy.load() (although using the past tense, like davisdude suggested, makes more sense here).
Re: How to make a restart button? :/
Posted: Sun Feb 09, 2014 10:08 pm
by ZoNe
sorry typo there.. meant "enemy.spawned"
yeah, I've added...
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
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...