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 - PapaSmurf

Pages: [1]
1
General / Input validation
« on: April 21, 2015, 08:28:55 pm »
Hi,

I have the following code which controls the players movement and shooting however I need a way to stop the player moving and shooting at the same time. I really don't know how to go about it.

I tried putting them all in else if's but this did not work.

void Game::handleEvents()
{
        sf::Event event;
        while (window.pollEvent(event))
        {
                // if the X is click or escape is pressed on the keyboard it will close the window
                if (event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
                        window.close();

                switch (event.type)
                {
                case sf::Event::KeyPressed:handleInput(event.key.code, true, temp);
                        break;
                case sf::Event::KeyReleased:handleInput(event.key.code, false, temp);


                default:
                        break;

                }
        }
}



void Game::handleInput(sf::Keyboard::Key keypress, bool keyPressed, sf::Clock temp)
{

        if (keypress == sf::Keyboard::Up || keypress == sf::Keyboard::W)
        {
                pMoveUp = keyPressed;
        }

        else if (keypress == sf::Keyboard::Down || keypress == sf::Keyboard::S)
        {
                pMoveDown = keyPressed;
        }

        else if (keypress == sf::Keyboard::Right || keypress == sf::Keyboard::D)
        {
                pMoveRight = keyPressed;
        }

        else if (keypress == sf::Keyboard::Left || keypress == sf::Keyboard::A)
        {
                pMoveLeft = keyPressed;
        }
       


        if (keypress == sf::Keyboard::Space && keyPressed && lastFiredArrow.getElapsedTime() > sf::milliseconds(250))
        {
                // shoot stuff
        }

}
 

Any Ideas?

Kind Regards
Andy

2
Graphics / Getting projectiles to move according to time
« on: March 23, 2015, 12:14:48 pm »
Hi,

I need to get my projectiles moving relevant to time. As well as sprite animation.

The code that I have for my bullets at the moment is as follows. They do not seem to animate with relevance to time. To be honest I am pretty baffled by this.



struct BulletData
{
        sf::Vector2f direction;
        sf::Vector2f startpos;  
        float        speed;    
        bool         active;   
        BulletData()
        {
                direction = sf::Vector2f(0.0f,0.0f);
                startpos = sf::Vector2f(0.0f,0.0f);
                speed    = 600000.0f;
                active   = false;
        }
};
        sf::Time            dt; // delta time
        sf::Time            elapsedTime;

        sf::Clock clock;

        elapsedTime += dt;
        int timeAsMs = elapsedTime.asMilliseconds();
        dt = clock.restart();


 void Game::updateBulletCollisions(sf::Time dt)
{

        for (int i = 0; i < NO_BULLETS; i++)
        {
                if (this->_arr_bullet_data[i].active)
                {
                        sf::Vector2f tbulletvel = this->_arr_bullet_data[i].direction * this->_arr_bullet_data[i].speed * dt.asSeconds();
                        this->_arr_bullet_spr[i].move(tbulletvel);
       }
}
 

Regards
Andy

3
Graphics / Re: Extra sprite spawning when the enemies are redrawn SFML
« on: March 23, 2015, 09:41:28 am »
Could you advise how to write the loop without skipping the elements?

4
Graphics / Extra sprite spawning when the enemies are redrawn SFML
« on: March 23, 2015, 01:38:05 am »
I am making a game in sfml and at the moment when all of the enemies die. They are set to respawn however when this is happening they are respawning with one extra sprite than before.

The code for loading in the sprites is
Code: [Select]
unsigned int orcNumber = 5;
for (int i = 0; i < orcNumber; i++)
{
    SpriteVector.push_back(ogreSprite);
    SpriteVector[i].setPosition(spawnPointX[i], spawnPointY[i]);
}
The code for removing the enemies if they are offscreen or shot is similar to below using erase.

for (unsigned j = 0; j < SpriteVector.size(); j++)
{
    if (this->SpriteVector[j].getPosition().x < 0 - 80 )
    {
        //this succesfully removes the object from the vector
        SpriteVector.erase(SpriteVector.begin() + j);
        std::cout << "Container size..." << SpriteVector.size() << "\n";
    }
}
The statement for redrawing them is:

unsigned int orcNumberRespawn = 5;
if (SpriteVector.size() <= 1)
{           
    for (int i = 0; i < orcNumberRespawn; i++)
    {
        SpriteVector.push_back(ogreSprite);
        SpriteVector[i].setPosition(spawnPointX[i], spawnPointY[i]);
    }
}

window.draw(SpriteVector[i]);
Can anyone identify why when the sprites need to be redrawn it draws with + 1 sprite everytime?

5
Graphics / Advise on collision detection using std::vector
« on: March 16, 2015, 12:08:09 am »
Hi,

I am unsure which method to use to handle my enemies in SFML.

The idea that I am trying to recreate is to spawn enemies every so many seconds. Initially I wanted to do this using a vector such as std::vector<sf::Sprite> SpriteVector; The advantages of this method would be that in theory it is easy to add and remove my enemies from the vector. Using the push_back and erase method.

However the error with this at the moment is that when I am trying to check whether the sprite or the arrows are hitting an enemy within my vector it will not work as I am getting the following error.

19
IntelliSense: no instance of overloaded function "sf::Rect<T>::intersects [with T=float]" matches the argument list
            argument types are: (sf::Sprite)
            object type is: sf::FloatRect
h:\Game Alpha 1.2\Game Alpha 1.2\Game.cpp
461 16
Game Alpha 1.2

Error 17 error C2664: 'bool sf::Rect<T>::intersects(const sf::Rect<T> &) const' : cannot convert parameter 1 from 'sf::Sprite' to 'const sf::Rect<T> &' h:\game alpha 1.2\game alpha 1.2\game.cpp 461 1 Game Alpha 1.2


I know that I could possibly use an array to store the enemies then set their state when hit to false however I am not sure how this will effect spawning my enemies. The functionality of being able to add to a vector is what I think I will need. Otherwise I am sure not sure how to get around the issue of having a certain number of enemies within the array then spawning more over a series of time.



Could you please advise as I am really stuck on this?

Kind Regards

6
Graphics / 4th Shooting statement not working in SFML game.
« on: March 14, 2015, 04:10:33 pm »
Hi,

I am currently creating a game using SFML 2.0. Basically it is a top-down shooter and I am having difficulty with the statement below. Which shoots an arrow when the space bar is pressed.

There are four statements which depend on the way the player sprite (archSprite) is facing.
This is determined from the source.y which is calculated by this function -

    int getCellY(DirectionKeys direction)
    {
       return (direction*64);
    }

I am following the logic that the statement

    int dir = source.y/64;

Will find out the direction the sprite is facing with the spritesheet I am using
0 = Up
1 = Left
2 = Down
3 = Right

This statement shoots correctly for everything except shooting right

    if(keypress == sf::Keyboard::Space && keyPressed)
    {
    int tblt = this->_game_data.curr_bullet;
    this->_arr_bullet_data[tblt].active      = true;
   
    //every 100th bullet it is set to the up rotation
    int dir = source.y/64;
   
    if (dir == 0)
    {
       _arr_bullet_spr[tblt].rotate(90);
       this->_arr_bullet_data[tblt].startpos     = this->archSprite.getPosition() + sf::Vector2f(64,-42);
       this->_arr_bullet_data[tblt].direction   = sf::Vector2f(0,-1);
    }
    else if (dir == 1)
    {
       _arr_bullet_spr[tblt].rotate(360);
       this->_arr_bullet_data[tblt].startpos     = this->archSprite.getPosition() + sf::Vector2f(-22,0);
       this->_arr_bullet_data[tblt].direction   = sf::Vector2f(-1,0);
    }
    else if (dir = 2)
    {
       _arr_bullet_spr[tblt].rotate(270);
       this->_arr_bullet_data[tblt].startpos     = this->archSprite.getPosition() + sf::Vector2f(0,96);
       this->_arr_bullet_data[tblt].direction   = sf::Vector2f(0,1);
    }
    else if (dir = 3)
    {
       _arr_bullet_spr[tblt].setRotation(180);
       this->_arr_bullet_data[tblt].startpos     = this->archSprite.getPosition() + sf::Vector2f(0,0);
       this->_arr_bullet_data[tblt].direction   = sf::Vector2f(1,0);
    }
   
    this->_arr_bullet_spr[tblt].setPosition(this>_arr_bullet_data[tblt].startpos);
    this->_game_data.curr_bullet++;
    }

Can anyone figure out why my arrow for direction right is shooting down and the arrow is not rotating?

It is like the statement for shooting right is being ignored all together.

Can anyone please advise?

Many Thanks

Pages: [1]