2014-04-26 32 views
0

因此,目前,我是一个在c#中开始编码的newb。我开始制作类似于太空侵略者的太空游戏,并且遇到了一个问题,在绘制方法中,我设定它在我打空间时立即画出我的子弹并向上移动。但问题是,只要我放下空间,我的子弹就消失了。当子弹离开屏幕后,我会绕着它传送到船上。无论如何要让游戏检查过去是否按下按钮,并让子弹继续移动,直到我希望它消失。在c中检测按键的历史记录#

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using Microsoft.Xna.Framework; 
    using Microsoft.Xna.Framework.Audio; 
    using Microsoft.Xna.Framework.Content; 
    using Microsoft.Xna.Framework.GamerServices; 
    using Microsoft.Xna.Framework.Graphics; 
    using Microsoft.Xna.Framework.Input; 
    using Microsoft.Xna.Framework.Media; 

namespace Spaceteroids 
{ 
/// <summary> 
/// This is the main type for your game 
/// </summary> 
public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    Texture2D spriteSheet; 
    Texture2D laser; 
    Texture2D Enemyship; 
    spriteAnimation loading; 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    KeyboardState currentKeyboard; 
    KeyboardState oldKeyboard; 
    PlayerShip character; 
    Rectangle characterRectangle; 
    Bullet bullet; 
    Bullet bullet2; 
    bool heightCheck; 

    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 
    } 

    /// <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 

     currentKeyboard = new KeyboardState(); 
     oldKeyboard = new KeyboardState(); 
     //characterRectangle = new Rectangle((int)character.Position.X,(int)character.Position.Y, spriteSheet.Width/4, spriteSheet.Height); 
     character = new PlayerShip(new Vector2(GraphicsDevice.Viewport.Width/2, GraphicsDevice.Viewport.Height - 40)); 
     bullet = new Bullet(new Vector2(character.Position.X, character.Position.Y - 20)); 

     base.Initialize(); 
    } 
    void Movement() 
    { 
     if (IsHeld(Keys.A)) 
     { 
      character.xSpeed = -6; 
     } 
     else if (IsHeld(Keys.D)) 
     { 
      character.xSpeed = 6; 
     } 
     else 
     { 
      character.xSpeed = 0; 
     } 
    } 
    void Collision() 
    { 
     if (character.Position.X < 0) 
     { 
      character.Position.X = 0; 
     } 
     else if (character.Position.X + (spriteSheet.Width/4) > graphics.GraphicsDevice.Viewport.Width) 
     { 
      character.Position.X = graphics.GraphicsDevice.Viewport.Width - (spriteSheet.Width/4) ; 
     } 
    } 
    /// <summary> 
    /// LoadContent will be called once per game and is the place to load 
    /// all of your content. 
    /// </summary> 
    protected override void LoadContent() 
    { 
     laser = Content.Load<Texture2D>("Laser"); 
     Enemyship = Content.Load<Texture2D>("EnemyShip"); 
     spriteSheet = Content.Load<Texture2D>("shipSpriteSheet"); 
     spriteBatch = new SpriteBatch(GraphicsDevice); 
     loading = new spriteAnimation(Content.Load<Texture2D>("shipSpriteSheet"), 4); 
     loading.Position = new Vector2(character.Position.X); 
     loading.IsLooping = true; 
     loading.FramesPerSecond = 30; 
     // 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) 
    { 
     bullet.Position.X = character.Position.X; 
     currentKeyboard = Keyboard.GetState(); 
     oldKeyboard = currentKeyboard; 
     loading.Update(gameTime, character); 
     bullet.ySpeed = -10; 

     bullet.UpdateBullet(); 

     Collision(); 
     Movement(); 
     if (NotPressed(Keys.Space)) 
     { 
      bullet.Position.Y = character.Position.Y - 15; 
     } 
     if (bullet.Position.Y < 0) 
     { 
      bullet.Position.Y = character.Position.Y - 15; 
     } 

     character.UpdateShip(); 
     // 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.Black); 

     spriteBatch.Begin(); 


     spriteBatch.Draw(laser, new Vector2(bullet.Position.X, bullet.Position.Y), Color.White); 


     loading.Draw(spriteBatch); 
     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 

    public bool IsHeld(Keys key) 
    { 
     if (currentKeyboard.IsKeyDown(key)) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
    public bool IsReleased(Keys key) 
    { 
     if (currentKeyboard.IsKeyUp(key) && oldKeyboard.IsKeyDown(key)) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
    public bool JustPressed(Keys key) 
    { 
     if (currentKeyboard.IsKeyDown(key) && oldKeyboard.IsKeyUp(key)) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    public bool NotPressed(Keys key) 
    { 
     if (IsHeld(key) != true) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
    public bool HasBeenPressed(Keys key) 
    { 
     if (oldKeyboard.IsKeyDown(key)) 
     { 
      return true; 
     } 
     else return false; 
    } 

} 
} 

如果您需要任何其他信息,请随时询问。

回答

0

不,在XNA中没有任何东西可以跟踪击键历史(或任何其他地方)。你必须自己做。

解决问题的常用方法是在表示子弹的对象上维护状态。击键可能会消失,但该物体仍然保持并保持子弹可见并移动,直至其被摧毁。您看起来在您的项目符号对象中具有该状态,因此您可能在更新时发生逻辑错误。

从快速阅读中可以看出问题并不明显,但您对键盘状态的处理看起来过于复杂。所有你需要的是关键和关键。所有其他你应该跟踪自己。

+0

你知道我该怎么做吗?就像我之前说过的,我对xna很陌生。你的意思是使用枚举吗? – user3574878