XNA中的标度问题
事情是这样的。我所在的团队正试图开发一款类似小行星的游戏,而我在游戏中的角色就是与图像相关的一切。我什么都懂,但不懂。我的问题是,当我缩放图像时,它会稍微偏离最初指定的位置。但是,当它被缩放时,我希望它保持在原来的位置。尝试用矩形替换向量,但无效。这是我的密码: 公共类游戏1:Microsoft.Xna.Framework.Game { 纹理2D纹理XNA中的标度问题,xna,Xna,事情是这样的。我所在的团队正试图开发一款类似小行星的游戏,而我在游戏中的角色就是与图像相关的一切。我什么都懂,但不懂。我的问题是,当我缩放图像时,它会稍微偏离最初指定的位置。但是,当它被缩放时,我希望它保持在原来的位置。尝试用矩形替换向量,但无效。这是我的密码: 公共类游戏1:Microsoft.Xna.Framework.Game { 纹理2D纹理 Vector2 position = new Vector2(525, 325); float scale = 0.3f;
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);
}
} }
Vector2位置=新的Vector2(525325);
浮标度=0.3f;
纹理2d纹理2;
图形管理器图形;
SpriteBatch SpriteBatch;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
//Windows Phone的帧速率默认为30 fps。
targetExpressedTime=时间跨度(333);
//在锁定状态下延长电池寿命。
InactiveSleepTime=时间跨度(从秒开始)(1);
}
///
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
///
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
base.Initialize();
}
///
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
///
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
纹理=Content.Load(“Spacenebula_Backround_”);
texture2=Content.Load(“PressureSphere”);
//TODO:使用此.Content在此处加载游戏内容
}
///
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
///
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
///
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
///
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
//TODO:在此处添加更新逻辑
更新(游戏时间);
}
///
///这就是所谓的比赛应该平局的时候。
///
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
绘制(纹理,矢量2.0,颜色,小麦);
spriteBatch.Draw(纹理2,位置,空值,颜色,小麦,0,新向量2(0,0),比例,SpriteTeffects.None,0);
spriteBatch.End();
//TODO:在此处添加图形代码
基础。抽签(游戏时间);
}
} }
最小刻度为0.3f,最大刻度为1.3f
当比例为0.3f时,如下所示:
当比例为1.3f时,如下所示:
明白我的意思吗?每次刻度改变时,我需要它在相同的位置。如果有人帮助我,我将不胜感激 作为spriteBatch.Draw方法的一部分,可以指定图像的原点。(“新向量(0,0)”) 目前,您似乎将其设置为图像的左上角,这意味着任何缩放工作也将从图像的左上角“增长” 尝试将原点设置为图像的中心
这将确保图像正确缩放,而不会因缩放而移动/移位。作为spriteBatch.Draw方法的一部分,您可以指定图像的原点。(“新向量(0,0)”) 目前,您似乎将其设置为图像的左上角,这意味着任何缩放工作也将从图像的左上角“增长” 尝试将原点设置为图像的中心
这将确保图像正确缩放,而不会因缩放而移动/移位。如果使用碰撞检测,请确保在计算中也使用缩放参数。无论您如何缩放,精灵的高度和宽度都是相同的。如果使用圆碰撞,还可以更改半径。如果使用碰撞检测,请确保在计算中也使用比例参数。无论您如何缩放,精灵的高度和宽度都是相同的。如果使用圆碰撞,也可以更改半径