Stehe bei XNA aufm Schlauch

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von Eistee.

    Stehe bei XNA aufm Schlauch

    Ich versuch mich gerade an nem einfachen Programm, das Punkte über den Bildschirm schieben soll (Im Stil von Krissels "Dots". ^^).
    Allerdings werden keine Punkte gezeichnet, was mich stutzig macht. Könnt ihr mal über den Code gucken, ich seh den Fehler vor lauter Clusterfuck nicht.

    Game1.cs

    Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Diagnostics;
    5. using Microsoft.Xna.Framework;
    6. using Microsoft.Xna.Framework.Audio;
    7. using Microsoft.Xna.Framework.Content;
    8. using Microsoft.Xna.Framework.GamerServices;
    9. using Microsoft.Xna.Framework.Graphics;
    10. using Microsoft.Xna.Framework.Input;
    11. using Microsoft.Xna.Framework.Media;
    12. namespace MoreDots {
    13. /// <summary>
    14. /// This is the main type for your game
    15. /// </summary>
    16. public class Game1 : Microsoft.Xna.Framework.Game {
    17. GraphicsDeviceManager graphics;
    18. SpriteBatch spriteBatch;
    19. List<Entities.Dot> dots;
    20. public Game1() {
    21. graphics = new GraphicsDeviceManager(this);
    22. Content.RootDirectory = "Content";
    23. }
    24. /// <summary>
    25. /// Allows the game to perform any initialization it needs to before starting to run.
    26. /// This is where it can query for any required services and load any non-graphic
    27. /// related content. Calling base.Initialize will enumerate through any components
    28. /// and initialize them as well.
    29. /// </summary>
    30. protected override void Initialize() {
    31. dots = new List<Entities.Dot>();
    32. base.Initialize();
    33. }
    34. /// <summary>
    35. /// LoadContent will be called once per game and is the place to load
    36. /// all of your content.
    37. /// </summary>
    38. protected override void LoadContent() {
    39. // Create a new SpriteBatch, which can be used to draw textures.
    40. spriteBatch = new SpriteBatch(GraphicsDevice);
    41. // TODO: use this.Content to load your game content here
    42. }
    43. /// <summary>
    44. /// UnloadContent will be called once per game and is the place to unload
    45. /// all content.
    46. /// </summary>
    47. protected override void UnloadContent() {
    48. // TODO: Unload any non ContentManager content here
    49. }
    50. /// <summary>
    51. /// Allows the game to run logic such as updating the world,
    52. /// checking for collisions, gathering input, and playing audio.
    53. /// </summary>
    54. /// <param name="gameTime">Provides a snapshot of timing values.</param>
    55. protected override void Update(GameTime gameTime) {
    56. // Allows the game to exit
    57. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
    58. this.Exit();
    59. // TODO: Add your update logic here
    60. if (dots.Count < 60) {
    61. dots.Add(new Entities.Dot(this));
    62. //Trace.WriteLine("Dot added!");
    63. }
    64. foreach (Entities.Dot d in dots) {
    65. d.Update(gameTime);
    66. }
    67. dots.RemoveAll(isMarked);
    68. //Trace.WriteLine(dots[0].Position);
    69. this.Window.Title = "More Dots (" + dots.Count.ToString() + " Dots geladen)";
    70. base.Update(gameTime);
    71. }
    72. private static bool isMarked(Entities.Dot d) {
    73. return d.marked;
    74. }
    75. /// <summary>
    76. /// This is called when the game should draw itself.
    77. /// </summary>
    78. /// <param name="gameTime">Provides a snapshot of timing values.</param>
    79. protected override void Draw(GameTime gameTime) {
    80. GraphicsDevice.Clear(Color.Black);
    81. // TODO: Add your drawing code here
    82. spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.Opaque);
    83. foreach (Entities.Dot d in dots) {
    84. d.Draw(gameTime);
    85. }
    86. spriteBatch.End();
    87. base.Draw(gameTime);
    88. }
    89. }
    90. }


    Entities\Dot.cs

    Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using Microsoft.Xna.Framework;
    6. using Microsoft.Xna.Framework.Graphics;
    7. namespace MoreDots.Entities {
    8. class Dot : Microsoft.Xna.Framework.DrawableGameComponent {
    9. Vector2 Velocity;
    10. Vector2 Position;
    11. float Scale;
    12. Texture2D Texture;
    13. Vector2 Origin;
    14. Game1 g1;
    15. public bool marked;
    16. public Dot(Game1 g) : base(g) {
    17. g1 = g;
    18. this.Texture = g1.Content.Load<Texture2D>("dot");
    19. Random r = new Random();
    20. //this.Position = new Vector2((float)r.NextDouble() * g1.Window.ClientBounds.Width, (float)r.NextDouble() * g1.Window.ClientBounds.Height);
    21. this.Position = new Vector2(300f, 100f); //Debugwert, sollte eigentlich mitten im Fenster auftauchen
    22. this.Origin = new Vector2(Texture.Width, Texture.Height);
    23. this.Scale = MathHelper.Clamp((float)r.NextDouble(), 0, .8f);
    24. }
    25. public override void Update(GameTime gameTime) {
    26. this.Velocity += new Vector2(1f,1f) * (float)gameTime.ElapsedGameTime.TotalSeconds;
    27. this.Position += this.Velocity;
    28. //marked = !g1.Window.ClientBounds.Contains((int)this.Position.X, (int)this.Position.Y);
    29. marked = Position.X < 0 || Position.Y > g1.Window.ClientBounds.Height;
    30. }
    31. public void Draw(SpriteBatch sb) {
    32. //sb.Draw(Texture, Position, Texture.Bounds, Color.FromNonPremultiplied(255,50,50,255), 0f, Origin, Scale, SpriteEffects.None, 0f);
    33. sb.Draw(Texture, Position, Texture.Bounds, Color.Red, 0f, Origin, Scale, SpriteEffects.None, 0f);
    34. }
    35. }
    36. }


    dot.png: