Where to place sound files

Pages: 12
It's not running your music because you only call play() if the music failed to load.

Remember: openFromFile returns:

- true if the load succeeded.
- false if the load failed.


The not operator must be tripping you up. By putting that exclamation point in there, you're basically reversing whatever openFromFile returns. false becomes true, and true becomes false.

1
2
3
4
5
6
7
8
9
if( !foo )
{
   // this block will only be executed if 'foo' is false
}

if( foo )
{
   // this block will only be executed if 'foo' is true
}
Lol I don't understand why all of SFML's tutorials use the ! then. It's so misleading.

edit:

I took out the ! and it works fine lol.
Last edited on
Lol I don't understand why all of SFML's tutorials use the ! then. It's so misleading.


Their if statements are handling error conditions. IE: it's printing an error message saying the file couldn't load.


It's not really misleading -- they're probably just assuming you know more about the language than you seem to. But that's okay, we all start somewhere. ;)
Last edited on
The way I tend to learn is dive into a project and kinda reverse engineer it. The things I don't understand I try to learn. Then eventually it all comes together easier.

I will say though, you are making my life way easier.
The way I tend to learn is dive into a project and kinda reverse engineer it. The things I don't understand I try to learn. Then eventually it all comes together easier.


I did a similar thing when I first started out. It's a fine way to do it. =)

I will say though, you are making my life way easier.


I'm happy to help.
Hey Disch,

In my game I have gravity. When you jump you fall back down. However the player can hold the jump button and keep floating.

What would be the best way to stop that? I made it so you can only float up to a certain point, but I need it so it like knocks you back down. Or maybe limit the player to only being able to press the W <-- jump key once per 2 seconds or something.

What would be the best way to tackles these? Time? Velocity?
Velocity?


Velocity. For sure. It's how you should handle pretty much all movement (except for things like teleports).

- Have each entity keep a velocity vector (like an sf::Vector2f or whatever)

- Every logic update, add the velocity to the entity's position.

- When the player moves left/right, adjust their velocity, not their position. Perhaps implement some kind of maximum horizontal velocity to prevent them from moving too fast.

- To simulate gravity, just add a fixed value to their downward velocity every update. Once they are touching the ground, you can reset their vertical velocity to zero and stop applying gravity.

- To jump, check to see if the player is touching the ground. If they aren't, don't let them jump. If they are, just give them a sudden burst of upward velocity.

- If you want more advanced jump control, like where holding jump longer makes you jump higher, then you can make it so when the user lets go of the jump button, you kill any upward velocity (if they have any).
Topic archived. No new replies allowed.
Pages: 12