仅当通过鼠标单击图像时,才需要移动图像。
我有这个运动:
if (Mouse.GetState().LeftButton == ButtonState.Pressed &&
previousMouseState.LeftButton != ButtonState.Pressed)
{
xpos = rnd.Next(windowWidth - texture.Width);
ypos = rnd.Next(windowHeight - texture.Height);
}
previousMouseState = Mouse.GetState();
但是我需要某种复合的
if
逻辑来使它仅在单击纹理时才移动。 最佳答案
我假设您的texture
对象不仅仅是一个Texture2D
。
绘制精灵时,您需要同时具有位置和大小。这样的事情应该起作用:
var currentMouseState = Mouse.GetState();
if (currentMouseState.LeftButton == ButtonState.Pressed &&
previousMouseState.LeftButton != ButtonState.Pressed)
{
Vector2 mousePosition = new Vector2(currentMouseState.X,currentMouseState.Y);
mousePosition-=sprite.Position;
/// .Bounds is a property of Texture2D, and returns a Rectangle() struct
var spriteWasClicked = sprite.Bounds.Contains(mousePosition.X,mousePosition.Y);
if(spriteWasClicked)
{
xpos = rnd.Next(windowWidth - texture.Width);
ypos = rnd.Next(windowHeight - texture.Height);
// update sprite position with to xpos,ypos here.
}
}
previousMouseState = currentMouseState;
关于c# - 在C#XNA游戏中用鼠标单击时移动图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7287039/