XNA - 跨类访问 vector2

发布于 2024-11-27 17:49:27 字数 629 浏览 1 评论 0原文

我正在实现一个粒子系统-article.cs,我需要访问移动敌人位置enemy.cs的向量2。我有多个粒子实例以及 << 中的敌人列表>这些是在 game1.cs 中分配的。

我的问题是如何从 << 访问敌人位置列表>在 game1 中设置并使其在 Particle.cs 中可用。 Borh粒子.cs和敌人.cs是游戏名称空间的一部分。

我尝试了各种分配对象的新实例和分配公共 set/get 的方法,但没有运气!

谢谢, 保罗.

添加更多详细信息: 当我回到我的开发电脑上时,我可以添加一些真实的代码,但现在需要一些进一步的评论。

oes.cs - 只是一个基本类,其中列出了变量 - 位置、速度、大小等...尽管这些都是在 game1.cs 中定义的值。

在 Game1.cs 中,我有一个所有敌人的列表以及他们位置的另一个列表。

我有一个在 Game1.cs 中调用的粒子引擎,它又引用 keywords.cs ->这是我需要调用 vector2 敌人位置值的地方。

我尝试通过建立 game1.cs 的实例来调用 molecular.cs 中的敌人位置,但这是针对每个粒子的,会减慢游戏运行速度。

我应该显示代码的哪些部分?

谢谢

I'm implementing a particle system- particle.cs which I need to access the vector2 of moving enemy location enemy.cs. I have multiple instance of the particle and also the enemy in < list> these are assigned in game1.cs.

My question is how do I access the enemies location from the < list> iset in game1 and make it availble in particle.cs. Borh particle.cs and enemy.cs are part of the Game namespae.

I have tried various approaches of assigning new instances of objects and assigning public set/get but no luck!

Thanks,
Paul.

Adding further detials:
I can add some real code when I'm back on my dev pc, but for now some further comments.

enemies.cs - is just a basic class with variables outlined - location, speed, size etc... although these are all defined in game1.cs with values.

In Game1.cs I have a list of all the enemies as well as another list of their positions.

I have a particle engine which is called in Game1.cs which in turn references particles.cs -> which is where I need to call the value of vector2 enemies location.

I tried calling the enemies location in particle.cs by establishin an instance of game1.cs but this is per particle and slows down game beyond running.

Which parts of the code shoudl I show?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长发绾君心 2024-12-04 17:49:27

如果您的粒子引擎需要访问敌人列表,那么您只需将该列表作为参数传递到粒子引擎的构造函数中即可。

我假设您的 Game1 中有类似的内容:

List<Enemy> enemies = new List<Enemy>();

因此更改您的粒子引擎构造函数以接收对 Enemy 列表的引用,并将其保存在私有变量中。

class ParticleEngine
{
    private List<Enemy> _enemies;

    public ParticleEngine(List<Enemy> enemies)
    {
        _enemies = enemies;
    }
}

在 Game1 中,当您构建粒子引擎对象时,传入敌人列表。

ParticleEngine particleEngine = new ParticleEngine(enemies);

现在在粒子引擎中,您可以通过您的成员_enemies访问敌人。由于它是对列表的引用,即使列表增长、缩小、更改,您仍然可以访问其“当前”成员。

编辑:重新阅读时,我发现您希望在您的 Particle.cs 中找到敌人的位置。这意味着每个粒子都需要知道每个敌人的位置,这可能是一个糟糕的设计。更好的解决方案是让您的 ParticleEngine(例如 ParticleManager)处理将单个粒子与敌人位置“关联”的问题。

If your particle engine needs access to the list of enemies, then you can just pass the list as a parameter in the constructor of your particle engine.

I assume you have something like this in your Game1:

List<Enemy> enemies = new List<Enemy>();

So change your particle engine constructor to receive a reference to a list of Enemy, and hold it in a private variable.

class ParticleEngine
{
    private List<Enemy> _enemies;

    public ParticleEngine(List<Enemy> enemies)
    {
        _enemies = enemies;
    }
}

and in Game1 when you construct your particle engine object, pass in the enemy list.

ParticleEngine particleEngine = new ParticleEngine(enemies);

Now in particle engine, you have access to the enemies through your member _enemies. And since it is a reference to the list, even if the list grows, shrinks, changes, you will still get access to its "current" members.

EDIT: On re-reading, I see you want the enemy location in your particle.cs. This would imply that each and every particle needs to know the position of each and every enemy, and this is likely a bad design. A better solution would be to have your ParticleEngine (think ParticleManager) handle "correlating" the individual particles to the enemy position.

空袭的梦i 2024-12-04 17:49:27

