[英]Inherit from internal SoundEffectInstance, no constructors defined
I'm trying to create a new class FadableSoundEffectInstance
, that inherits from SoundEffectInstance
. 我正在尝试创建一个从SoundEffectInstance
继承的新类FadableSoundEffectInstance
。
My FadableSoundEffectInsance
will allow SoundEffectInstances
to be easlily faded in and out, however after finishing the class, I was rewarded with this lovely error here: 我的FadableSoundEffectInsance
允许SoundEffectInstances
轻松地淡入和淡出,但是在完成课程之后,我在这里遇到了一个可爱的错误:
The type 'Microsoft.Xna.Framework.Audio.SoundEffectInstance' has no constructors defined
After a while of research and digging into the XNA source, I found this. 经过一段时间的研究并深入研究XNA源码,我发现了这一点。
internal SoundEffectInstance(SoundEffect parentEffect, bool fireAndForget)
{
...
}
So it does have a constructor, but I cannot access it. 因此它确实具有构造函数,但我无法访问它。
How can I work around this to be able to create my class? 我该如何解决才能创建我的课程?
Note for people who have not used XNA before, you DO NOT create a new class with blah = new SoundEffectInstance
instead you load a SoundEffect
and use the method CreateInstance()
. 请注意,对于以前从未使用过XNA的用户,请勿使用blah = new SoundEffectInstance
创建新类,而应加载SoundEffect
并使用方法CreateInstance()
。 Also, SoundEffect
is a sealed class. 同样, SoundEffect
是一个密封类。 Also, adding a constructor to my new class will throw the same error, but for SoundEffectInstance
instead. 此外,向我的新类添加构造函数将引发相同的错误,但对于SoundEffectInstance
。
Here is my relevant code incase you need to look in depth 这是我的相关代码,以防您需要深入了解
FadableSoundEffectInstance FadableSoundEffectInstance
public sealed class FadableSoundEffectInstance : SoundEffectInstance
{
public float FadeSpeed = .01f;
public AudioFadeState FadeState = AudioFadeState.Normal;
public void FadeOut()
{
//Logic
}
public void FadeIn()
{
//Logic
}
public void Update(GameTime gameTime)
{
//Logic
}
}
And an extension method so I can call SoundEffect.CreateFadableInstance()
还有一个扩展方法,因此我可以调用SoundEffect.CreateFadableInstance()
public static FadableSoundEffectInstance CreateFadableInstance(this SoundEffect soundEffect)
{
return (FadableSoundEffectInstance)soundEffect.CreateInstance();
}
Your question is not unique. 您的问题不是唯一的。
You don't have access to the constructor, so you can't create a derived class. 您无权访问构造函数,因此无法创建派生类。 This is the same whether you use XNA or not. 无论是否使用XNA,这都是相同的。
If you want to use your new sound effect directly, you can create a pseudo-decorator, that takes in the actual SoundEffectInstance
and wraps it. 如果要直接使用新的声音效果,则可以创建一个伪装饰器,该伪装饰器将接收实际的SoundEffectInstance
并将其包装。
However, this doesn't help you if you want to pass your sound effect to XNA methods that want a SoundEffectInstance
. 但是,如果您要将声音效果传递给需要SoundEffectInstance
XNA方法,那么这对您没有帮助。
I solved this by adding a SoundEffectInstance
to the class instead of inheriting it, it's probably not the most elegant solution, but it works. 我通过向类中添加SoundEffectInstance
而不是继承它来解决此问题,它可能不是最优雅的解决方案,但它可以工作。
public SoundEffectInstance BaseInstance;
And changing my extension method to this: 并将我的扩展方法更改为此:
public static FadableSoundEffectInstance CreateFadableInstance(this SoundEffect soundEffect)
{
return new FadableSoundEffectInstance(soundEffect);
}
And my class to handle the sound effect, turn it into a SoundEffectInstance
, and do any initialization the class may need 我的类负责处理声音效果,将其转换为SoundEffectInstance
,并执行该类可能需要的任何初始化
public FadableSoundEffectInstance(SoundEffect soundEffect, float fadeSpeed = .1f)
{
BaseInstance = soundEffect.CreateInstance();
BaseInstance.Volume = 0;
FadeSpeed = fadeSpeed;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.