簡體   English   中英

XNA旋轉和移動精靈

[英]XNA Rotate and Move Sprite

public class dgc_Spaceship : DrawableGameComponent
{

    Texture2D shipTexture;
    Rectangle spriteRectangle;
    SpriteBatch spriteBatch;
    //
    bool horizisSelected = false;
    bool rotateSelected = false;
    bool fireisSelected = false;

    SpriteFont Count;

    //Controls for ship
    dgc_TriggerHorizontal triggerHorizantal;



    Vector2 spritePosition;
    Vector2 spriteOrigin;
    Vector2 direction;


    float rotation = 0.1f;
    Random x, y;


   List<Missle> missleCollection = new List<Missle>();
   List<Asteriod> asteriodCollection = new List<Asteriod>();


    Missle missle;

    Asteriod asteriod;

    public dgc_Spaceship(Game game)
        : base(game)
    {
        // TODO: Construct any child components here


        triggerHorizantal = new dgc_TriggerHorizontal(game);
        triggerHorizantal.triggerHorizantal += new EventHandler(triggerHorizantal_Handler);
        triggerHorizantal.triggerRotate +=new EventHandler(triggerHorizantal_triggerRotate);
        triggerHorizantal.triggerFire +=new EventHandler(triggerHorizantal_triggerFire);
        Game.Components.Add(triggerHorizantal);
        missle = new Missle(game);
        asteriod = new Asteriod(game);

        for (int i = 0; i < 2; i++)
        {
            missleCollection.Add(new Missle(game));
        }


        for (int i = 0; i < 2; i++)
        {
            asteriodCollection.Add(new Asteriod(game));
        }


      }




    /// <summary>
    /// Allows the game component to perform any initialization it needs to before starting
    /// to run.  This is where it can query for any required services and load content.
    /// </summary>
    public override void Initialize()
    {
        // TODO: Add your initialization code here

        base.Initialize();
    }

    protected override void LoadContent()
    {

        spriteBatch = new SpriteBatch(GraphicsDevice);
        shipTexture = Game.Content.Load<Texture2D>(@"Sprites\spaceship");
        spritePosition = new Vector2(450,320);
        missle.LoadContent();

        Count = Game.Content.Load<SpriteFont>(@"Sprites\Count");
        //
        x = new Random();
        y = new Random();

        foreach (Asteriod display in asteriodCollection)
        {
            display.LoadContent();

            int xVec = x.Next(0, 200), yVec = y.Next(0, 200);
            display.position = new Vector2(xVec, yVec);
        }





        base.LoadContent();
    }

    /// <summary>
    /// Allows the game component to update itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    public override void Update(GameTime gameTime)
    {
        // TODO: Add your update code here

        spriteRectangle = new Rectangle((int)spritePosition.X, (int)spritePosition.Y, shipTexture.Width, shipTexture.Height);

        spriteOrigin = new Vector2(spriteRectangle.Width / 2, spriteRectangle.Height / 2);

        direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));


        if (horizisSelected)
        {
           spritePosition.X-= 0.5f;
           spritePosition.Y-= 0.5f;

        }


        if (rotateSelected)
        {               
            rotation += 0.1f;
           // missle.rotateSprite(); 
            spritePosition += direction * 20 * gameTime.ElapsedGameTime.Milliseconds;
        }

        if (fireisSelected)
        {
            missle.update();
        }



        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {

        spriteBatch.Begin();
      //  missle.draw(spriteBatch,rotation);
        spriteBatch.Draw(shipTexture,spritePosition,null,Color.White,rotation,spriteOrigin,1f,SpriteEffects.None,0);
      //  spriteBatch.DrawString(Count,asteriodCollection.Count.ToString(),new Vector2(100,100),Color.Red);
        foreach (Asteriod display in asteriodCollection)
        {
            display.Draw(spriteBatch);
        }
         spriteBatch.End();

        base.Draw(gameTime);
    }


    public void triggerHorizantal_Handler(object sender, EventArgs e)
    {
        //Do Stuff
        horizisSelected = !horizisSelected;

    }

    public void triggerHorizantal_triggerRotate(object sender, EventArgs e)
    {
        //Do Stuff
        rotateSelected = !rotateSelected;
    }

    public void triggerHorizantal_triggerFire(object sender, EventArgs e)
    {
        //Do Stuff
        fireisSelected = !fireisSelected;
    }




    }
}

我的問題:

我似乎無法弄清楚。 屏幕上有2個功能按鈕。 單擊后,船將順時針旋轉,另一艘船將其向前移動。 經過一番搜索,我找到了Math.Sin / Cos公式,但是我認為我沒有正確實現它。

我需要做的是沿旋轉方向移動船。

工作方式:用戶將按下“旋轉”按鈕,它將順時針旋轉船。 當其處於用戶想要的位置時,他們按下旋轉按鈕以將其停止在該特定方向上。

按下水平按鈕,它將使船朝該方向前進。

您的代碼建議您在選擇“旋轉”時將精靈沿所需的方向移動,這需要在選擇“水平”時進行,如果選擇了“旋轉”,則應更改方向(不是每幀)

編輯:(最多5分鍾)

    if (horizisSelected)
    {
       //spritePosition.X-= 0.5f;
       //spritePosition.Y-= 0.5f;
       spritePosition += direction;// * 20 * gameTime.ElapsedGameTime.Milliseconds;

    }


    if (rotateSelected)
    {               
        rotation += 0.1f;
       // missle.rotateSprite(); 

        direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));
        //spritePosition += direction * 20 * gameTime.ElapsedGameTime.Milliseconds;
    }

如果您的飛船可能是由於經過的時間(毫秒)而飛走的,則您必須進行調試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM