2013-08-20 39 views
1

嘿傢伙我得到了這個問題,我只是不能讓我的敵人旋轉我的角色 我一直在嘗試幾天,並詢問周圍,但沒有,所以這將是awsome如果你可以給我一些想法。如何讓敵人面對一個角色

這是我的敵人類現在這個代碼在這裏一切正常,但它確實我想要它面臨的鼠標,而不是我的性格

class Class1 
    { 
     Character character = new Character(); 
     EnemyShip blah = new EnemyShip(); 

     Texture2D texture; 
     Rectangle rectangle; 

     public Vector2 origin; 
     public Vector2 velocity; 
     public Vector2 position; 
     float rotation; 
     const float forwardvelocity = 1f; 
     float friction = 0.1f; 

     public Vector2 distance; 


     public void LoadContent(ContentManager Content) 
     { 
      texture = Content.Load<Texture2D>("Ships/WarShip"); 
      position = new Vector2(800, 300); 
     } 




     public void Update(GameTime gameTime) 
     { 
      MouseState mouse = Mouse.GetState(); 

      distance.X = mouse.X - position.X; // these two line are the one i want to 
      distance.Y = mouse.Y - position.Y; // change however when i change mouse.X to //say character.Position.X my enemy ship moves towards the top left corner of the screen //and not the character 

      rotation = (float)Math.Atan2(distance.Y, distance.X); 

      position = velocity + position; 

      velocity.X = (float)Math.Cos(rotation) * forwardvelocity; 
      velocity.Y = (float)Math.Sin(rotation) * forwardvelocity; 

      rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height); 
      origin = new Vector2(rectangle.Width/2, rectangle.Height/2); 
     } 

     public void Draw(SpriteBatch spriteBatch, GameTime gameTime) 
     { 
      spriteBatch.Draw(texture, position, null, Color.White, rotation, origin, 1f, SpriteEffects.None, 0); 
     } 
    } 
    } 

這是我的性格類

class Character 
    { 
     public Texture2D texture; 
     public float angle = 0; 
     public Vector2 velocity; 
     public Vector2 Position = new Vector2(0, 0); 
     public float forwardvelocity = 5; 
     public float friction = 0.03f; 
     public Vector2 origin; 

     public Rectangle sourcerectangle; 

     public void LoadContent(ContentManager Content) 
     { 
      texture = Content.Load<Texture2D>("Ships/charactership"); 

     } 

     public void Update(GameTime gameTime) 
     { 
      Position += velocity; 
      if (Keyboard.GetState().IsKeyDown(Keys.W)) 
      { 
       velocity.X = (float)Math.Cos(angle) * forwardvelocity; 
       velocity.Y = (float)Math.Sin(angle) * forwardvelocity; 
      } 
      if (Keyboard.GetState().IsKeyDown(Keys.S)) 
      { 
       velocity.X = -(float)Math.Cos(angle) * forwardvelocity; 
       velocity.Y = -(float)Math.Sin(angle) * forwardvelocity; 
      } 
       if (Keyboard.GetState().IsKeyDown(Keys.A)) angle -= 0.05f; 

       if (Keyboard.GetState().IsKeyDown(Keys.D)) angle += 0.05f; 

       else if (velocity != Vector2.Zero) 
       { 
        float i = velocity.X; 
        float j = velocity.Y; 

        velocity.X = i -= friction * i; 
        velocity.Y = j -= friction * j; 
       } 
      //-------------------------------------------------------------- 

     } 



     public void Draw(SpriteBatch spriteBatch) 
     { 

      sourcerectangle = new Rectangle(0, 0, texture.Width, texture.Height); 
      origin = new Vector2(texture.Width/2, texture.Height/2); 

      spriteBatch.Draw(texture, Position, sourcerectangle, Color.White, angle, origin, 1.0f, SpriteEffects.None, 0); 
     } 


    } 

} 
+0

「character.Position.X [...]朝左上角」 - 似乎字符位置是0/0。那爲什麼呢?你確定,人物移動正確嗎?你確定你正在檢查正確的角色對象的位置嗎?在這兩個註釋行上設置一個斷點,並檢查預期的字符位置是否與實際位置匹配。 – Corak

+0

我可以建議你使用矩陣而不是角度。它將使向3D系統的過渡變得容易得多,避免潛在的歐拉角問題,並且性能會比使用atan2,sin和cos更好。矩陣在2D(3x3)中的工作方式與在3D中一樣(4x4) – Skizz

+0

感謝您的建議虐待嘗試在我的下一個項目中實現它 – Luke

回答

0

你說「Class1」是你的敵人級別...

class Class1 
{ 
    Character character = new Character(); 
    EnemyShip blah = new EnemyShip(); 

Instanciate你身上的角色敵方階級毫無意義,我認爲你並沒有在你的敵人階級中引用正確的角色實例。

看來,你的敵人類中的字符變量永遠不會更新,有什麼可它總是在(0,0)

5

的原因,我注意到你有你的Class1一個Character變量。它被設置爲new Character()但是之後沒有其他事情可以做到。我猜這意味着你的實際Character是你Game內的某處,並在此Class1這個其他Character是一個完全不同的變量完全。所以很自然地,使用它的Position是沒有意義的。

因爲你的敵人Character變量取決於爲自己計算,通的依賴性:

public void Update(Character c, GameTime gameTime) 
{ 
    MouseState mouse = Mouse.GetState(); 
    distance.X = c.Position.X - position.X; 
    distance.Y = c.Position.Y - position.Y; 
... 

然後在頂層Game或什麼不是:

//your actual character 
Character c; 
... 
//in Game.Update 
c.Update(gameTime); 
c1.Update(c, gameTime); 

然後,您可以簡單地刪除中的那個Character character = new Character();,因爲它沒用。

有「lazier」方法,如單身人士和其他static ness相關的方法,但我不推薦這些。

+0

Scott我沒有詞語來表達我多麼感恩,我感覺自己像一個白癡(我)因爲錯過了將我的角色更新放在我的敵人課程中這麼簡單的事情,我想我只是假設在我的遊戲中更新它足夠了。感謝一束 – Luke

+0

沒問題。如果這解決了您的問題,請繼續並「接受」它。 – 2013-08-21 07:32:21