Page 2 of 3

Re: How do I remove drawn images & printed text?

Posted: Sat Mar 14, 2015 1:35 pm
by Kingdaro
RBXcpmoderator12345 wrote:can i have a function in a if statement in a function then?
like this:

Code: Select all

function love.keypressed(key) 
if key == "q" then
function love.draw()
love.graphics.draw(image)
end
end
end
You can, but I'm 99% sure that's not what you want at all. As far as I can tell, you're going about this the wrong way. This isn't how LOVE (and a bunch of other frameworks out there) work.

Look at love.run. This is the function that's actually behind every LOVE game. You don't have to understand what it does exactly, but it should give somewhat of a gist of what's actually going on.

Basically, video games work in frames. Every frame, the game updates itself, including all of its variables and such then draws elements on screen, such as images, text, shapes, and so on, according to these variables.

To put it in perspective, let's say you want a character to move across the screen. Every update, the game will ask: "is the user holding down the right arrow key?" If so, the game will then say "let's change the player's X variable by a certain amount." If not, the game will say "alright, so they aren't holding down the key, so we'll just do nothing there." Of course, the same will be done for every other arrow key, and subsequently for jumping, shooting guns, and so on.

Now, for your example, when love.draw is called, it will draw the image on screen every frame.

Code: Select all

function love.draw()
   love.graphics.draw(image)
end
So if we remove love.graphics.draw, the image doesn't show up. We just get a black screen. This you're probably already aware of. You can abuse this, however, using an if statement, which is where the magic comes in.

Code: Select all

function love.load()
   showImage = true
end

function love.draw()
   if showImage then
      love.graphics.draw(image)
   end
end
Since showImage is true, the image will show. If it was false, the image would not show. So then, what you'll want to do, is add a control to toggle this variable, so you can control when the image shows up.

Code: Select all

function love.load()
   showImage = true
end

function love.keypressed(key)
   if key == 'q' then
      showImage = not showImage -- toggles the showImage variable
   end
end

function love.draw()
   if showImage then
      love.graphics.draw(image)
   end
end
And here, by pressing Q, you can turn the image on and off. Feel free to ask any questions, hope I helped.

Re: How do I remove drawn images & printed text?

Posted: Sun Mar 15, 2015 3:53 am
by RBXcpmoderator12345
Because I want it so when you press Q, it draws the new map.

Re: How do I remove drawn images & printed text?

Posted: Sun Mar 15, 2015 9:36 am
by s-ol
RBXcpmoderator12345 wrote:Because I want it so when you press Q, it draws the new map.
Still, there's no reason for a function in a function for that behavior.

Re: How do I remove drawn images & printed text?

Posted: Sun Mar 15, 2015 10:41 pm
by RBXcpmoderator12345
thanks
i didn't realize I could use a boolean for switching images

Re: How do I remove drawn images & printed text?

Posted: Sun Mar 15, 2015 11:01 pm
by RBXcpmoderator12345
Updated code:

Code: Select all

--rename to main.lua
-- character is DrForce
function wait(time) --  so I do less typing and i feel familiar with it
love.timer.sleep(time)
end

function love.load() -- LOAD ALL THE IMAGES AND SOUNDS
titlescreen = love.graphics.newImage("titlescreen.png")
background = love.graphics.newImage("background.png")
Master_Illumina = love.graphics.newImage("Master Illumina (illuminabreaks).png")
fanzo = love.graphics.newImage("fanzo (guestslovepizza).png")
narbthenoob = love.graphics.newImage("narbthenoob (luckyleo1231).png")
newbag = love.graphics.newImage("newbag (damanius).png")
wires = love.graphics.newImage("wires.exe (vengancevinny).png")
mask = love.graphics.newImage("mask (lolhatz).png")
wizgo = love.graphics.newImage("wizgo (creepywriter).png")
character = love.graphics.newImage("gfx/character.png")
ghost = love.graphics.newImage("ghost.mp4 (successfulfundude).png")
plush1 = love.graphics.newImage("plushie (blockopsmaster).png")
plush2 = love.graphics.newImage("plushie2(Robloxia38).png")
theme = love.audio.newSource("werenotalone(spookysoundtrack.com).mp3")
scream = love.audio.newSource("scaryscream(soundbible.com).mp3")
kaito4life = love.graphics.newImage("Kaito4life(collestgirl).png")
playing = false
jumpscare = false
end

