I'm trying to add in some sound on my little project... and my ears start bleeding because its playing the sound every loop!
Why does the code below print "Hello" every loop it seems? The actions inside the collision IF statement should only occur when the balls collide:
Method Update()
'update positions
point.x:+Vec.x
point.y:+Vec.y
If point.x<radius Then
point.x=radius
vec.x:*-1.0
PlaySound sfxballwall
End If
If point.x>GraphicsWidth()-radius Then
point.x=GraphicsWidth()-radius
vec.x:*-1.0
PlaySound sfxballwall
End If
If point.y<radius Then
point.y=radius
vec.y:*-1.0
PlaySound sfxballwall
End If
If point.y>GraphicsHeight()-radius Then
point.y=GraphicsHeight()-radius
vec.y:*-1.0
PlaySound sfxballwall
End If
For Local ball2:Enemyball = EachIn BallList
Local collisionDistance# = 20
Local actualDistance# = Sqr((ball2.point.x-point.x)^2+(ball2.point.y-point.y)^2)
If actualDistance<collisionDistance
Local collNormalAngle#=ATan2(ball2.point.y-point.y, ball2.point.x-point.x)
Local moveDist1#=(collisionDistance-actualDistance)*(ball2.mass/Float((mass+ball2.mass)))
Local moveDist2#=(collisionDistance-actualDistance)*(mass/Float((ball2.mass+mass)))
point.x=point.x + moveDist1*Cos(collNormalAngle+180)
point.y=point.y + moveDist1*Sin(collNormalAngle+180)
ball2.point.x=ball2.point.x + moveDist2*Cos(collNormalAngle)
ball2.point.y=ball2.point.y + moveDist2*Sin(collNormalAngle)
Local n:TVec2 = New TVec2.Init(Cos(collNormalAngle),Sin(collNormalAngle))
Local a1# = vec.DotProduct(n)
Local a2# = ball2.vec.DotProduct(n)
Local optimisedP# = (2.0 * (a1-a2)) / (mass + ball2.mass)
vec.x:- (optimisedP*ball2.mass*n.x)
vec.y:- (optimisedP*ball2.mass*n.y)
ball2.vec.x:+ (optimisedP*mass*n.x)
ball2.vec.y:+ (optimisedP*mass*n.y)
'Why is this being printed every loop it seems?
Print "Hello"
End If
Next
If Sqr((PlayerX-point.x)^2+(PlayerY-point.y)^2) < 20
SetColor 0,0,0
DrawText "GAME OVER",0,0
SetColor 255,255,255
End If
End Method
The balls rarely collide, most of the time they just bounce off the walls (the wall sound works correctly)
|