简体   繁体   中英

Trying to Reset Game in Lua

Very new to game development and Lua in general here. I'm making a platformer, and I want to be able to restart the game once a character collides with a certain object, and show a title before that. I also want to reset the character's position back to 0,0 once that happens, but I don't know how.

I made a global variable called WIN that's set to true if the character collides with the object, which works, but then going into my love.draw() function, I have this:

function love.draw()

-- begin virtual resolution drawing
push:apply('start')

-- clear screen using Mario background blue
love.graphics.clear(108/255, 140/255, 255/255, 255/255)

-- renders our map object onto the screen
love.graphics.translate(math.floor(-map.camX + 0.5), math.floor(-map.camY + 0.5))
map:render()

if WIN == true then
    love.graphics.printf('NEXT LEVEL', 0, 30, VIRTUAL_WIDTH, 'center')
    love.graphics.printf('Continue to Next Level', 0, 45, VIRTUAL_WIDTH, 'center')
    love.load()
end
-- end virtual resolution
push:apply('end')

end When I actually collide with the object, I get the following error:

Error
push.lua:48: love.window.setMode cannot be called while a Canvas is active in love.graphics.
Traceback
[C]: in function 'windowUpdateMode'
push.lua:48: in function 'setupScreen'
main.lua:43: in function 'load'
main.lua:116: in function 'draw'
[C]: in function 'xpcall'

The error lines are in my love.load() function, which is as follows:

function love.load()
    -- sets up a different, better-looking retro font as our default
    love.graphics.setFont(love.graphics.newFont('fonts/font.ttf', 8))

-- sets up virtual screen resolution for an authentic retro feel
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
    fullscreen = false,
    resizable = true
})

love.window.setTitle('Super Mario 50')

love.keyboard.keysPressed = {}
love.keyboard.keysReleased = {}
end

I'm guessing the issue is that I can't set up screen again after already doing that once, but I don't know how to fix this and create a fresh start. Any help is appreciated!

love.load() runs once. That one time is when your game starts up. Don't call love.load() (or love.update() ) within love.draw() or vice versa. The Love2D game engine calls these functions, so you don't call them.

Update the start position within love.update() not love.draw() . love.draw() is only for drawing stuff. I believe it would be the View in a model-view-controller architecture.

To restart the position of your object, you would have to do something like

if WIN == true then
    push.x = push.start.x
    push.y = push.start.y
    map.camX = map.start.camX
    map.camY = map.start.camY
end

Or something along those lines, it's hard to know without seeing your variables. Your love.draw() will then draw the things at the coordinates that they are at.

To reset/restart the Game without reloading the executable use:

love.event.quit('restart')

But before doing this make a dark screen before with red dropping letters: GAME OVER

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM