【问题标题】:Sound button not working? COCOS2D声音按钮不起作用? COCOS2D
【发布时间】:2013-03-17 03:15:01
【问题描述】:

我试过这个代码它不起作用,我希望按钮在单击时播放声音。

-(void)starButtonsound:(id)sender
{
    [[SimpleAudioEngine sharedEngine] playEffect:@"fire_truck_1.wav"];
}

在初始化中

starMenuItemsound = [CCMenuItemImage
                    itemWithNormalImage:@"play.png" selectedImage:@"play.png"
                    target:self selector:@selector(starButtonsound:)];
starMenuItemsound.position = ccp(525, 500);
[self addChild:starMenuItemsound z: 4];
starMenuItemsound.scale = 0.75;

怎么了?

【问题讨论】:

  • 需要更多信息。它到底做错了什么?
  • 你能从控制台显示日志吗?
  • 看起来 cocosDenshion 的标签在 SO 上丢失了

标签: objective-c cocos2d-iphone simpleaudioengine


【解决方案1】:

在初始化中

{
  // Using SpriteSheet ? Use spriteWithSpriteFrameName in place of spriteWithFile
       CCSprite *spriteNormal   = [CCSprite spriteWithFile:@"play.png"];
       CCSprite *spriteSelected   = [CCSprite spriteWithFile:@"play.png"];
       ccColor3B color = {128,128,128};
       spriteSelected.color = color;

       starMenuItemsound = [CCMenuItemImage
                        itemWithNormalSprite: spriteNormal selectedImage: spriteSelected
                        target:self selector:@selector(starButtonsound:)];
       starMenuItemsound.position = ccp(525, 500);
       starMenuItemsound.scale = 0.75;

       CCMenu *menu = [CCMenu menuWithItems: starMenuItemsound, nil];
       menu.position = ccp(0.0f,0.0f);
       [self addChild: menu z:4];
  }

调用函数:

-(void) starButtonsound:(id)sender
{
    [[SimpleAudioEngine sharedEngine] setEffectsVolume:1.0f]; //Do this in init method
    [[SimpleAudioEngine sharedEngine] playEffect:@"fire_truck_1.wav"];
}

【讨论】:

    【解决方案2】:

    A) Set a breakpoint - 任何程序员的必备知识。

    B) CCMenuItemImage 选择器不带参数。从@selector(startButtonSound) 中删除冒号并将方法更改为:

    -(void)starButtonsound
    {
        [[SimpleAudioEngine sharedEngine] playEffect:@"fire_truck_1.wav"];
    }
    

    【讨论】:

    • 它似乎没有区别.. -(void)starButtonsound { [[SimpleAudioEngine sharedEngine] playEffect:@"fire_truck_1.wav"]; } starMenuItemsound = [CCMenuItemImage itemWithNormalImage:@"play.png" selectedImage:@"play.png" target:self 选择器:@selector(starButtonsound)]; starMenuItemsound.position = ccp(525, 500); //CCMenu *starMenusound = [CCMenu menuWithItems:starMenuItem, nil]; [self addChild:starMenuItemsound z: 4]; starMenuItemsound.scale = 0.75;我很困惑..
    【解决方案3】:

    有人报告说这是一个解决方案:关闭声音再打开(不是开玩笑)

    [SimpleAudioEngine sharedEngine].muted = YES;
    [SimpleAudioEngine sharedEngine].muted = NO;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      相关资源
      最近更新 更多