2014-04-26 33 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