声音按钮不起作用?的Cocos2D

时间:2013-02-22 15:07:19

标签: objective-c cocos2d-iphone simpleaudioengine

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

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

在init

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;

怎么了?

3 个答案:

答案 0 :(得分:0)

A)Set a breakpoint - 任何程序员的基本知识。

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

{
  // 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"];
}