基本上我在大学里有一个任务,我必须在屏幕上显示按下时从屏幕上的xbox控制器按下游戏手柄的按钮。
我有图像来表示游戏手柄按下和所有坐标。我可以在按下按钮时显示按钮...但是。我必须在dPad上按下对角按钮才能显示,没有相应的按钮你需要按“NOT”apearing。我还需要让触发器淡入淡出,因为它们会慢慢被推入最大值。
像这样:Image of how the program should look when keys are pressed
我遇到的问题是,当我向上和向右按压时...所有三个图像都会在屏幕上亮起(而不仅仅是对角线向上/向右图像)。
我基本上在我的绘图方法中设置了IF语句,以便在按下时绘制特定的图像,并用不同的颜色为它们着色,如下所示:
spriteBatch.Begin();
spriteBatch.Draw(under, underPos, Color.White);
if (GamePad.GetState(PlayerIndex.One).Triggers.Right == 1.0f)
{
spriteBatch.Draw(rightTrigger, rTriggerPos, Color.White);
}
if (GamePad.GetState(PlayerIndex.One).Triggers.Left == 1.0f)
{
spriteBatch.Draw(leftTrigger, lTriggerPos, Color.White);
}
if (GamePad.GetState(PlayerIndex.One).DPad.Right == ButtonState.Pressed)
{
spriteBatch.Draw(right, rightPos, Color.Red);
}
if (GamePad.GetState(PlayerIndex.One).DPad.Left == ButtonState.Pressed)
{
spriteBatch.Draw(left, leftPos, Color.Blue);
}
if (GamePad.GetState(PlayerIndex.One).DPad.Up == ButtonState.Pressed)
{
spriteBatch.Draw(up, upPos, Color.Yellow);
}
if (GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Pressed)
{
spriteBatch.Draw(down, downPos, Color.Green);
}
if (GamePad.GetState(PlayerIndex.One).DPad.Right == ButtonState.Pressed)
if (GamePad.GetState(PlayerIndex.One).DPad.Up == ButtonState.Pressed)
{
spriteBatch.Draw(upRight, upRightPos, Color.Orange);
}
if (GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Pressed)
if (GamePad.GetState(PlayerIndex.One).DPad.Right == ButtonState.Pressed)
{
spriteBatch.Draw(downRight, dRightPos, Color.Brown);
}
if (GamePad.GetState(PlayerIndex.One).DPad.Up == ButtonState.Pressed)
if (GamePad.GetState(PlayerIndex.One).DPad.Left == ButtonState.Pressed)
{
spriteBatch.Draw(upLeft, upLeftPos, Color.Cyan);
}
if (GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Pressed)
if (GamePad.GetState(PlayerIndex.One).DPad.Left == ButtonState.Pressed)
{
spriteBatch.Draw(downLeft, dLeftPos, Color.Purple);
}
spriteBatch.End();
我的问题是我不知道如何将两个IF语句组合成只显示一个对角线图像,你可以同时组合两个IF语句或游戏按下吗?...以及如何获得触发器慢慢增加颜色和透明度......任何人都可以帮助我吗?
答案 0 :(得分:0)
使用&&可以在If语句中传递多个条件对于AND和||对于OR。
如果仅为了清楚起见,我会使用else而不是多个。 只需使用代码中的示例代码段来执行此操作:
else if (GamePad.GetState(PlayerIndex.One).DPad.Up == ButtonState.Pressed && GamePad.GetState(PlayerIndex.One).DPad.Left == ButtonState.Pressed)
{
spriteBatch.Draw(upLeft, upLeftPos, Color.Cyan);
}
如果同时按下UP和LEFT,这将使条件通过。
希望这会让你朝着正确的方向前进。