这就是事情。我在一个团队中,我们正在尝试开发类似小行星的游戏,而我的游戏部分则是与图像相关的一切。我理解一切,但缩放。我的问题是,当我缩放图像时,它会从最初分配的位置稍微移动。但是,我希望它在缩放时保持在同一个地方。尝试用矩形替换向量,但它不起作用。这是我的代码:
公共类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时,如下所示:
明白我的意思?每次改变比例时我都需要它在同一个位置。如果有人帮助我,我会很高兴。
答案 0 :(得分:4)
作为spriteBatch.Draw方法的一部分,您可以指定图像的原点。 (“new Vector(0,0)”)
目前,您似乎将其设置为图像的左上角,这意味着任何缩放工作也将从图像的左上角“增长”。
尝试将原点设置为图像的中心。
这将确保图像正确缩放,而不会因缩放而移动/移位。
答案 1 :(得分:0)
如果您正在使用碰撞检测,请确保您还使用比例参数进行计算。精灵的高度和宽度仍然相同,无论你如何缩放它。如果使用圆形碰撞,也可以改变半径。