XNA - accessing vector2 across classes
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
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.
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.
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.
精彩评论