function love.keypressed(key)
if key == "q" then
playing = true
end
end

function love.draw()
love.graphics.draw(titlescreen)
love.audio.play(theme)
end

function love.draw()
if playing == true then
love.graphics.draw(background)
love.audio.stop(theme)
end
end








It shows nothing at first until I press Q.

Re: How do I remove drawn images & printed text?

Posted: Mon Mar 16, 2015 12:07 am
by arampl
1. Don't put love.audio.play(theme) in love.draw(). Put it at the end of love.load().
2. function love.draw() must be only one.

Code: Select all

--rename to main.lua
-- character is DrForce
function wait(time) --  so I do less typing and i feel familiar with it
   love.timer.sleep(time)
end

function love.load() -- LOAD ALL THE IMAGES AND SOUNDS
   titlescreen = love.graphics.newImage("titlescreen.png")
   background = love.graphics.newImage("background.png")
   Master_Illumina = love.graphics.newImage("Master Illumina (illuminabreaks).png")
   fanzo = love.graphics.newImage("fanzo (guestslovepizza).png")
   narbthenoob = love.graphics.newImage("narbthenoob (luckyleo1231).png")
   newbag = love.graphics.newImage("newbag (damanius).png")
   wires = love.graphics.newImage("wires.exe (vengancevinny).png")
   mask = love.graphics.newImage("mask (lolhatz).png")
   wizgo = love.graphics.newImage("wizgo (creepywriter).png")
   character = love.graphics.newImage("gfx/character.png")
   ghost = love.graphics.newImage("ghost.mp4 (successfulfundude).png")
   plush1 = love.graphics.newImage("plushie (blockopsmaster).png")
   plush2 = love.graphics.newImage("plushie2(Robloxia38).png")

   theme = love.audio.newSource("werenotalone(spookysoundtrack.com).mp3")
   scream = love.audio.newSource("scaryscream(soundbible.com).mp3")
   kaito4life = love.graphics.newImage("Kaito4life(collestgirl).png")

   playing = false
   jumpscare = false

   love.audio.play(theme)
end

function love.keypressed(key)
   if key == "q" then
      playing = true
      love.audio.stop(theme)
   end
end

function love.draw()
   if playing then --same as "if playing == true then" ("not playing" means "playing == false")
      love.graphics.draw(background)
   else
      love.graphics.draw(titlescreen)
   end
end

different approach:

Code: Select all

--rename to main.lua
-- character is DrForce
function wait(time) --  so I do less typing and i feel familiar with it
   love.timer.sleep(time)
end

function love.load() -- LOAD ALL THE IMAGES AND SOUNDS
   titlescreen = love.graphics.newImage("titlescreen.png")
   background = love.graphics.newImage("background.png")
   Master_Illumina = love.graphics.newImage("Master Illumina (illuminabreaks).png")
   fanzo = love.graphics.newImage("fanzo (guestslovepizza).png")
   narbthenoob = love.graphics.newImage("narbthenoob (luckyleo1231).png")
   newbag = love.graphics.newImage("newbag (damanius).png")
   wires = love.graphics.newImage("wires.exe (vengancevinny).png")
   mask = love.graphics.newImage("mask (lolhatz).png")
   wizgo = love.graphics.newImage("wizgo (creepywriter).png")
   character = love.graphics.newImage("gfx/character.png")
   ghost = love.graphics.newImage("ghost.mp4 (successfulfundude).png")
   plush1 = love.graphics.newImage("plushie (blockopsmaster).png")
   plush2 = love.graphics.newImage("plushie2(Robloxia38).png")

   theme = love.audio.newSource("werenotalone(spookysoundtrack.com).mp3")
   scream = love.audio.newSource("scaryscream(soundbible.com).mp3")
   kaito4life = love.graphics.newImage("Kaito4life(collestgirl).png")

   playing = false
   jumpscare = false

   currentBackground = titlescreen

   love.audio.play(theme)
