Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

4 years ago by yhsper

Hi, everyone!

I'm new in this forum and new to ImpactJS. So far, it has been a blast working with the framework. Now I have a question:

Does the .volume property of a Sound object controls its volume for all channels?

What's happening in the game I'm making is that I have an Entity which is spawned more than once (in my case, enemies) and I wanted to control the volume of the sounds they make (steps, shots, etc.) according to the distance from the player's Entity.

What seems to be happening is that whenever I set the volume for the sound of each individual instance of the Entity (with this.sound.volume = 1/(1+distance) or something) all entities' sounds seem to be affected.

Am I doing something wrong?

Thanks in advance!

4 years ago by dungeonmaster

I just checked the sound code and each new instance should have it's own sound volume. So what you are trying to do seems possible.

this.sound.volume = 1/(1+distance) 

Can you show how do you set this.sound in your entity, how do you create it?

And also, if your distances are in pixel distances, this formula (1/1+distance) will give you so little volume after ~10 pixel distance that you can barely understand the differance.Try a linear approximation with a max and min level set.

4 years ago by Joncom

Sounds like your enemy entities are sharing a common instance of ig.Sound.

It will happen if you load sounds this way:
EntityEnemy = ig.Entity.extend({
    exampleSound: new ig.Sound('path/to/sound.mp3'),
    init: function(x, y, settings) {
        this.parent(x, y, settings);
        ...
    },
    ...
});

You can create sounds individually (so entities don't share) like this:
EntityEnemy = ig.Entity.extend({
    exampleSound: null,
    init: function(x, y, settings) {
        this.parent(x, y, settings);
        this.exampleSound = new ig.Sound('path/to/sound.mp3');
        ...
    },
    ...
});

A word of caution: by using the second method, the sound will no longer get picked up by the preloader.

4 years ago by yhsper

Great, friends, thanks for the quick reply!

I'm creating the sound as a shared instance, yes, like Joncom's first snippet. I'm going to try the second way, and I'll get back to you guys.

One question: if the sound will no longer get picked by the preloader, when will it be loaded? Do I have to add it to the preloader manually?

4 years ago by dungeonmaster

Then they are loaded as soon as you create the instance of the sound.

To preload the sounds, create them like

exampleSound1: new ig.Sound('path/to/sound.mp3'),
exampleSound2: new ig.Sound('path/to/sound2.mp3'),

for example in you main game, so all of them will be preloaded. Then you can create sounds per instance as Joncom explained.
Page 1 of 1
« first « previous next › last »