How to Control the "Button is pressed" ?

Hola como estas?
“Except you give me something i can feeeeeeel”
ok sorry for that , anyway haha :

I have a little problem… in game update i have this :

http://img543.imageshack.us/img543/7962/missileshottoofastd.jpg

If i press backspace just one time , it shoots 3 missiles in one time… How can i control that? i want a delay… i tried to make some System.currentmilis etc but, well, i couldnt make it work.

I was looking for some gdx method to tell me when the button is released but couldnt find it.

How can i “fix” that ?

:smiley:

  1. you should post in newbie, debugging, questions section

  2. don’t give us screen shot of your code, because it’s not clear and in case we want to test it, no one will re-write it, plus your screen shot is completely useless it has ZERO information on it .

now for your question, this is how it works to make a “delay”

1)create a loading variable (integer)
2)create a counter variable (integer)
3)when you make a shot, the counter variable increase, in the current time, you disable shooting until the counter is equal to the loading variable
4)once they are equal, you make the counter equal to zero again

in case you want a working example, check the player class in this tutorial i wrote about moving and shooting bullets, it’s in Java2D but the principles are the same

To reiterate what alaslip said, here’s some pseudo code:


int counter = 0
int startFiring = 5
update {
   if Key_Pressed {
      if counter = 0 {
         fire missile
      }   
      else {
         counter + 1
      }
   }
   if counter > startFiring {
      counter = 0
   }
}

Correct me if I’m wrong, but in that way /\ the counter is gonna update only when the key is pressed.
Shouldn’t the counter update even if the key is down? something like


counter = counter > 0 ? counter - 1 : counter;
if (counter == 0 && keydown){
fireMissile();
counter = 5;
}

  1. Dont need to be mean

  2. if everyone here were Pro’s , we wouldnt need a forum.

  3. ZERO information? Well, you need learn to read topics and interpret and be a little more nice to newcomers.

Thanks for the help.

@Andre Lopes:

Don’t worry, quite a few new people become like that on the forum. It either wears off after a few weeks/months or they just disappear.

Im sorry i got mad, its just that, i came so peacefully and with happiness to this forum… And now to be trolled seems unfair.

Anyway, i will do this counter strategy tommorow morning, i think it will work perfectly :stuck_out_tongue:

I wasn’t referring to you.

I too am annoyed at people who respond like that, even when it’s not my question.

See, it pisses me off when I see noobs acting all high and mighty to other newbies. If Riven/princec/other pros don’t do it there’s no reason for you to.

While alasipknot is correct, he could’ve been a little nicer. New people don’t know any better so it is good to inform them, but in a nicer way. “Hey, welcome to JGO! answer question and for future reference posts like this go in insert board here” or something like that.
EDIT: Also, take a page out of Cas’ book and use a lot of smileys ;D they always help

On topic:
Another approach would be to have a set fire rate and simply check the time since you’ve last fired. The fire rate would be some amount of time, and you would need to initialize it somewhere. Then simply store a lastFire for the last time you fired. For example:


// I suggest using System.nanoTime() instead of System.currentTimeMillis() as the millis timer resolution isn't very good

private fireRate = 1000000000; // This is 1 second in nanoseconds
private lastFire = System.nanoTime() - fireRate;

public void fire() {
   if(System.nanoTime() - lastFire >= fireRate) {
      // fire code
      lastFire = System.nanoTime();
   }
}

i didn’t mean any harm at all :-\
plus i don’t think that i said something wrong … and i also answered his question
so why so serious guys ??

@Andre Lopes
sorry if i offended you

Yeah, dunnno why Andre reacted the way he did, I felt alaslipknot didn’t say or mean anything negative.

alaslipknot’s answer was not all rainbows and unicorns, but very informative and helpful.

Just that post alone means nothing.

But when put in the context of some of his previous posts, you see some of the tone behind it, and the tone isn’t very nice.

Yo Andre, dont listen to them.
You could do something with a delay and stuff. But let me first introduce to InputAdapter

Now Gdx.input.isKeyPressed tells you if the key is down
The InputAdapter das a keydown and keyup method, which are only invokes once. Once you press and once you let go, respectively.

You write a small class extending InputAdapter or implementing InputProcessor, I would recommend at the end of you game class, so that you have access to game things.

and in you create() method you have to call Gdx.input.setInputProcessor(INSTANCE OF YOUR INPUTADAPTER CLASS)

if you have any follow-up questions, dont hesitate.

Guys, thanks :stuck_out_tongue:
It worked perfectly, i did the counter strategy, i will try the input processor later.

Thanks!!! i will make another topic in newbies because im having problems finding rectangle intersection collision method oin libgdx.