Search code examples
objective-ccocos2d-iphonesimpleaudioengine

Sound button not working? COCOS2D


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?


Solution

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