事情就是这样。我在一个团队中,我们正在尝试开发类似小行星的游戏,而我的游戏部分是与图像相关的所有内容。我明白一切,但缩放。我的问题是,当我缩放图像时,它会从最初分配的位置略微移动。但是,我希望它在缩放后保持在同一个地方。尝试用矩形替换向量,但没有用。这是我的代码:
公共类 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 时,如下所示:
当比例为 1.3f 时,如下所示:
明白了吗?每次更改比例时,我都需要它位于同一位置。如果有人帮助我,我将不胜感激。