C++/DirectX Trying to get a sound to play when moving an object?
Hi everyone, i have a directX project and would like to loop a sound when my object is moving, and then pausing it when it is still. I am using this code although cannot find where to put my play sound line for it to play at the right place. At the moment the sound loops continuously when the object is still rather than when it is moving, any ideas?
Dir = 0;
if(m_Keyboard.GetKeyState(KEY_RIGHT) == TRUE)
Dir |= 1;
if(m_Keyboard.GetKeyState(KEY_UP) == TRUE)
Dir |= 2;
if(m_Keyboard.GetKeyState(KEY_LEFT) == TRUE)
Dir |= 4;
if(m_Keyboard.GetKeyState(KEY_DOWN) == TRUE)
Dir |= 8;
XMove = ZMove = 0.0f;
if(Dir) {
MoveAngle = Angles[Dir] + m_Camera.GetYRotation();
//playsound
m_SndChannel[2].Play("..\\Data\\Earthslow.wav", 100, 0);
float Speed = (float)Elapsed / 1000.0f * 384.0f;
XMove = (float)sin(MoveAngle) * Speed;
ZMove = (float)cos(MoveAngle) * Speed;
// Change animation and check triggers
if(XMove != 0.0f || ZMove != 0.0f) {
if(LastAnim != 0) {
LastAnim = 0;
m_CharacterObject.SetAnimation(&m_CharacterAnim, "Walk", timeGetTime() / 20);
}
// Trigger showing map if any triggers hit
}
} else {
// Change animation to idle
if(LastAnim != 1) {
LastAnim = 1;
m_CharacterObject.SetAnimation(&m_CharacterAnim, "Idle", timeGetTime() / 20);
}
}
Try change this:
to:
Perhaps you just don't know enough programming yet or I'm too stupid to understand your problem, but this means "only play if you move".
Another problem might be if you press a key and hold it down so it repeats, the sample will start over again each time the key repeats (making stupid noise perhaps).
Anyway, good luck.
albert
m_SndChannel[2].Play("..\\Data\\Earthslow.wav", 100, 0);
to:
if (Dir!=0) m_SndChannel[2].Play("..\\Data\\Earthslow.wav", 100, 0);
Perhaps you just don't know enough programming yet or I'm too stupid to understand your problem, but this means "only play if you move".
Another problem might be if you press a key and hold it down so it repeats, the sample will start over again each time the key repeats (making stupid noise perhaps).
Anyway, good luck.
albert
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement