Game Development Community

Sound Effects?

by Justin Wright · in iTorque 2D · 08/11/2012 (7:15 pm) · 12 replies

I'm new to iTorque, and I've been working through the tutorials. I don't see anything in there about how to play a sound effect. Anyone mind telling me how real quick?

#1
08/12/2012 (5:05 am)
Here's code snippet to play a music file.

Change single quote to double quote. Something wrong with this forum.

new AudioDescription(MusicNonLooping)
{	
   volume   = 0.75;
   isLooping= false;
   is3D     = false;
   type     = $musicAudioType; // Use $effectsAudioType for sound effects
};

new AudioProfile(musicNFMenu)
{  
   filename = '~/data/audio/NFMenuMusic.wav';
   description = 'MusicNonLooping';
   preload = false;
};

// To play the music
alxPlay(musicNFMenu);
#2
08/12/2012 (9:13 am)
Thanks man.
#3
08/12/2012 (10:00 pm)
I couldn't get it to work. Here's my code: And yes, I made sure to put the audio file in the audio folder.


//-----------------------------------------------------------------------------
// Torque
// Copyright GarageGames, LLC 2011
//--------------------------------------------------------------------------

new AudioDescription(MusicNonLooping)
{
volume = 1.0;
isLooping = false;
is3D = false;
type = $effectsAudioType;
};

new AudioProfile(musicNFMenu)
{
filename = "~/data/audio/WeaponBonus.wav";
description = "MusicNonLooping";
preload = false;
};

function buttonClass::onLevelLoaded(%this, %scenegraph)
{
// .....
%vector = "0 -55";
%this.moveTo(%vector, 355.0, true, false, false, 0.1 );
alxPlay(musicNFMenu);
}
#4
08/12/2012 (10:14 pm)
Your code looks fine. I'm not sure why it's not playing.
#5
08/13/2012 (8:24 am)
You don't want "~/data/audio/file.wav", try using "data/audio/file.wav" instead.

You might also want to schedule the music to play at the end of onLevelLoaded... I've had some tricky bits with playing audio before or immediately after switching levels.
#6
08/13/2012 (8:54 am)
Here's what I've got now. Still doesn't work.


//-----------------------------------------------------------------------------
// Torque
// Copyright GarageGames, LLC 2011
//--------------------------------------------------------------------------

new AudioDescription(MusicNonLooping)
{
volume = 1.0;
isLooping = false;
is3D = false;
type = $effectsAudioType;
};

new AudioProfile(musicNFMenu)
{
filename = "~/audio/WeaponBonus.wav";
description = "MusicNonLooping";
preload = false;
};

function buttonClass::onLevelLoaded(%this, %scenegraph)
{
// .....
%vector = "0 -55";
%this.moveTo(%vector, 355.0, true, false, false, 0.1 );
//alxPlay(musicNFMenu);
schedule(2000, "alxPlay", musicNFMenu);
}
#7
09/03/2012 (8:57 pm)
I'm having the same problem getting my sound effects to work. Here's an example of what I have in code.

In audio.cs:

new AudioDescription(AudioNonLooping)
{
    volume = 1.0;
    isLooping= false;
    is3D = false;
    type = $effectsAudioType;
};

new AudioProfile(buttonPress)
{
    filename = "data/audio/Piano-A4.WAV";
    description = "AudioNonLooping";
    preload = false;
};

And in my buttons.cs file:

function muteButton::onTouchUp( %this, %touchID, %worldPos )
{
    alxPlay(buttonPress);
    %this.setFrame( 0 );
    %this.takeAction();
}

The other muteButton functions work, so I know that isn't the issue. I have audio.cs set to exec in main.cs. The wave file plays fine in media player, so I know it works. I'm not getting any error in the console.log file. Anyone see a problem with that code that I'm missing?
#8
09/03/2012 (9:03 pm)
Okay, it appears to be a problem with the file itself. I'm betting it's a compression issue. I re-exported it from my sound software without any compression and it was fine. So Justin, try a wav file that you know is not compressed and see if that fixes it for you.
#9
12/10/2012 (11:35 pm)
Hello I have the same problem here is my definition

new AudioProfile(disparo)
{
filename = "data/audio/disparo.wav";
description = "AudioNonLooping";
preload = false;
};
and in my "fire" button

alxPlay(disparo);

I dont get any sound, is there something Im not loading? I would really thank any help

Gracias
#10
12/18/2012 (1:22 pm)
@Raul - check your console.log file in your project folder for errors. Hopefully it will tell you it was unable to play the sound or something to give a clue as to what the problem is.
#11
12/18/2012 (6:01 pm)
Just for future reference, if you have a problem, it probably would be faster to google it like this:

site:garagegames.com (Whatever you need)

It's probably best to do that before making a new thread.

Oh, btw, that Alx thing never works for me. "PlayMusic();", however, works fine.
#12
12/18/2012 (7:40 pm)
"That alx thing" is the only way the engine has to play sounds - it's how OpenAL is exposed to the engine and to script. I'm not sure where you found PlayMusic() - it's not part of the stock engine code. But whatever works I guess.