Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How can i create an effect only once when a button is held?  (Read 2257 times)

0 Members and 1 Guest are viewing this topic.

moonbeamer2234

  • Newbie
  • *
  • Posts: 10
    • View Profile
I dont know how to do it :[ I dont want the effect to continue if the button is held. its for the magic bar decreasing each time the Z key is Pressed. It draws a rectangle when the space key is pressed but it remains when it is held, so instead of just detucting the energy required from activating the rectangle drawn, its drains it according the amount of time(im assuming in miliseconds at the drain rate). So the energy deduction is unstable. how can i say syntactically that i want the rectangle to be drawn for the same amount of time when the key is held or just pressed once? I want them both to do the same thing basically

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How can i create an effect only once when a button is held?
« Reply #1 on: July 24, 2012, 03:27:50 am »
You sir must come from outerspace, what exactly are you talking about there with energy drains and unstable stuff? ??? ;D
(Don't expect people to know what you're working on. ;))

If you want to something to happen only once when you press a key you could do this with a boolean variable, e.g.:
bool action = false;
// ...
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z) && !action)
{
   action = true;
   // Do your stuff
}
else if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
   action = false;
}


If you want a rect to get drawn only when you press a key you could do this like:
bool action = false;
// ...
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
   action = true;
}
// ...
if(action)
{
   window.draw(rect);
   action = false;
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

moonbeamer2234

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: How can i create an effect only once when a button is held?
« Reply #2 on: July 24, 2012, 04:27:15 am »


here is the part that im having problems with along with its class.

void collision(sf::RectangleShape snakebodym, sf::RectangleShape &enemy, sf::RectangleShape &stat, int cost)
//Collision with a stat drain(health, energy, ect)
{

    if(snakebodym.getPosition().x + snakebodym.getSize().x < enemy.getPosition().x ||
           snakebodym.getPosition().x > enemy.getPosition().x + enemy.getSize().x ||
           snakebodym.getPosition().y + snakebodym.getSize().y < enemy.getPosition().y ||
           snakebodym.getPosition().y > enemy.getPosition().y + enemy.getSize().y)
{

}
else
{


    stat.setSize(sf::Vector2f(stat.getSize().x-cost, stat.getSize().y-0));

    }}
//Class
[code=cpp]
[/code =none]
Here is the actual event
[code=none]
[/code=cpp]
if((event.type == sf::Event::KeyPressed) && (event.type != sf::Event::KeyReleased) && (event.key.code == sf::Keyboard::Z))
  {
    //Sword Draws according to direction
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
    {snakebody.setPosition(sf::Vector2f(snakebody.getPosition().x+175, snakebody.getPosition().y+0));
    sword.setPosition(sf::Vector2f(snakebody.getPosition().x-175, snakebody.getPosition().y+4));}
   else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
    {snakebody.setPosition(sf::Vector2f(snakebody.getPosition().x-175, snakebody.getPosition().y+0));
    sword.setPosition(sf::Vector2f(snakebody.getPosition().x+0, snakebody.getPosition().y+4));}
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
    {snakebody.setPosition(sf::Vector2f(snakebody.getPosition().x+0, snakebody.getPosition().y-175));
    swordVerticle.setPosition(sf::Vector2f(snakebody.getPosition().x+4, snakebody.getPosition().y+0));}
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
    {snakebody.setPosition(sf::Vector2f(snakebody.getPosition().x+0, snakebody.getPosition().y+175));
    swordVerticle.setPosition(sf::Vector2f(snakebody.getPosition().x+4, snakebody.getPosition().y-175));
    }



  }
if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
    sword.setPosition(0, 0);
swordVerticle.setPosition(0,0);
}
//Not sure what to call this. Its right after the game loop where window.display, ect are. I think its class initialization maybe?
collision(sword,enemy,weaponhealth, 10);
[code=cpp]
     collision(swordVerticle, enemy, weaponhealth, 10);


[quote author=eXpl0it3r link=topic=8628.msg58125#msg58125 date=1343093270]
You sir must come from outerspace, what exactly are you talking about there with energy drains and unstable stuff? ??? ;D
(Don't expect people to know what you're working on. ;))

If you want to something to happen only once when you press a key you could do this with a boolean variable, e.g.:
[code=cpp]bool action = false;
// ...
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z) && !action)
{
   action = true;
   // Do your stuff
}
else if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
   action = false;
}


If you want a rect to get drawn only when you press a key you could do this like:
bool action = false;
// ...
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
   action = true;
}
// ...
if(action)
{
   window.draw(rect);
   action = false;
}
it drains weaponhealth instead of just taking 10 away. I dont want sword and swordverticle to remain when the button is pressed. I just want them to be displayed once no matter if the key is held or pressed once. and i just want weaponhealth to be deducted 10(according the the class) only once, regardless if the button is held or pressed.

moonbeamer2234

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: How can i create an effect only once when a button is held?
« Reply #3 on: August 05, 2012, 01:54:13 am »
I still cant get that code to work. I dont want the rectangle to be drawn and continue to be drawn while the key is held. I just want it to appear once and disapear even if the key is held

theodor

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: How can i create an effect only once when a button is held?
« Reply #4 on: August 07, 2012, 01:19:27 pm »
bool DoOnce = true;
if (DoOnce == true)
{
      DoIt();
      DoOnce = false;
}

It would probably disappear too quickly if I just clear the sceen there so..
if (ElapsedTime.asSeconds == 0.5f)
{
      clearscreen();
}

Something like that... I'm sure you are drawing other objects to the screen as well, in that case you need to figure out how to implement what I wrote above yourself.
« Last Edit: August 07, 2012, 01:22:59 pm by theodor »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How can i create an effect only once when a button is held?
« Reply #5 on: August 07, 2012, 01:54:42 pm »
Instead of a boolean, you should use a sf::Time object and a sf::Clock. Whenever you press the key you get the time from the clock and add a display time. Now as long as the sf::Time is greater than the clock time you draw it.

For the key detection you could use a boolean and only set the time when the boolean is false and the key is pressed and whenever the key is not pressed you set it to false.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/