end

function love.keypressed(key)
   if key == "q" then
      currentBackground = background
      love.audio.stop(theme)
   end
end

function love.draw()
   love.graphics.draw(currentBackground)
end

third variant:

Code: Select all

--rename to main.lua
-- character is DrForce
function wait(time) --  so I do less typing and i feel familiar with it
   love.timer.sleep(time)
end

function love.load() -- LOAD ALL THE IMAGES AND SOUNDS
   titlescreen = love.graphics.newImage("titlescreen.png")
   background = love.graphics.newImage("background.png")
   Master_Illumina = love.graphics.newImage("Master Illumina (illuminabreaks).png")
   fanzo = love.graphics.newImage("fanzo (guestslovepizza).png")
   narbthenoob = love.graphics.newImage("narbthenoob (luckyleo1231).png")
   newbag = love.graphics.newImage("newbag (damanius).png")
   wires = love.graphics.newImage("wires.exe (vengancevinny).png")
   mask = love.graphics.newImage("mask (lolhatz).png")
   wizgo = love.graphics.newImage("wizgo (creepywriter).png")
   character = love.graphics.newImage("gfx/character.png")
   ghost = love.graphics.newImage("ghost.mp4 (successfulfundude).png")
   plush1 = love.graphics.newImage("plushie (blockopsmaster).png")
   plush2 = love.graphics.newImage("plushie2(Robloxia38).png")

   theme = love.audio.newSource("werenotalone(spookysoundtrack.com).mp3")
   scream = love.audio.newSource("scaryscream(soundbible.com).mp3")
   kaito4life = love.graphics.newImage("Kaito4life(collestgirl).png")

   playing = false
   jumpscare = false

   love.draw = draw1

   love.audio.play(theme)
end

function love.keypressed(key)
   if key == "q" then
      love.draw = draw2
      love.audio.stop(theme)
   end
end

function draw1()
   love.graphics.draw(titlescreen)
end

function draw2()
   love.graphics.draw(background)
end


Re: How do I remove drawn images & printed text?

Posted: Mon Mar 16, 2015 9:36 am
by DaedalusYoung
arampl wrote:

Code: Select all

function wait(time)
   love.timer.sleep(time)
end
Easier:

Code: Select all

local wait = love.timer.sleep
But don't use that function, it doesn't do what you think it does.

Re: How do I remove drawn images & printed text?

Posted: Wed Mar 18, 2015 12:18 am
by RBXcpmoderator12345
"

Code: Select all

love.timer.sleep( s )
Arguments:

number s
Seconds to sleep for.
Returns:

Nothing."
Apparently love.timer.sleep is the equivalent of wait(seconds) in real Lua.

Re: How do I remove drawn images & printed text?

Posted: Wed Mar 18, 2015 2:00 am
by Kingdaro
RBXcpmoderator12345 wrote: Apparently love.timer.sleep is the equivalent of wait(seconds) in real Lua.
There is no wait() in "real Lua", that's only in ROBLOX Lua. The only actual equivalent is sleep() from luasocket, but that's a library, so it doesn't count.

That aside, he's correct, 99% of the time, you do not want to use this function for timed actions. The reason being that it stalls the game for that amount of time, disallowing any updating or drawing while it's in effect. Consider using a timer library instead, such as hump's timer or cron.

Re: How do I remove drawn images & printed text?

Posted: Wed Mar 18, 2015 4:27 am
by RBXcpmoderator12345
ohhh...
thats why i thought wait() was real
it wasn't, i was just fresh out of roblox
but there is os.sleep though