Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8515965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:17:25+00:00 2026-06-11T05:17:25+00:00

I see how to add a image file to a CCSprite but how can

  • 0

I see how to add a image file to a CCSprite but how can I also add a audio file?

Is the an attribute I can store a map or dictionary?

FYI I’m on a mobile device posting this so code is not an option right now, will try to update my question

UPDATE:

What I’m trying to do is associate an audio file with each CCSprite and on touch drag play the audio file.
Each CCSprite will play a different audio file.
I’m trying to figure out the easiest way to either attach or associate the audio file with the CCSprite

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:17:26+00:00Added an answer on June 11, 2026 at 5:17 am

    OK, here is the approach I’d prefer, depending on whether you are playing a “background music” sound or an “effect” sound:

    If you are playing an MP3 sound, ie background music:

    1) Add a parameter to the sprite constructor to assign a sound file:

    [MySpriteClass spriteWithSound:@"mySound.mp3"];
    

    2) Simply use:

    // on touchDragged:
    [[SimpleAudioEngine sharedEngine] playBackgroundMusic:self.sound];
    
    // on touchEnded: touchCancelled:
    [[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
    

    NOTE: This approach is simple because you can only play one BGM at any given time.


    If you are playing an uncompressed sound, ie effect:

    1) Add a parameter to the sprite constructor to assign a sound file:

    [MySpriteClass spriteWithSound:@"mySound.caf"];
    

    2) Play the effect on touchDragged: while storing the return value:

    // on touchDragged:
    soundID = [[SimpleAudioEngine sharedEngine] playEffect:self.sound];
    

    3) Stop the effect on touchEnded: using the soundID:

    // on touchEnded: cancelled:
    [[SimpleAudioEngine sharedEngine] stopEffect:soundID];
    

    NOTE: This approach is better as it allows you to play more sounds at a time.


    Final Remarks:

    If you have subclasses of each different sprite (ie, FishSprite, DogSprite, … etc), such that ALL instance of any given class have the same sound, it would be better to add static method to return the sound name for that class:

    // somewhere in DogSprite.m
    + (NSString *)soundName {
        return @"bark.caf";
    }
    

    And for the sake of completeness, add a superclass that implements the touchDragged:, touchEnded:, so you don’t have redundant code:

    // on touchDragged:
    soundID = [[SimpleAudioEngine sharedEngine] playBackgroundMusic:[[self class] soundName]];
    
    // on touchEnded: cancelled:
    [[SimpleAudioEngine sharedEngine] stopEffect:soundID];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm loading image from my database to temp file but my IIS can't see
Wanted to see if anyone knows how to add props to a image with
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
I see some app on Play Store, when install these apps auto add an
I can't add android sdk to eclipse.I download android sdk and i can't see
I am a codeignite newbie and tried to add an image in my file
In my app I see terrible performance when I add some custom components to
Hi I want to add an if statement that checks to see if a
I have this edmx model (see below) and I would like to add an
When you add the Twitter Search Widget to your website, you see that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.