【问题标题】:cocos2d - How can I play sound effect only when the user is touching?cocos2d - 如何仅在用户触摸时播放音效?
【发布时间】:2023-04-07 15:11:02
【问题描述】:

如何仅在用户触摸时播放声音效果?只需重复它并在用户结束触摸时停止。

谢谢。

【问题讨论】:

    标签: cocos2d-iphone simpleaudioengine


    【解决方案1】:

    在你的场景中使用

    self.isTouchEnabled = YES;
    

    现在您可以响应 ccTouchBegin 或 ccTouchEnd 之类的方法(忘记了它们的名字)。所以是的,当触摸开始时,您可以安排一个方法来一遍又一遍地播放声音效果。当触摸结束时,取消调度。

    【讨论】:

      【解决方案2】:

      如果你使用SimpleAudioEngine,你可以在调用ccTouchesBegan:withEvent:时使用playBackgroundMusic:(默认循环播放),然后在调用ccTouchesEnd:withEvent:时调用stopBackgroundMusic:。

      使用 SimpleAudioEngine:

      #import "SimpleAudioEngine.h"
      

      在 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"];
      

      在 ccTouchesEnd:withEvent 中停止音乐:

      [[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-15
        • 1970-01-01
        相关资源
        最近更新 更多