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

Author Topic: Trying to play sf::Sound from class void function on Collision Class  (Read 11316 times)

0 Members and 2 Guests are viewing this topic.

Wake

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Hi,

Please see the images below;

#Image 1: Collision.hp - I have a sf::SoundBuffer m_SoundBuffer; & sf::Sound m_Sound member variables.

#Image 2: Collision.cpp - In the Collision Class Constructor, I'm loading the .wav to the m_SoundBuffer and setting it to m_Sound;

#Image 3: Collision.cpp - I want the void function to play the sound if (bool m_IsColliding == true).

Result = The sound doesn't play when colliding with the item! :/

« Last Edit: January 18, 2021, 03:06:50 am by Wake »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Trying to play sf::Sound from class void function on Collision Class
« Reply #1 on: January 18, 2021, 05:25:22 am »
Are you calling playItemChime every frame and thus calling play every frame while m_IsColliding is true?

When you call play on a sound that is already playing it restarts it from the beginning, so if that's what you're doing your sound is restarting every frame so you never have enough time hear it.

WTF, why do you use a picture to show text?

Wake

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Trying to play sf::Sound from class void function on Collision Class
« Reply #2 on: January 18, 2021, 04:38:04 pm »
Good call!

I had it in the while (window.isOpen()) loop!

I guess I'll need to make a timer that doesn't this collision check at regard intervals way less than 1 frame. :)

I think it's easier to see the image of the code.

How do I do the code tags on this platform? On learncpp.com it was
Code: [Select]


Wake

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Trying to play sf::Sound from class void function on Collision Class
« Reply #3 on: January 18, 2021, 07:51:38 pm »
Are you calling playItemChime every frame and thus calling play every frame while m_IsColliding is true?

When you call play on a sound that is already playing it restarts it from the beginning, so if that's what you're doing your sound is restarting every frame so you never have enough time hear it.

WTF, why do you use a picture to show text?

My collider class is checking for the collision and displaying the "Item!" message bubble every frame. Our do you suggest I connect this to trigger a sound  outside of "while(isOpen) loop? I've been busting my brain on this all day!

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Trying to play sf::Sound from class void function on Collision Class
« Reply #4 on: January 18, 2021, 09:12:54 pm »
You could check the status of your sound and not play it if it is already "Playing"