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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - anrdyshmrdy

Pages: [1]
1
Graphics / Re: Jumping over sprite collision issue[SOLVED]
« on: March 26, 2016, 11:43:39 pm »
Well, I managed to figure out the solution on my own. Turns out that it was a very easy fix.For future reference, here's the solution:
  else if (inAir == maxInAir)
                    {
                        // Down side crash
                        onGround = true;
                        inAir = 0.f;
                        player.setPosition( player.getPosition().x, player.getPosition().y - area.height );
                    }
                }

When I tested the variables using cout, I saw that the down side crash was being recognized when inAir was not equal to maxInAir. So I added an else if statement to make the section check when inAir was equal to maxInAir, and it immediately fixed it.

2
Graphics / Jumping over sprite collision issue[SOLVED]
« on: March 24, 2016, 03:45:26 pm »
I have been using SFML for about 4 months now. I recently began making a 2D platformer, after finally learning how to make a sprite jump by using AlexanderX's jumping tutorial. I pretty much use the same code AlexanderX used to make the sprite jump. I have no problem with the jumping overall, but there was one nagging issue I encountered that involved the sprite jumping over a block. When I hold down either the left or right key, collide into a block, and jump while still holding down either the left or right key, the moment the sprite goes over the corner of the block, the sprite acts as though it already landed and jumped again. Because this happens, the player looks like it is moving diagonally over a staircase of blocks. To better explain it, the sprite looks as though it never collides with the ground, and looks like it is floating diagonally over a staircase of blocks. I realize this could be a very common problem, but I couldn't find anything online to help solve it.
The code I have here is the code from the tutorial. I'm using this because it is much shorter than my code, and I didn't want to just show a small part, since you can't really see how it is applied. This code still has the same issue that I had in my code, so it will still help identify the problem.
#include <SFML/Graphics.hpp>
 
#include <vector>
 
const int gravity = 500;
bool onGround = false;
float inAir;
float maxInAir = 0.3f;
 
void move(sf::Vector2f &playerVelocity, float dt)
{
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
    {
        playerVelocity.x = -gravity;
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
    {
        playerVelocity.x = gravity;
    }
    else if (playerVelocity.x != 0)
    {
        playerVelocity.x = 0;
    }
 
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && (onGround || inAir < maxInAir))
    {
        playerVelocity.y = -gravity;
        inAir += dt;
    }
    else
    {
        playerVelocity.y = gravity;
        inAir = maxInAir;
    }
}
 
int main()
{
    sf::RenderWindow window(sf::VideoMode(800,600), "How to apply collision?");
 
    // Loading player texture
    sf::Texture playerTexture;
    if (!playerTexture.loadFromFile("player.png")) return 0;
 
    // Creating player sprite
    sf::Sprite player;
    player.setTexture(playerTexture);
 
    // Loading grass texture
    sf::Texture grassTexture;
    if (!grassTexture.loadFromFile("grass.png")) return 0;
 
    // Creating a vector because we have more blocks and we will need them into a container
    std::vector<sf::Sprite> grass;
 
    // Add 4 grass blocks to the container
    grass.resize(4);
    for (std::size_t i=0; i<3; ++i)
    {
        grass[i].setTexture(grassTexture);
        grass[i].setPosition(128 * i, 384);
    }
 
    // Last grass block will bo above the first one
    grass[3].setTexture(grassTexture);
    grass[3].setPosition(0,256);
 
    // Create a sf::Vector2f for player velocity and add to the y variable value gravity
    sf::Vector2f playerVelocity(0, gravity);
 
    sf::Clock clock;
    while (window.isOpen())
    {
        // Get the frame elapsed time and restart the clock
        float dt = clock.restart().asSeconds();
 
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
 
        // Apply physics to player
        player.setPosition(player.getPosition().x + playerVelocity.x * dt, player.getPosition().y + playerVelocity.y * dt);
        onGround = false;
        for (std::size_t i=0; i<grass.size(); ++i)
        {
            // Affected area
            sf::FloatRect area;
            if (player.getGlobalBounds().intersects(grass[i].getGlobalBounds(), area))
            {
                // Verifying if we need to apply collision to the vertical axis, else we apply to horizontal axis
                if (area.width > area.height)
                {
                    if (area.contains({ area.left, player.getPosition().y }))
                    {
                        // Up side crash
                        player.setPosition({ player.getPosition().x, player.getPosition().y + area.height });
                    }
                    else
                    {
                        // Down side crash
                        onGround = true;
                        inAir = 0.f;
                        player.setPosition({ player.getPosition().x, player.getPosition().y - area.height });
                    }
                }
                else if (area.width < area.height)
                {
                    if (area.contains({ player.getPosition().x + player.getGlobalBounds().width - 1.f, area.top + 1.f }))
                    {
                        //Right side crash
                        player.setPosition({ player.getPosition().x - area.width, player.getPosition().y });
                    }
                    else
                    {
                        //Left side crash
                        player.setPosition({ player.getPosition().x + area.width, player.getPosition().y });
                    }
                }
            }
        }
 
        move(playerVelocity, dt);
 
        window.clear();
         
        // Draw the grass
        for (std::size_t i=0; i<grass.size(); ++i)
        {
            window.draw(grass[i]);
        }
 
        // Draw the player
        window.draw(player);
 
        window.display();
    }  
 
    return 0;
}

3
Graphics / Re: Problem's making a sprite "flash" a color
« on: February 23, 2016, 04:10:06 am »
Thank you again, DarkRoku. I think that my issue is solved.

4
Graphics / Re: Problem's making a sprite "flash" a color
« 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 ;
}

5
Graphics / [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;}

Pages: [1]