Local sleep?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
DiabetiMark
Prole
Posts: 3
Joined: Wed Mar 05, 2014 10:24 pm

Local sleep?

Post by DiabetiMark »

Hello!, My name's Mark,

I'm pretty new to LUA coding.
And just have started a project called: Neon Fleet!
And as many others may have experienced, I ran into an obstacle...

My problem is:
I'm trying to make a scoreboard, but when W is pressed, it just keeps spamming the "score = score + 1" instead of just adding +1 every hit and release. So I guess the sleep is not even inplace, but I couldn't think of any other solution.


Conclusion:
I need a (shorter) code when a key is pressed, you have to wait 5 seconds before re-pressing it. (Repressing = not holding the button, but actually you need to re-press it after the 5 sec.)
this is the code I tried:

Code: Select all

if love.keyboard.isDown("w") then 
	score = score + 1
	scoreToggle = 1
end
--Need a way to make the sleep only local, to execute the score toggle.
if scoreToggle == 1 then
	love.timer.sleep ( 50*(dt)) 
	print ("Hay!")
	scoreToggle = 0
end
It's about the part that the scoreToggle needs an delay, but instead of the lines freezing/sleeping,
the whole game stops for a sec...

I hope someone can help me with my code,
or even knows a better and quicker way of doing it.
and already thanks for al yer support guys ^^



I hope my question has been clear.
Greetings,

DiabetiMark ;p
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Local sleep?

Post by micha »

Hi Mark. Welcome to our forum.

There are two ways to handle keyboard input. 1) at any moment you can check if a given key is down. This is what you already use. 2) The keypressed callbacks are called, everytime a key is pressed (= changes from up to down). It looks like you need the second method here. Have a look at the wiki.

The code would look like this:

Code: Select all

function love.keypressed(key)
  if key == 'w' then
    score = score + 1
  end
end
And keep in mind that you should never make the game actually sleep. It would freeze then and become unresponsive. Instead the game loop should always continue, only the update should do nothing during a delay.
DiabetiMark
Prole
Posts: 3
Joined: Wed Mar 05, 2014 10:24 pm

Re: Local sleep?

Post by DiabetiMark »

Thanks micha!
I'll try it out as soon as I'm
Home, what a quick response :D

There is still one question for me to ask,
Ill ask it at home.. Don't have the time for it now. :o:

DiabetiMark
DiabetiMark
Prole
Posts: 3
Joined: Wed Mar 05, 2014 10:24 pm

Re: Local sleep?

Post by DiabetiMark »

Thanks very much!
it worked :)

the key isn't spamming and the score is being counted probably!

now there lasts a last question.

I'm trying to make a ship going up and down, and firing bullets.
but I don't know how to form this code.

cause when I take the x coord = Height, when the bullet (whichs also uses Height) the ship and the bullet will be move up and down.
is there a code to create an instance (as I'm used in GameMaker)? So that there will be a code like bullet01 then bullet2 etc... and that it will get the height of being fired, and not the same as the ships height... it's really confusing me.
And I couldn't find any clue on the forum, or on the wiki.

and I have all files in the Main.lua and don't know how the require function works.

Thanks for all support! (again ) ;p!

DiabetiMark
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Local sleep?

Post by micha »

In the future, if you have unrelated questions, then don't hesitate to open a new thread. It is not a bad thing to have many short threads. That way the thread titles are more informative.

Now for your question: I cannot answer that very specifically, because I cannot see your code. If you want specific feedback, please upload a .love file of what you already have.

LÖVE does not provide any prefabricated OOP-methods. That means you have to organize your bullets/enemies/players yourself. Usually you would store all bullets in one table, for example like this:

Code: Select all

bullets = {
  {x=100,y=100,vx = 50,vy=20,height = 10,width=10},
  {x=100,y=120,vx = 50,vy=-20,height = 10,width=10},
  {x=100,y=100,vx = 50,vy=70,height = 10,width=10}
}
Each bullet is a table and "bullets" is a table, containing all these tables. There are many other ways of implementing multiple objects. I suggest you read a bit about Lua, for example in PiL.

Requiring: If you want to require the file "bullet.lua" you do (for example)

Code: Select all

require 'bullet'
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests