gpt4 book ai didi

c# - 如何使此代码更易于管理碰撞音频?

转载 作者:行者123 更新时间:2023-12-03 00:55:58 25 4
gpt4 key购买 nike

我只是在寻找可以使此代码更好的任何建议。如果我以这种方式继续前进,我认为这是可以避免的大量代码,但现在我无法做到这一点。这将很快失去控制。

public enum PhysicMaterialType
{
SoundFailure = 0,

AsteroidBall,

BalloonRubber
}

public void PlaySoundBasedOnCollision ( PhysicMaterialType phyMatA, PhysicMaterialType phyMatB )
{
switch ( phyMatA )
{
case PhysicMaterialType.AsteroidBall:
{
CheckAsteroidBallCollision ( phyMatB );

break;
}
case PhysicMaterialType.BalloonRubber:
{
CheckBalloonRubberCollision ( phyMatB );

break;
}
}

}

void CheckAsteroidBallCollision ( PhysicMaterial phyMatB )
{
switch ( phyMatB )

{
case PhysicMaterialType.AsteroidBall:
{
//Play AsteroidAsteroidBallSound

break;
}
case PhysicMaterialType.BalloonRubber:

{
break;
}
}
}

最佳答案

如果您将有很多类型,并且不同类型之间的预期碰撞声音必须以多种方式处理,请考虑为您要实现的每种类型从 PhysicMaterial 派生一个类。在派生类中定义一个公共(public)方法来处理确定播放哪种声音 - 将第二个对象的类型传递给此方法。像这样:

class PhysicMaterial
{
public virtual string GetCollisionSound (PhysicMaterialType targetType)
{
// define default behavior here, if derived class doesn't need to do anything special
}
}

class AsteroidBall : PhysicMaterial
{
public override string GetCollisionSound (PhysicMaterialType targetType)
{
...
}
}

class BalloonRubber: PhysicMaterial
{
public override string GetCollisionSound (PhysicMaterialType targetType)
{
...
}
}

返回最适合您的方案的任何数据类型。

关于c# - 如何使此代码更易于管理碰撞音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13770134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com