EDN Admin
Well-known member
Hey! I have this class:
<pre class="prettyprint using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace WindowsGame2
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState key = new KeyboardState();
Texture2D ParticleTex;
List<Particle> particles = new List<Particle>();
int PartCount = 1;
float speedPart = 3.0f;
public static Random rnd = new Random();
public static Motion motion = new Motion();
public static Vector2 Center;
float randomCircular;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1200;
graphics.PreferredBackBufferHeight = 800;
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
Center = new Vector2((float)graphics.GraphicsDevice.Viewport.Width / 2.0f,
(float)graphics.GraphicsDevice.Viewport.Height / 2.0f);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
for (int particle = 0; particle < PartCount; particle++)
{
randomCircular = rnd.Next(0, 36000)/100;
ParticleTex = Content.Load<Texture2D>("pixel");
particles.Add(new Particle(1, ParticleTex,
new Vector2((float)Math.Sin(randomCircular)*speedPart, (float)Math.Cos(randomCircular)*speedPart)));
}
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
key = Keyboard.GetState();
if (key.IsKeyDown(Keys.Space))
PartCount++;
if (particles.Count < PartCount)
for (int particle = 0; particle < PartCount; particle++)
{
randomCircular = rnd.Next(0, 36000) / 100;
particles.Add(new Particle(1, ParticleTex,
new Vector2((float)Math.Sin(randomCircular) * speedPart,
(float)Math.Cos(randomCircular) * speedPart)));
}
foreach (Particle particle in particles)
{
particle.Update();
if (particle.Position.X + particle.myTexture.Width / 2 < 0)
{
particle.addVector.X *= -1; particle.Position.X = 0 - particle.myTexture.Width / 2;
}
if (particle.Position.X + particle.myTexture.Width * 1.5f > graphics.GraphicsDevice.Viewport.Width)
{
particle.addVector.X *= -1; particle.Position.X = graphics.GraphicsDevice.Viewport.Width -
particle.myTexture.Width * 1.5f;
}
if (particle.Position.Y + particle.myTexture.Height / 2 < 0)
{
particle.addVector.Y *= -1; particle.Position.Y = 0 - particle.myTexture.Width / 2;
}
if (particle.Position.Y + particle.myTexture.Height * 1.5f > graphics.GraphicsDevice.Viewport.Height)
{
particle.addVector.Y *= -1; particle.Position.Y = graphics.GraphicsDevice.Viewport.Height -
particle.myTexture.Height * 1.5f;
}
foreach (Particle particleC in particles)
{
if (particle.bb.Intersects(particleC.bb))
{
if (particleC == particle)
continue;
particle.addVector = motion.Distancer(particle.addVector, particle.Position, particleC.Position, -1.0f);
}
}
}
key = Keyboard.GetState();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.NonPremultiplied);
for (int particle = 0; particle < PartCount; particle++)
{
particles[particle].Draw(spriteBatch);
}
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
[/code]
<br/>
I want to modify "particles" list outside the Game1 class from this class:
<pre class="prettyprint using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
namespace WindowsGame2
{
class Particle
{
byte R;
byte G;
byte B;
float A = 255.0f;
//Vector3 ColorV;
public Texture2D myTexture;
public Rectangle bb;
public Vector2 Position = Game1.Center;
public Vector2 addVector = Vector2.Zero;
int type;
int temperature;
public Particle(int type, Texture2D texture, Vector2 motion)
{
this.type = type;
this.myTexture = texture;
Position = Position + new Vector2((float)Game1.rnd.Next(-50, 50), (float)Game1.rnd.Next(-50, 50));
this.addVector = motion;
R = (byte)Game1.rnd.Next(0, 255);
G = (byte)Game1.rnd.Next(0, 255);
B = (byte)Game1.rnd.Next(0, 255);
bb = new Rectangle((int)Position.X, (int)Position.Y,
5,5);
if (type == 1)
temperature = 10;
}
public void Update()
{
if (A>0)
A-=0.5f;
else
{
/* I WANT TO REMOVE A MEMBER FROM THE "particle" LIST HERE*/
}
Position += addVector*2;
}
public void Draw(SpriteBatch batch)
{
batch.Draw(myTexture, Position + new Vector2(myTexture.Width/2, myTexture.Height/2), new Color(R, G, B, (byte)A));
}
}
}[/code]
HOW???<br/>
View the full article
<pre class="prettyprint using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace WindowsGame2
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState key = new KeyboardState();
Texture2D ParticleTex;
List<Particle> particles = new List<Particle>();
int PartCount = 1;
float speedPart = 3.0f;
public static Random rnd = new Random();
public static Motion motion = new Motion();
public static Vector2 Center;
float randomCircular;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1200;
graphics.PreferredBackBufferHeight = 800;
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
Center = new Vector2((float)graphics.GraphicsDevice.Viewport.Width / 2.0f,
(float)graphics.GraphicsDevice.Viewport.Height / 2.0f);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
for (int particle = 0; particle < PartCount; particle++)
{
randomCircular = rnd.Next(0, 36000)/100;
ParticleTex = Content.Load<Texture2D>("pixel");
particles.Add(new Particle(1, ParticleTex,
new Vector2((float)Math.Sin(randomCircular)*speedPart, (float)Math.Cos(randomCircular)*speedPart)));
}
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
key = Keyboard.GetState();
if (key.IsKeyDown(Keys.Space))
PartCount++;
if (particles.Count < PartCount)
for (int particle = 0; particle < PartCount; particle++)
{
randomCircular = rnd.Next(0, 36000) / 100;
particles.Add(new Particle(1, ParticleTex,
new Vector2((float)Math.Sin(randomCircular) * speedPart,
(float)Math.Cos(randomCircular) * speedPart)));
}
foreach (Particle particle in particles)
{
particle.Update();
if (particle.Position.X + particle.myTexture.Width / 2 < 0)
{
particle.addVector.X *= -1; particle.Position.X = 0 - particle.myTexture.Width / 2;
}
if (particle.Position.X + particle.myTexture.Width * 1.5f > graphics.GraphicsDevice.Viewport.Width)
{
particle.addVector.X *= -1; particle.Position.X = graphics.GraphicsDevice.Viewport.Width -
particle.myTexture.Width * 1.5f;
}
if (particle.Position.Y + particle.myTexture.Height / 2 < 0)
{
particle.addVector.Y *= -1; particle.Position.Y = 0 - particle.myTexture.Width / 2;
}
if (particle.Position.Y + particle.myTexture.Height * 1.5f > graphics.GraphicsDevice.Viewport.Height)
{
particle.addVector.Y *= -1; particle.Position.Y = graphics.GraphicsDevice.Viewport.Height -
particle.myTexture.Height * 1.5f;
}
foreach (Particle particleC in particles)
{
if (particle.bb.Intersects(particleC.bb))
{
if (particleC == particle)
continue;
particle.addVector = motion.Distancer(particle.addVector, particle.Position, particleC.Position, -1.0f);
}
}
}
key = Keyboard.GetState();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.NonPremultiplied);
for (int particle = 0; particle < PartCount; particle++)
{
particles[particle].Draw(spriteBatch);
}
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
[/code]
<br/>
I want to modify "particles" list outside the Game1 class from this class:
<pre class="prettyprint using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
namespace WindowsGame2
{
class Particle
{
byte R;
byte G;
byte B;
float A = 255.0f;
//Vector3 ColorV;
public Texture2D myTexture;
public Rectangle bb;
public Vector2 Position = Game1.Center;
public Vector2 addVector = Vector2.Zero;
int type;
int temperature;
public Particle(int type, Texture2D texture, Vector2 motion)
{
this.type = type;
this.myTexture = texture;
Position = Position + new Vector2((float)Game1.rnd.Next(-50, 50), (float)Game1.rnd.Next(-50, 50));
this.addVector = motion;
R = (byte)Game1.rnd.Next(0, 255);
G = (byte)Game1.rnd.Next(0, 255);
B = (byte)Game1.rnd.Next(0, 255);
bb = new Rectangle((int)Position.X, (int)Position.Y,
5,5);
if (type == 1)
temperature = 10;
}
public void Update()
{
if (A>0)
A-=0.5f;
else
{
/* I WANT TO REMOVE A MEMBER FROM THE "particle" LIST HERE*/
}
Position += addVector*2;
}
public void Draw(SpriteBatch batch)
{
batch.Draw(myTexture, Position + new Vector2(myTexture.Width/2, myTexture.Height/2), new Color(R, G, B, (byte)A));
}
}
}[/code]
HOW???<br/>
View the full article