1

I tried this code it didn't work, I want the button to play sound when it is clicked.

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

In 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;

Whats wrong?

Guru
  • 21,652
  • 10
  • 63
  • 102

3 Answers3

0

A) Set a breakpoint - essential knowledge for any programmer.

B) The CCMenuItemImage selector does not take an argument. Remove the colon from @selector(startButtonSound) and change the method to this:

-(void)starButtonsound
{
    [[SimpleAudioEngine sharedEngine] playEffect:@"fire_truck_1.wav"];
}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • It does not seem to make a difference.. -(void)starButtonsound { [[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); //CCMenu *starMenusound = [CCMenu menuWithItems:starMenuItem, nil]; [self addChild:starMenuItemsound z: 4]; starMenuItemsound.scale = 0.75; I am confused.. – user2083920 Feb 22 '13 at 15:21
0

Some reported that this is a solution: turning sound off and on again (no joke)

[SimpleAudioEngine sharedEngine].muted = YES;
[SimpleAudioEngine sharedEngine].muted = NO;
Stephane Delcroix
  • 16,134
  • 5
  • 57
  • 85
0

in init

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

Call Function:

-(void) starButtonsound:(id)sender
{
    [[SimpleAudioEngine sharedEngine] setEffectsVolume:1.0f]; //Do this in init method
    [[SimpleAudioEngine sharedEngine] playEffect:@"fire_truck_1.wav"];
}
Guru
  • 21,652
  • 10
  • 63
  • 102