0

事情就是这样。我在一个团队中,我们正在尝试开发类似小行星的游戏,而我的游戏部分是与图像相关的所有内容。我明白一切,但缩放。我的问题是,当我缩放图像时,它会从最初分配的位置略微移动。但是,我希望它在缩放后保持在同一个地方。尝试用矩形替换向量,但没有用。这是我的代码:

公共类 Game1:Microsoft.Xna.Framework.Game { Texture2D 纹理;

    Vector2 position = new Vector2(525, 325);


    float scale = 0.3f;



    Texture2D texture2;

    GraphicsDeviceManager graphics;

    SpriteBatch spriteBatch;

    public Game1()
    {

        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        // Frame rate is 30 fps by default for Windows Phone.
        TargetElapsedTime = TimeSpan.FromTicks(333333);

        // Extend battery life under lock.
        InactiveSleepTime = TimeSpan.FromSeconds(1);
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        texture = Content.Load<Texture2D>("Spacenebula_Backround_");


        texture2 = Content.Load<Texture2D>("PressureSphere");


        // TODO: use this.Content to load your game content here
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();

        spriteBatch.Draw(texture, Vector2.Zero, Color.Wheat);

        spriteBatch.Draw(texture2,position, null, Color.Wheat, 0, new Vector2(0, 0), scale, SpriteEffects.None, 0);



        spriteBatch.End();

        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }
} }

最小刻度为 0.3f,最大为 1.3f。

当比例为 0.3f 时,如下所示:

比例 = 0.3f

当比例为 1.3f 时,如下所示:

比例 = 1.3f

明白了吗?每次更改比例时,我都需要它位于同一位置。如果有人帮助我,我将不胜感激。

4

2 回答 2

4

作为 spriteBatch.Draw 方法的一部分,您可以指定图像的原点。(“新向量(0,0)”)

目前,您似乎将其设置为图像的左上角,这意味着任何缩放工作也将从图像的左上角“增长”。

尝试将原点设置为图像的中心。

这将确保图像正确缩放,而不会因缩放而移动/移位。

于 2013-03-05T23:24:37.697 回答
0

如果您使用碰撞检测,请确保在计算中也使用比例参数。无论您如何缩放,精灵的高度和宽度仍然相同。如果使用圆形碰撞,也可以更改半径。

于 2013-03-06T14:47:57.923 回答