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

Author Topic: [SOLVED]Problem's making a sprite "flash" a color  (Read 5207 times)

0 Members and 1 Guest are viewing this topic.

anrdyshmrdy

  • Newbie
  • *
  • Posts: 5
    • View Profile
[SOLVED]Problem's making a sprite "flash" a color
« on: February 21, 2016, 06:07:42 pm »
Hello, I am new to these forums, and it was only recently that I registered. For a while now I have continuously encountered this aggravating problem. I search for hours to find a solution, but all to no avail. So I came here to ask if anyone could help.

My problem is with changing the sprite's color to red, then having it wait a half of a second, and then changing back. I wanted this to occur when a key is pressed. The problem is that I have to hold down the key for it to occur. It won't occur otherwise. If i just change "event.key.code" to "isKeyPressed" it doesn't help me at all.
I use sfml clock to do this, but I am not sure what i am doing wrong.
The key in question that I press is the space bar.
Here is the code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <time.h>
#include <Windows.h>
#include <SFML\My Additions to SFML\sfml.h> // this file is just for linkig sfml, so if it is already linked, don't worry about it
#include <sstream>
#include <iomanip>
#include <iostream>
using namespace std;
int sprite_on_screen;
sf::RenderWindow window(sf::VideoMode(1700, 900), "SFML RPG test");
float x;
float y;
int rectleft = 300;
int recttop = 0;
int rectwidth = 300;
int rectheight = 300;
sf::Event event;
sf::Texture texture;
sf::IntRect rectSourceSprite(rectleft, recttop, rectwidth, rectheight);
sf::Sprite sprite(texture,rectSourceSprite);
sf::Clock clock1;
int color1 = sprite.getColor().toInteger();
void Clock0_Function(){
        if (clock1.getElapsedTime().asSeconds() > 0.5f){
                sprite.setColor(sf::Color::Red);} // increase and decrease
        if (clock1.getElapsedTime().asSeconds() > 1.0f){
                sprite.setColor(sf::Color(sf::Color(color1)));
        clock1.restart();}
                sprite.setTextureRect(rectSourceSprite);}
void Define_Texture(){
        texture.loadFromFile("Stickman sprite.png");}
void Define_Sprite_Position(){
        sprite.setPosition(1050, 500);}
void Redefine_Sprite_Position(){
        sprite.setPosition(950, 300);}
void Change_Sprite(){
        switch (event.type){
        case sf::Event::Closed:
                window.close();
                break;
        case sf::Event::KeyPressed:
                if((event.key.code == sf::Keyboard::Space))
                        Clock0_Function();
                break;}}
int main(){
window.setVerticalSyncEnabled(true);
        Define_Sprite_Position();
        Define_Texture();
while(window.isOpen())
    {
                while (window.pollEvent(event)){
                Change_Sprite();
      if (event.type == sf::Event::EventType::Closed){
                  window.close();}
          if ((sf::Keyboard::isKeyPressed(sf::Keyboard::F5))){
                  window.close();}
    }
window.clear();
window.draw(sprite);
window.display();
}
return 0;}
« Last Edit: February 23, 2016, 04:10:46 am by anrdyshmrdy »

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Problem's making a sprite "flash" a color
« Reply #1 on: February 22, 2016, 02:59:11 am »
First of all sf::Event MUST be here:

Quote
int main(){
window.setVerticalSyncEnabled(true);
        Define_Sprite_Position();
        Define_Texture();
while(window.isOpen())
    {        
                sf::Event event ; // HERE!!!!
                while (window.pollEvent(event)){
                Change_Sprite();
      if (event.type == sf::Event::EventType::Closed){
                  window.close();}
          if ((sf::Keyboard::isKeyPressed(sf::Keyboard::F5))){
                  window.close();}
    }
window.clear();
window.draw(sprite);
window.display();
}
return 0;}
 


Second: Use a boolean to switch from red to non-red

  bool colorRed = false
  // later //
  bool colorRed = !colorRed ; // Switch true to false or viceversa.
 

