2011-11-27 86 views
1

我只是搞亂了XNA,試圖獲得一個簡單的播放器類,可以通過鍵盤控制移動。儘管我現在似乎無法按照我現在設置的方式來繪製它。我沒有看到我在做什麼錯誤。爲什麼我的精靈不會畫? (XNA)

這裏的演員基本類:

using System; 
using System.Collections.Generic; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 

namespace RunnerTEst 
{ 
    class Actor 
    { 
     public static List<Actor> actors; 

     public Texture2D texture; 

     protected Vector2 position; 
     protected float rotation; 
     protected float scale; 
     protected Color color; 

    #region Constructors 

     static Actor() 
     { 
      actors = new List<Actor>(); 
     } 

     public Actor(Texture2D texture) 
     { 
      actors.Add(this); 
      this.texture = texture; 
      this.position = Vector2.Zero; 
      this.rotation = 0f; 
      this.scale = 1f; 
     } 

    #endregion 

    #region Properties 

     public Vector2 Position 
     { 
      get { return this.position; } 
      set { this.position = value; } 
     } 

     public Vector2 Origin 
     { 
      get { return new Vector2(this.position.X + this.texture.Width/2, 
            this.position.Y + this.texture.Height/2); } 
     } 

     public float Rotation 
     { 
      get { return this.rotation; } 
      set { this.rotation = value; } 
     } 

     public float Scale 
     { 
      get { return this.scale; } 
      set { this.scale = value; } 
     } 

     public Color Color 
     { 
      get { return this.color; } 
      set { this.color = value; } 
     } 

     public float Width 
     { 
      get { return this.texture.Width; } 
     } 

     public float Height 
     { 
      get { return this.texture.Height; } 
     } 

    #endregion 

    #region Methods 

     public virtual void Update(GameTime gameTime) 
     { 

     } 

     public virtual void Draw(SpriteBatch spriteBatch) 
     { 
      spriteBatch.Draw(this.texture, this.position, this.color); 
     } 

    #endregion 
    } 
} 

Player類:

using System; 
using System.Collections.Generic; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 

namespace RunnerTEst 
{ 
    class Player : Actor 
{ 
    private const float speed = 5f; 

    protected Vector2 velocity; 

    private static KeyboardState currState, prevState; 

    static Player() 
    { 
     currState = prevState = Keyboard.GetState(); 
    } 

    public Player(Texture2D texture, Vector2 position) 
     : base(texture) 
    { 
     this.position = position; 
     this.velocity = Vector2.Zero; 
    } 

    public override void Update(GameTime gameTime) 
    { 
     this.HandleInput(); 

     this.position += this.velocity; 

     foreach (Actor actor in Actor.actors) 
     { 
      if (this == actor) 
       continue; 
      this.CheckCollision(actor); 
     } 

     base.Update(gameTime); 
    } 

    public override void Draw(SpriteBatch spriteBatch) 
    { 
     base.Draw(spriteBatch); 
    } 

    public void CheckCollision(Actor actor) 
    { 
     //--left/right sides 
     if (this.position.X + this.Width > actor.Position.X) //right of this hitting left actor 
     { 
      this.position.X = actor.Position.X - this.Width; 
     } 
     else if (this.position.X < (actor.Position.X + actor.Width))//left this hit right actor 
     { 
      this.position.X = actor.Position.X + actor.Width; 
     } 

     //--top/bottom 
     if (this.position.Y + this.Height > actor.Position.Y) //this bottom hit actor top 
     { 
      this.position.Y = actor.Position.Y - this.Width; 
     } 
     else if (this.position.Y < (actor.Position.Y + actor.Height))//this top hit actor bottom 
     { 
      this.position.Y = actor.Position.Y + actor.Height; 
     } 

     //TODO: check screen bounds 
    } 

    public void HandleInput() 
    { 
     currState = Keyboard.GetState(); 

     if (currState.IsKeyDown(Keys.W)) 
     { 
      this.velocity.Y = -speed; 
     } 
     else if (currState.IsKeyDown(Keys.S)) 
     { 
      this.velocity.Y = speed; 
     } 
     else 
     { 
      this.velocity.Y = 0f; 
     } 

     if (currState.IsKeyDown(Keys.A)) 
     { 
      this.velocity.X = -speed; 
     } 
     else if (currState.IsKeyDown(Keys.D)) 
     { 
      this.velocity.X = speed; 
     } 
     else 
     { 
      this.velocity.X = 0f; 
     } 

     prevState = currState; 
    } 
} 
} 

,最後,遊戲:

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 RunnerTEst 
{ 
public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 

    Texture2D playerTexture; 
    Player me; 

    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 
    } 

    protected override void Initialize() 
    { 


     base.Initialize(); 
    } 

    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

     playerTexture = Content.Load<Texture2D>(@"Textures\player"); 
     me = new Player(playerTexture, new Vector2(200, 200)); 
    } 

    protected override void UnloadContent() 
    { 
     // TODO: Unload any non ContentManager content here 
    } 

    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     me.Update(gameTime); 


     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     spriteBatch.Begin(); 

     me.Draw(spriteBatch); 

     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 
} 
} 

預先感謝您的時間!

回答

4

我翻遍了它,它看起來像你永遠不會定義Actor的顏色屬性。這意味着color變量默認爲黑色,這意味着賦予Draw線(spriteBatch.Draw(this.texture, this.position, this.color);)的色調爲黑色,隱藏了精靈。

只需將Actor的顏色設置爲白色(我只是將其設置在me = new Player(playerTexture, new Vector2(200, 200));下面)。

所以新LoadContent會是什麼樣子:

protected override void LoadContent() 
{ 
    // Create a new SpriteBatch, which can be used to draw textures. 
    spriteBatch = new SpriteBatch(GraphicsDevice); 

    playerTexture = Content.Load<Texture2D>(@"Textures\player"); 
    me = new Player(playerTexture, new Vector2(200, 200)); 
    me.Color = Color.White; 
} 

希望幫助,
最大

+0

AHA!非常感謝。我以爲我在構造函數中將它初始化爲白色,但我想我錯過了它! – TNTisCOOL