Question : Adding sound to bouncing ball with AS3

Hi all

I have compiled a small AS3 for a bouncing ball with sound for when the ball hits the floorboards.  The sound volume falls away as the the bounces become smaller. I am getting  the "1120:Access of undefined property vol" error - it should be a simple solution - I have had variations of the "undefined property " errors before  and have solved them but, not this time. Can some one please corrrect and explain my error please.
Attached is the code.

Kind regards
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
import flash.media.Sound;
// create a ball sprite
var initialrotationSpeed=12;
var rotationSpeed:Number;
var initialxVel:Number=8;
var stage_width:Number=750;
ball_mc.x=80;
ball_mc.y=360;


// setup position and velocity variables
var xPos:Number=ball_mc.x;
var yPos:Number=ball_mc.y;
var xVel:Number ;
var yVel:Number = 0.5;
var grav:Number=1;
var snd:Sound = new bounce2();
snd.play();

addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
function onLoop(evt:Event):void {
    if(ball_mc.x/stage_width<1)
	{	
    xVel=initialxVel*(1-(ball_mc.x/stage_width)); 		
	yVel+=grav;	
	xPos+=xVel;
	yPos+=yVel;
	if(Math.abs(yPos-560)<1&&Math.abs(yVel)<1)
	{
	   removeEventListener(Event.ENTER_FRAME,onLoop);	
	}	
	else if(yPos>560){		
		yPos=560;
		yVel*=-.8;		
		xVel*=.8;
		vol-=0.1;
		snd.play(0,0,new SoundTransform(vol) );
		
	}
	ball_mc.x=xPos;
	ball_mc.y=yPos;
	if (ball_mc.x>stage.stageWidth-ball_mc.width/4||ball_mc.x<0+ball_mc.width/4) {
		xVel*=-1;
	}
	}
}

addEventListener(Event.ENTER_FRAME, Rotation);
function Rotation(event:Event):void {	 
  if(ball_mc.x/stage_width<1)
  {
	 
	ball_mc.rotation+=initialrotationSpeed*(1-ball_mc.x/stage_width);
	if(ball_mc.x<700)
	{	
    xVel=initialxVel*(1-(ball_mc.x/stage_width)); 			
	ball_mc.x+=xVel;
	}
	else
	{
		removeEventListener(Event.ENTER_FRAME,Rotation);
	}
  }
}

Answer : Adding sound to bouncing ball with AS3

I am reading what Garrett has just written  and as far as I am aware I have not knowingly created a custom class. In going over the whole exercise  I was reminded that  "bounceSound" was not a class of the mp3 . It is correct - yes, the mp3 is imbedded in the library and, was exported for actionscript however, the mistake I made was in the "linkage" box of the sound properties. In the box labelled "class", I had loaded  "bounceSound.mp3 ". What I did was remove the "mp3" from the file name so my class became "bounceSound". That is all I did and it works properly now so, my question now is  -  should the class be "bounceSound.mp3" or, "bounceSound" ? Was it an oversight of mine to include "mp3" in the class or, it should be there and the error is somewhere else. If you open the fla. I attached the other day and remove the "mp3"  from the class name in the properties box, it will work.
Random Solutions  
 
programming4us programming4us