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.