2

I'm going through this tutorial on SoundEffect: http://msdn.microsoft.com/en-us/library/bb195053.aspx However, I'm getting the error from the compiler that "Content" doesn't exist in the current context.

Here's the code:

using Microsoft.Xna.Framework.Audio; 
using Microsfot.Xna.Framework.Content;

namespace SoundTouchTest 
{
     class SoundTouchTest
     {
            public void loadContent()
            {
                  SoundEffect s; 
                  s = Content.Load<SoundEffect>("example"); 
            }
     }
}

I've successfully added the references (or at least I think that I have).Any ideas why this is happening? Thanks!

4

3 回答 3

8

Content 是一个属性,通常引用类中的类型ContentManager实例Game。由于您的SoundTouchTest课程不是 a Game,因此它没有该属性

您应该ContentManager实例作为参数传递给您的loadContent()方法:

void loadContent(ContentManager content)
{
    SoundEffect e = content.Load<SoundEffect>("example");
}

在你Game班里的某个地方:

SoundTouchTest soundTouchTest = new SoundTouchTest();
soundTouchTest.loadContent(this.Content);
于 2012-12-05T15:01:11.803 回答
2

您可以ContentManager通过 loadContent 作为参数传入,然后在 game1 中找到的 load 方法中调用 loadContent ,或者您可以创建它的新实例(我不建议这样做,您不想每次都创建一个新对象当已经为您创建了对象时需要加载一些东西)。

于 2012-12-05T12:49:54.870 回答
0

而不是 Content.Load 它应该真的是 ContentManager.Load

http://msdn.microsoft.com/en-us/library/bb197848.aspx

您可能需要创建一个 Content Manager 实例才能使用它

于 2012-12-05T08:20:58.357 回答