Author Topic: creating a time delay before a code runs[SOLVED]  (Read 2321 times)

at0mix

  • Member
  • **
  • Posts: 17
  • Karma: +0/-0
    • View Profile
EDIT: Fixed, thanks everyone for their help
« Last Edit: Sun, Aug 21, 2011 by at0mix »

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: creating a time delay before a code runs.
« Reply #1 on: Fri, Aug 19, 2011 »
hi try this
Code: [Select]
if (FlxG.keys.justPressed("R"))
{
   ammodelay = 1;
}

if (ammodelay > 0)
{
  ammodelay -= FlxG.elapsed;
}
else if (ammodelay > -1)
{
  ammodelay = -1;
  fillammo();
}

or you can use boolean
Code: [Select]
if (FlxG.keys.justPressed("R"))
{
   ammodelay = 1;
   doneReload = false;
}

if (ammodelay > 0)
{
  ammodelay -= FlxG.elapsed;
}
else
{
  if (!doneReload)
  {
    doneReload = true;
    fillammo();
  }
}

at0mix

  • Member
  • **
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: creating a time delay before a code runs.
« Reply #2 on: Fri, Aug 19, 2011 »
see that's just it, I can't used elapsed because the code only runs for about .016 of a second. So when I do that, ammodelay will only get down to .984 before the code finishes. I need a way to make it last longer than just the split second it takes to press R.

osro

  • Active Member
  • ***
  • Posts: 121
  • Karma: +1/-0
    • View Profile
    • Purppura
Re: creating a time delay before a code runs.
« Reply #3 on: Fri, Aug 19, 2011 »
Have you tried FlxTimer? http://flixel.org/docs/org/flixel/FlxTimer.html

Code: [Select]
timer.start(1);
Code: [Select]
if(timer.finished)
{
    // give ammo code
}
« Last Edit: Fri, Aug 19, 2011 by osro »

at0mix

  • Member
  • **
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: creating a time delay before a code runs.
« Reply #4 on: Fri, Aug 19, 2011 »
Tried it, same issue, because the code runs for such a fraction of a second, the time never actually finishes.

Chelnok

  • Contributor
  • ****
  • Posts: 257
  • Karma: +0/-0
    • View Profile
Re: creating a time delay before a code runs.
« Reply #5 on: Fri, Aug 19, 2011 »
good question..

Quote
Oh right, my question is, what's the simplest way to go about making the code wait a second or two before it gives the ammo.

No answer for The Simplest way.. My simplest way to go about making the code wait a second would be be like:

if (myFlashOrFlixelTimer >  myHardCodedSeconds)
  then justDoIt

So..
if ReloadKey.pressed == true
 then wait for Timer
   if Timer == what i want
    then reload()
    ReloadKey.pressed = false

edit:
"Tried it, same issue, because the code runs for such a fraction of a second, the time never actually finishes"

btw.. you can use whatever timer.. flash timer, Flixel.ellapsed, getTimer.. your own time++ in update() ..server side..
« Last Edit: Fri, Aug 19, 2011 by Chelnok »

chiguire

  • Member
  • **
  • Posts: 26
  • Karma: +0/-0
    • View Profile
    • My Personal Site
Re: creating a time delay before a code runs.
« Reply #6 on: Fri, Aug 19, 2011 »
If you're using an animation to represent the weapon reload, you could use addAnimationCallback in FlxSprite to call the function to give the player ammo once the animation is over.

osro

  • Active Member
  • ***
  • Posts: 121
  • Karma: +1/-0
    • View Profile
    • Purppura
Re: creating a time delay before a code runs.
« Reply #7 on: Fri, Aug 19, 2011 »
Tried it, same issue, because the code runs for such a fraction of a second, the time never actually finishes.

Hmm... I don't get it why it won't work.

You start the timer when you you press the reload button.

And then you check on every update that if the timer has finished. And if it is... you add the ammos.
« Last Edit: Fri, Aug 19, 2011 by osro »

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: creating a time delay before a code runs.
« Reply #8 on: Fri, Aug 19, 2011 »
Have to agree with osro - the issue isn't the speed at which the function runs, it's the order in which your logic has been placed. Quite simply on the reload key press you start a timer, and only when it finishes do you issue the ammo.
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

arcade27

  • Member
  • **
  • Posts: 40
  • Karma: +0/-0
    • View Profile
Re: creating a time delay before a code runs.
« Reply #9 on: Fri, Aug 19, 2011 »
Havent used timers yet,  ive been using this from the dojo

http://flashgamedojo.com/wiki/index.php?title=Delayed_Actions_(Flixel)

Wich knowing my luck is actualy flxtimer XD

at0mix

  • Member
  • **
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: creating a time delay before a code runs.
« Reply #10 on: Sat, Aug 20, 2011 »
Hmm... I don't get it why it won't work.

You start the timer when you you press the reload button.

And then you check on every update that if the timer has finished. And if it is... you add the ammos.

Code: [Select]
public function reload():void
{
ammoDelay.start(1);

if (!ammoDelay.finished)
{
FlxG.log(ammoDelay.timeLeft);
}

if(ammoDelay.finished)
{
availAmmo -= reloadAmount;
curAmmo = reloadAmount;
}


}

every time I pres R, it logs 0.986. And I don't get the ammo, if I don't make it it's own function, and just put it in update(), same thing. Nothing seems to be working.

at0mix

  • Member
  • **
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: creating a time delay before a code runs.
« Reply #11 on: Sat, Aug 20, 2011 »
I got it to work.

Code: [Select]
public function reload():void
{



if (ammoDelay == -1)
{
reloading = false;
if(availAmmo - clipSize >= 0) {
var reloadAmount:int = clipSize;
if (availAmmo - reloadAmount < 0) {
reloadAmount += availAmmo - reloadAmount;
}

availAmmo -= reloadAmount;
curAmmo = reloadAmount;
ammoDelay = 1;


}
}
}

I added

Code: [Select]
ammoDelay -= FlxG.elapsed;
if (ammoDelay < -1)
{
ammoDelay = -1;
}

to update()

osro

  • Active Member
  • ***
  • Posts: 121
  • Karma: +1/-0
    • View Profile
    • Purppura
Re: creating a time delay before a code runs.
« Reply #12 on: Sun, Aug 21, 2011 »

every time I pres R, it logs 0.986. And I don't get the ammo, if I don't make it it's own function, and just put it in update(), same thing. Nothing seems to be working.

You are starting the timer just before you check if it's finished. That's why you get the "0,986" numbers.

What you should do, is something like this:

Code: [Select]
public function reload():void
{
ammoDelay.start(1);
}

override public function update():void
{
if (!ammoDelay.finished)
{
FlxG.log(ammoDelay.timeLeft);
}

if(ammoDelay.finished)
{
availAmmo -= reloadAmount;
curAmmo = reloadAmount;
}

if (FlxG.keys.justPressed("R") && !ammoDelay.finished)
{
reload();
}
}

That doesn't probably work straight, but it should give you the idea how you manage the timer logic.
« Last Edit: Sun, Aug 21, 2011 by osro »