XNA 4 Express Studio中的宽度和高度问题 [英] Width and height problem in XNA 4 Express Studio

查看:110
本文介绍了XNA 4 Express Studio中的宽度和高度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建XNA迷宫游戏,但是一开始我就遇到了问题.这是我的更新方法:

I''m trying to create a XNA maze game, but at the very beginning, I got a problem. Here''s my Update method:

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
    this.Exit();
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left))
    ballPosition.X -= ballSpeed;
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right))
    ballPosition.X += ballSpeed;
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down))
    ballPosition.Y += ballSpeed;
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up))
    ballPosition.Y -= ballSpeed;
if (ballPosition.X >= width)
    ballPosition.X = width;
if (ballPosition.X <= 0)
    ballPosition.X = 0;
if (ballPosition.Y >= height)
    ballPosition.Y = height;
if (ballPosition.Y <= 0)
    ballPosition.Y = 0;
base.Update(gameTime);


但是,当我运行游戏时,球不会在x = 0或y = 0时熄灭,但是在其他时候,它不在我的游戏屏幕之外.
P. S.的宽度和高度是在Initialize方法中使用ballPosition默认值指定的:


But, when I run the game, ball won''t go out at x=0 or y=0, but at other points it goes out of my game screen.
P. S. width and height are specified in Initialize method with ballPosition default values:

width = graphics.GraphicsDevice.Viewport.Width;
height = graphics.GraphicsDevice.Viewport.Height;
ballPosition.X = width / 2;
ballPosition.Y = height / 2;

推荐答案

球离开屏幕的原因是球速可以大于1,因为这样,您必须检查球的当前位置和球速是否超出屏幕并进行处理(通过将其位置设置为宽度)
The reason the ball is going out of the screen is that the ball speed can be more then 1 as such you must check if the current position of the ball plus the ball speed goes out of screen and handle it (by setting its postion to width)


这篇关于XNA 4 Express Studio中的宽度和高度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