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

Author Topic: if key pressed and released draw image doesnt work  (Read 2258 times)

0 Members and 2 Guests are viewing this topic.

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
if key pressed and released draw image doesnt work
« on: July 07, 2011, 03:57:10 pm »
hello everyone
i have an image and i want when i press key::M to draw the image but it isnt working

i tried this but the image disappear when releasing M key
Code: [Select]

if(Game.GetInput().IsKeyDown(sf::Key::M))
Game.Draw(Sprite);


i also tried this and it works perfectly but when i move the mouse  the image disappear
Code: [Select]

while(Game.IsOpened())
{
sf::Event Event;
while(Game.GetEvent(Event))
{
}

Game.Clear();
if(Event.Type == sf::Event::KeyReleased && Event.Key.Code == sf::Key::M)
Game.Draw(Sprite);
Game.Display();


i also tried this but it doesn't work at all
Code: [Select]

while(Game.IsOpened())
{
sf::Event Event;
while(Game.GetEvent(Event))
{
if(Event.Type == sf::Event::KeyReleased && Event.Key.Code == sf::Key::M)
Game.Draw(Sprite);
}

Game.Clear();
Game.Display();


please can someone help  :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
if key pressed and released draw image doesnt work
« Reply #1 on: July 07, 2011, 04:01:30 pm »
You want the image to stay after the key is released, so you can't simply rely on the "key pressed" state or event, you must set a permanent flag.

Code: [Select]
bool draw = false;
while(Game.IsOpened())
{
    sf::Event Event;
    while(Game.GetEvent(Event))
    {
        if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::M)
            draw = true;
    }
}

Game.Clear();
if (draw)
    Game.Draw(Sprite);
Game.Display();
Laurent Gomila - SFML developer

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
if key pressed and released draw image doesnt work
« Reply #2 on: July 07, 2011, 04:11:11 pm »
Quote from: "Laurent"
You want the image to stay after the key is released, so you can't simply rely on the "key pressed" state or event, you must set a permanent flag.

Code: [Select]
bool draw = false;
while(Game.IsOpened())
{
    sf::Event Event;
    while(Game.GetEvent(Event))
    {
        if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::M)
            draw = true;
    }
}

Game.Clear();
if (draw)
    Game.Draw(Sprite);
Game.Display();


omg thank you so much i have been thinking of a way for the past hour and a half.
thank you  :D  :D

 

anything