Reputation: 39
How can I play a sound effect only when the user is touching? Just repeat it and stop when the user ends the touch.
Thanks.
Upvotes: 0
Views: 525
Reputation: 1050
If you use SimpleAudioEngine, you can use playBackgroundMusic: (which defaults to loop) when you call ccTouchesBegan:withEvent:, then call stopBackgroundMusic: when you call ccTouchesEnd:withEvent:.
To use SimpleAudioEngine:
#import "SimpleAudioEngine.h"
To play the music in ccTouchesBegan:withEvent:
SimpleAudioEngine *sae = [SimpleAudioEngine sharedEngine];
[sae preloadBackgroundMusic:@"MusicLoop.mp3"];
sae.backgroundMusicVolume = 0.5f; // Sets the volume to 50%; this is optional
[sae playBackgroundMusic:@"MusicLoop.mp3"];
To stop the music in ccTouchesEnd:withEvent:
[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
Upvotes: 1
Reputation: 18149
In your scene use
self.isTouchEnabled = YES;
And now you can respond to methods like ccTouchBegin or ccTouchEnd (forgot their names). So yeah, when touch begins, you can schedule a method which will play the sound effecr over and over. And when the touch ends, unschedule it.
Upvotes: 1