2014-09-02 13 views
0

我使用MonoGame開發Windows 8商店應用程序/遊戲(不適用於手機)。我也在XAML中使用這個項目,但是這個問題不是與XAML相關的。將Sprite移動到它所面向的方向,爲什麼我的嘗試不能正常工作?

我想讓一艘船朝它所面對的方向移動,並且可以通過按左右鍵旋轉船來改變方向。向上鍵用於將船舶朝向它的方向。

當遊戲開始時,船的圖像/紋理最初面朝下(想象一下箭頭朝下),所以當我按下向上鍵時,我想向下移動它,但是它向右移動。我已經收集到這是與輪換有關的事情嗎?

我已經google瞭如何解決我的問題,並嘗試了各種方法,這是我最好的嘗試,但它不工作。

我父精靈類:

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Ship_Meteor_Game_V1 
{ 
    abstract class cSprite 
    { 
     #region Properties 
     Texture2D spriteTexture; 
     Rectangle spriteRectangle; 
     Vector2 spritePosition; 

     public Texture2D SpriteTexture { get { return spriteTexture; } set { spriteTexture = value; } } 
     public Rectangle SpriteRectangle { get { return spriteRectangle; } set { spriteRectangle = value; } } 
     public Vector2 SpritePosition { get { return spritePosition; } set { spritePosition = value; } } 
     #endregion 

     abstract public void Update(GameTime gameTime); 

     abstract public void Draw(SpriteBatch spriteBatch); 

    } 
} 

我的播放器類:

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Ship_Meteor_Game_V1 
{ 
    class cPlayer : cSprite 
    { 
     Vector2 origin; 
     float rotation; 
     float speed; 

     public cPlayer() 
     { 
     } 

     public cPlayer(Texture2D newTexture2D, Vector2 newPosition) 
     { 
      SpriteTexture = newTexture2D; 
      SpritePosition = newPosition; 
      speed = 2; 
      rotation = 0; 
     } 
     public override void Update(GameTime gameTime) 
     { 

      if(Keyboard.GetState().IsKeyDown(Keys.Right)) 
      { 
       rotation = rotation + 0.1f; 
      } 

      if(Keyboard.GetState().IsKeyDown(Keys.Left)) 
      { 
       rotation = rotation - 0.1f; 
      } 

      if (Keyboard.GetState().IsKeyDown(Keys.Up)) 
      { 
       Move(); 
      } 
     } 

     public override void Draw(SpriteBatch spriteBatch) 
     { 
      spriteBatch.Draw(SpriteTexture, SpritePosition, null, Color.White, rotation, origin, 0.2f, SpriteEffects.None, 0f); 
     } 
     public void Move() 
     { 
      Vector2 direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)); 
      direction.Normalize(); 
      SpritePosition = SpritePosition + (direction * speed); 
     } 
    } 
} 

基本上我想要一艘船在其面對的方向移動,而是它不斷地在任何方向橫盤它正面臨着,我不知道如何解決它。如果我擁有它,我可以向你展示任何額外的課程/代碼。

PS:任何人都知道可以接受鼠標和鍵盤輸入的變量/類型?

回答

2

看不出有什麼明顯的錯誤代碼。

猜測會是你在內容管理器中使用的船舶圖形不是朝上的。

如果是這種情況,您必須在圖像編輯器中將其旋轉或修改開始旋轉。

我敢打賭,它是正確的,這將是一個可以理解的混亂,因爲在常規的數學中,弧度0將面向右側。然而在Xna中0已經到了。

相關問題