And third: You are changing the color of the sprite when the events are processed.

        while (window.pollEvent(event)){
        Change_Sprite();
 

Minimal and functional example:

#include <iostream>
#include <SFML/Graphics.hpp>


int main()
{
        // Create the window.
        sf::RenderWindow window( sf::VideoMode( 800 , 600 ) , "COLOR CHANGE" ) ;
 
        // Load a shape.
        sf::RectangleShape rectangle( sf::Vector2f( 100 , 50 ) ) ;
                           rectangle.setPosition( 400 , 300 ) ;

        sf::Clock clock ;
        bool colorRed = false ;
 
    // Main loop.
        while ( window.isOpen() )
        {      
                sf::Event event ; // Create the event-handler.

        while ( window.pollEvent( event ) ) // Handle the events.
        {
            switch( event.type ) // Use switch when possible. Switch is faster than a series of "if".
            {
                case sf::Event::Closed :
                     window.close() ;
                     break ;
            }
                }

                if( clock.getElapsedTime().asSeconds() > 0.5f )
                {
                    if( colorRed )
              rectangle.setFillColor( sf::Color::White ) ;
                        else
                          rectangle.setFillColor( sf::Color::Red ) ;
                         
                    colorRed = !colorRed ; // Switch them.
                        clock.restart() ;
                }

                window.clear() ;
                window.draw( rectangle ) ;
                window.display() ;
        }
                                                               
        return 0 ;
}
 

Hope it help you.

Postdata:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <time.h>
#include <Windows.h>
#include <SFML\My Additions to SFML\sfml.h> // this file is just for linkig sfml, so if it is already linked, don't worry about it
#include <sstream>
#include <iomanip>
#include <iostream>
 

« Last Edit: February 22, 2016, 03:23:11 am by DarkRoku »
I would like a spanish/latin community...
Problems building for Android? Look here

anrdyshmrdy

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Problem's making a sprite "flash" a color
« Reply #2 on: February 23, 2016, 12:37:07 am »
Thank you for the help DarkRoku. I just have one other problem. I don't know if I mentioned this in my post, but I want the sprite to flash once each time the space bar is pressed. I have altered the code to make it flash once, but it wont flash again when I press the space bar and if I press another key or move the mouse during the flash it stops flashing.
Here is the code:
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML\My Additions to SFML\sfml.h> // again, not necessary if you already linked SFML

int main()
{
    // Create the window.
    sf::RenderWindow window( sf::VideoMode( 800 , 600 ) , "COLOR CHANGE" ) ;
 
    // Load a shape.
    sf::RectangleShape rectangle( sf::Vector2f( 100 , 50 ) ) ;
                       rectangle.setPosition( 400 , 300 ) ;

    sf::Clock clock;
    bool colorRed = false ;
 
    // Main loop.
    while ( window.isOpen() )
    {  
        sf::Event event ; // Create the event-handler.

        while ( window.pollEvent( event ) ) // Handle the events.
        {
            switch( event.type ) // Use switch when possible. Switch is faster than a series of "if".
            {
                case sf::Event::Closed :
                     window.close() ;
                     break ;
            }
        }
                if(event.key.code == sf::Keyboard::Space)
        if(clock.getElapsedTime().asSeconds() > 0.5f)
                {
            if(colorRed){
                                rectangle.setFillColor(sf::Color::White);
                                }
            else{
                                rectangle.setFillColor(sf::Color::Red);
             
            colorRed = !colorRed ; // Switch them.
                        clock.restart();}
                }
        window.clear() ;
        window.draw( rectangle ) ;
        window.display() ;
    }
                               
    return 0 ;
}

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem's making a sprite "flash" a color
« Reply #3 on: February 23, 2016, 01:09:11 am »
Don't use the sf::Event outside of the pollEvent() block. It will only be equal to whatever the last event processed was (it can process multiple in one cycle). Process the events inside the window.pollEvent() loop so that each event is full processed.

Also, don't access event.key without first confirming that the event is a key event. If it's the wrong type, this code could be anything as it's a union (see the events tutorial - also note the red warning box)

I suggest you add braces to that if (the one that tests the event key being space) so that the next block is clearly inside it. It should be indented too.
That said, that entire "if" (including the braces/indentation) should be inside the pollEvent block, as mentioned above probably changed to be included in the switch to first test that it's correct type.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Problem's making a sprite "flash" a color
« Reply #4 on: February 23, 2016, 02:25:31 am »
Thank you for the help DarkRoku. I just have one other problem. I don't know if I mentioned this in my post, but I want the sprite to flash once each time the space bar is pressed. I have altered the code to make it flash once, but it wont flash again when I press the space bar and if I press another key or move the mouse during the flash it stops flashing.

Here is another example. Try this:

#include <iostream>
#include <SFML/Graphics.hpp>


int main()
{
        // Create the window.
        sf::RenderWindow window( sf::VideoMode( 800 , 600 ) , "COLOR CHANGE" ) ;

        // Load a shape.
        sf::RectangleShape rectangle( sf::Vector2f( 100 , 50 ) ) ;
        rectangle.setPosition( 400 , 300 ) ;

        sf::Clock clock ;
        bool colorRed = false ;

        // Main loop.
        while ( window.isOpen() )
        {
                sf::Event event ; // Create the event-handler.

                while ( window.pollEvent( event ) ) // Handle the events.
                {
                        switch ( event.type ) // Use switch when possible. Switch is faster than a series of "if".
                        {
                                case sf::Event::Closed :
                                        window.close() ;
                                        break ;
                                case sf::Event::KeyPressed :
                                        if ( event.key.code == sf::Keyboard::Space )
                                        {
                                                if ( colorRed )
                                                        rectangle.setFillColor( sf::Color::White ) ;
                                                else
                                                        rectangle.setFillColor( sf::Color::Red ) ;

                                                colorRed = !colorRed ; // Switch them.
                                        }
                                    break ;  
                        }
                }

                window.clear() ;
                window.draw( rectangle ) ;
                window.display() ;
        }

        return 0 ;
}
 

If you really need to delimit the time for switching colors you could still use sf::Clock. (If you support c++11 will be better to use std::chrono )

An exmaple with space-bar hit + sf::Clock.

(click to show/hide)

With a small wrapper for std::chrono.

(click to show/hide)
« Last Edit: February 23, 2016, 02:37:48 am by DarkRoku »
I would like a spanish/latin community...
Problems building for Android? Look here

anrdyshmrdy

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Problem's making a sprite "flash" a color
« Reply #5 on: February 23, 2016, 04:10:06 am »
Thank you again, DarkRoku. I think that my issue is solved.
« Last Edit: February 23, 2016, 04:54:14 am by anrdyshmrdy »

 

anything