v XNA mám komponentu kulička, která se odráží v okně. v game1 jsem chtěl, aby si uživatel mohl vytvořit kuliček, kolik chce.
List<kulička> k = new List<kulička>();
protected override void Initialize()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Window.AllowUserResizing = true;
k.Add(new kulička(this,Content,spriteBatch,GraphicsDevice));
foreach (kulička kk in k)
{
Components.Add(kk);
}
base.Initialize();
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (Keyboard.GetState().IsKeyDown(Keys.PageUp))
{
k.Add(new kulička(this, Content, spriteBatch, GraphicsDevice));
foreach (kulička kk in k)
{
Components.Add(kk); // zde nastane chyba
}
}
base.Update(gameTime);
}
System.ArgumentException was unhandled
Message=Cannot add the same game component to a game component collection multiple times.
Source=Microsoft.Xna.Framework.Game
StackTrace:
at Microsoft.Xna.Framework.GameComponentCollection.InsertItem(Int32 index, IGameComponent item)
at System.Collections.ObjectModel.Collection`1.Add(T item)
at Breakout_XNA.Game1.Update(GameTime gameTime) in C:\Users\JoJo\Documents\Visual Studio 2010\Projects\Breakout_XNA\Breakout_XNA\Breakout_XNA\Game1.cs:line 61
at Microsoft.Xna.Framework.Game.Tick()
at Microsoft.Xna.Framework.Game.HostIdle(Object sender, EventArgs e)
at Microsoft.Xna.Framework.GameHost.OnIdle()
at Microsoft.Xna.Framework.WindowsGameHost.RunOneFrame()
at Microsoft.Xna.Framework.WindowsGameHost.ApplicationIdle(Object sender, EventArgs e)
at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle(Int32 grfidlef)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Microsoft.Xna.Framework.WindowsGameHost.Run()
at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
at Microsoft.Xna.Framework.Game.Run()
at Breakout_XNA.Program.Main(String[] args) in c:\users\jojo\documents\visual studio 2010\Projects\Breakout_XNA\Breakout_XNA\Breakout_XNA\Program.cs:line 15
InnerException:
|