I am trying to sync a sound effect with an action in a flash game, but it does'nt seem bo be working. I am trying to make an explsion sound when the player's ship gets destroyed, here's some of my code

mySoundObject = new Sound();                         //initializing my sound 
MySoundObject.attachSound("explosion.wav");

and then a bit later

createEnemy = function(type, x, y)
{
	var nm = "enemy" + enemyCount;
	enemyLayer.attachMovie(type, nm, enemyCount);
	enemyLayer[nm]._x = x;
	enemyLayer[nm].xline = x;	
	enemyLayer[nm]._y = y;	
	enemyLayer[nm].dy = Math.random() * 3 + 10;	
	enemyLayer[nm].t = Math.random() * 6.28;
	
	enemyLayer[nm].onEnterFrame = function()
	{
		this._x = this.xline + Math.sin(this.t) * 100;
		this._y += this.dy;
		
		this.t += 0.1;
		
		if (this._currentframe == 1 && ship.hitTest(this._x, this._y, true))
		{
			counter = 1;
			doCounter();
			ship.play();
			mySoundObject.start(0,1);                     //place I tried to put my sound
		}
		
		if (this._y > 500) this.removeMovieClip();
	}

	enemyCount ++;
	enemyCount %= 10;
}

I have tried a few things, but they either play the sound as soon as I start the game, or they do not play the sound at all. does anyone have any ideas?

Apologies for the delay in replying to this...For some reason I didn't spot this post until today!

From the code you've posted, the 2nd line of the following block of AS contains a simple typo which is the source of your problem:

mySoundObject = new Sound();                         
//initializing my sound 
MySoundObject.attachSound("explosion.wav");

In line 1 you've declared a sound object called "mySoundObject" and in line 2 you're attaching a sound file "explosion.wav" to a different object called "MySoundObject" (see the capital M? There's your problem. Identifier names are case sensitive, so mySoundObject and MySoundObject are treated as different entities!)

Try the following:

var mySoundObject:Sound = new Sound();                         //initializing my sound 
mySoundObject.attachSound("explosion.wav");

and that should solve your problem. Everything else in your code looks good!

Cheers for now,
Jas.

commented: good spot +19
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.