所以你说你尝试过为每个粒子提供对 Game1 的引用?我无法判断您是否这样做或尝试为每个粒子创建一个新的 Game1。后者会...很糟糕(我认为我从未在游戏中实际实例化过 Game1...那不是无限递归吗?)。但我不认为简单地将 Game1 的引用传递给每个粒子类有问题。在 Particle.cs 中:

private Game1 game;

public Particle(/*your constructor vars here*/, Game1 mainGame)
{
    //your existing constructor here
    this.game = mainGame;
}

现在,当您想要检查敌人的位置时:

private void CheckEnemyPositions() //or whatever you do
{
    foreach(Enemy enemy in game.Enemies)
    {
        //do something with enemy.Position
    }
}

这...不应该导致性能问题。至少,不是我所理解的。可以这么说,它只是告诉每个粒子如何找到 Game1.cs 的单个实例,以便它们可以使用敌人注册表。

So you say you've tried giving each particle a reference to Game1? I can't tell if you did that or tried to create a new Game1 for each particle. The latter would... be bad (I don't think I've ever actually instantiated Game1 inside the game... wouldn't that be infinite recursion?). But I don't see the problem with simply passing a reference to Game1 to each particle class. In particle.cs:

private Game1 game;

public Particle(/*your constructor vars here*/, Game1 mainGame)
{
    //your existing constructor here
    this.game = mainGame;
}

now, when you want to check positions of enemies:

private void CheckEnemyPositions() //or whatever you do
{
    foreach(Enemy enemy in game.Enemies)
    {
        //do something with enemy.Position
    }
}

this... shouldn't cause performance issues. At least, not as I understand it. It's simply telling each particle how to find your single instance of Game1.cs, so that they can use the enemy registry, so to speak.

梦中的蝴蝶 2024-12-04 17:49:27

这是我发表的关于将值从一个类传递到另一个类的文章:

...因此,在游戏中传递您想要的任何值(例如您的屏幕宽度)的一个非常简单的方法是创建一个“GlobalVariable”您可以在任何您想要的课程中使用。以下是如何完成这个简单的技巧。

首先创建一个新类并将其命名为 GlobalClass 如下所示:

class GlobalClass
{
}

现在添加一个具有公共访问方式的私有变量。这里我创建了两个变量,一个用于屏幕高度,另一个用于宽度。

class GlobalClass
{
    private static float screenWidth; 
    private static float screenHeight;

    //As I said I made this for an other post but the
    //concept is the same if you want to get 
    // a vector2 value try this: private static vector2 enemieposition;
    // or a vector2 List: private static List<Vector2> enemieposition; ... etc

    public static float ScreenWidth
    {
        get { return screenWidth; }
        set { screenWidth = value; }
    }

    public static float ScreenHeight
    {
        get { return screenHeight; }
        set { screenHeight = value; }
    }
}

你就完成了!通过这个类,您可以将这两个变量传递到游戏周围的任何地方。

要访问这些变量,请执行以下操作:

//In game1.cs
GlobalClass.ScreenWidth = graphics.PreferredBackBufferWidth;
GlobalClass.ScreenHeight = graphics.PreferredBackBufferHeight;

//In an other class.
int TextureHeight = GlobalClass.ScreenWidth;

就是这样,这样您就可以在任何地方传递您想要的任何值。

Here is a post I made about passing a value from a class to an other:

...So A really simple way to pass the value of anything you want (exemple your screen Width) arround your game is to create a "GlobalVariable" that you can use in ANY class you want. Here is how to accomplish this easy trick.

First Create a new Class and call it GlobalClass Like this:

class GlobalClass
{
}

Now add A private variable with a public way of accessing it. Here I create two variables, one for the Screen Height and the other for the Width.

class GlobalClass
{
    private static float screenWidth; 
    private static float screenHeight;

    //As I said I made this for an other post but the
    //concept is the same if you want to get 
    // a vector2 value try this: private static vector2 enemieposition;
    // or a vector2 List: private static List<Vector2> enemieposition; ... etc

    public static float ScreenWidth
    {
        get { return screenWidth; }
        set { screenWidth = value; }
    }

    public static float ScreenHeight
    {
        get { return screenHeight; }
        set { screenHeight = value; }
    }
}

And you are done! With this class you can pass those two variables anywhere arround your game.

To access those variables do this:

//In game1.cs
GlobalClass.ScreenWidth = graphics.PreferredBackBufferWidth;
GlobalClass.ScreenHeight = graphics.PreferredBackBufferHeight;

//In an other class.
int TextureHeight = GlobalClass.ScreenWidth;

So that's it, this way you can pass any value you want anywhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文