Reputation: 536
I have array of coins spread in the screen.I want to add sound when I touch a coin.
private Music coinSound;
in show():
coinSound=assetManager.get(Assets.collectCoin);
calling coinSound.play() here:
if (MyInputProcessor.isTouchDown) {
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
game.camera.unproject(touchPos);
for (int i = 0; i < coins.length; i++) {
Rectangle textureBounds = new Rectangle(coins[i].getX(), coins[i].getY(), coins[i].getWidth(),coins[i].getHeight());
if (textureBounds.contains(touchPos.x, touchPos.y) && !coins[i].isBreakBool()) {
coinSound.play();
coins[i].setTapBool(true);
}
}
MyInputProcessor.isTouchDown = false;
}
Now sound is playing but not for all the array elements.for touch on one coin it is playing correctly every time.What do I need to change for making it play for each coins on touch?
Upvotes: 0
Views: 51
Reputation: 20140
Use Sound
instead of Music
.
Sound effects are small audio samples, usually no longer than a few seconds, that are played back on specific game events such as a character jumping or shooting a gun.
AssetManager manager = new AssetManager();
manager.load("data/mysound.mp3", Sound.class);
manager.finishLoading();
Get sound from AssetManager
Sound sound = manager.get("data/mysound.mp3", Sound.class);
sound.play();
Music
instances are heavy, you should usually not have more than one or two at most loaded.
Upvotes: 2