2014-11-29 3 views
-1

XNA에서 Space Invaders 복사본을 만들고 있습니다. 운동과 스프라이트 애니메이션을 처리하는 playerShip 클래스가 있습니다. 오늘 나는 배를 쏘기로 결심했다. 내 탄환 논리를 유지하고 playShip 클래스에서 초기화/업데이트 한 클래스를 만들었습니다. 그래서 제 문제는 빌드하고 디버깅 할 때 촬영을위한 키를 누르는 것입니다. (제 경우에는 X) 총알은 화면에 그려지지 않습니다. 내 Draw() 호출에 중단 점을 넣었으며 예외는 없었습니다. 이것은 코드가 실행되지 않았 음을 의미합니다. 여기내 총알이 화면에 표시되지 않습니까?

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Storage; 
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; 
using Microsoft.Xna.Framework.Net; 

namespace SpaceInvaders 
{ 
    class playerShip 
    { 
     public static Texture2D playerShipTex, bulletsTex; 
     public static Rectangle playerShipHitBox; 
     public static Vector2 playerShipPos = new Vector2(369, 546), playerShipOrigin; 
     int playerShipCurrentFrame = 1, playerShipFrameWidth = 62, playerShipFrameHeight = 86; 
     public float spriteTimer = 0f, spriteInterval = 100f, bulletDelay; 
     public List<blasterLasers> bulletsList; 

     public playerShip() 
     { 
      bulletsList = new List<blasterLasers>(); 
      bulletDelay = 20f; 
     } 

     public void LoadContent(ContentManager Content) 
     { 
      playerShipTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\playerShip\\playerShipSpriteSheet"); 
      bulletsTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\playerShip\\playerShipBlaster"); 
     } 

     public void Update(GameTime gameTime) 
     { 
      playerShipHitBox = new Rectangle(playerShipCurrentFrame * playerShipFrameWidth, 0, playerShipFrameWidth, playerShipFrameHeight); 
      playerShipOrigin = new Vector2(playerShipHitBox.X/2, playerShipHitBox.Y/2); 

      MouseState mouseState = Mouse.GetState(); 
      KeyboardState keyboardState = Keyboard.GetState(); 

      spriteTimer += (float)gameTime.ElapsedGameTime.Milliseconds; 
      if (spriteTimer > spriteInterval) 
      { 
       playerShipCurrentFrame++; 
       spriteTimer = 0f; 
      } 

      if (playerShipCurrentFrame == 2) 
      { 
       playerShipCurrentFrame = 0; 
      } 

      playerShipHitBox = new Rectangle(playerShipCurrentFrame * playerShipFrameWidth, 0, playerShipFrameWidth, playerShipFrameHeight); 
      playerShipOrigin = new Vector2(playerShipHitBox.Width/2, playerShipHitBox.Height/2); 

      if (keyboardState.IsKeyDown(Keys.X)) 
      { 
       Shoot(); 
      } 

      UpdateBullets(); 

      if (keyboardState.IsKeyDown(Keys.Right)) 
      { 
       playerShipPos.X += 3; 
      } 

      if (keyboardState.IsKeyDown(Keys.Left)) 
      { 
       playerShipPos.X -= 3; 
      } 

      if (keyboardState.IsKeyDown(Keys.Down)) 
      { 
       playerShipPos.Y += 3; 
      } 

      if (keyboardState.IsKeyDown(Keys.Up)) 
      { 
       playerShipPos.Y -= 3; 
      } 

      if (playerShipPos.X <= 0) 
      { 
       playerShipPos.X = 0; 
      } 

      if (playerShipPos.X + playerShipTex.Width >= 1110) 
      { 
       playerShipPos.X = 1110 - playerShipTex.Width; 
      } 

      if (playerShipPos.Y <= 48) 
      { 
       playerShipPos.Y = 48; 
      } 

      if (playerShipPos.Y + playerShipTex.Height >= Game1.screenHeight) 
      { 
       playerShipPos.Y = Game1.screenHeight - playerShipTex.Height; 
      } 
     } 

     public void Shoot() 
     { 
      if (bulletDelay >= 0) 
      { 
       bulletDelay--; 
      } 

      if (bulletDelay <= 0) 
      { 
       blasterLasers newBullet = new blasterLasers(bulletsTex); 
       newBullet.spritePos = new Vector2(playerShipPos.X + 14 - newBullet.spriteTex.Width/2, playerShipPos.Y); 
       newBullet.isVisible = true; 

       if (bulletsList.Count() > 20) 
       { 
        bulletsList.Add(newBullet); 
       } 
      } 

      if (bulletDelay == 0) 
      { 
       bulletDelay = 20; 
      } 
     } 

     public void UpdateBullets() 
     { 
      foreach (blasterLasers bulletsSpawn in bulletsList) 
      { 
       bulletsSpawn.spritePos.Y = bulletsSpawn.spritePos.Y - bulletsSpawn.spriteSpeed; 
       if (bulletsSpawn.spritePos.Y == 0) 
       { 
        bulletsSpawn.isVisible = false; 
       } 
      } 

      for (int i = 0; i < bulletsList.Count(); i++) 
      { 
       if (!bulletsList[i].isVisible) 
       { 
        bulletsList.RemoveAt(i); 
        i--; 
       } 
      } 
     } 

     public void Draw(SpriteBatch spriteBatch) 
     { 
      spriteBatch.Draw(playerShipTex, playerShipPos, playerShipHitBox, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0); 
      foreach (blasterLasers bulletsSpawn in bulletsList) 
      { 
       bulletsSpawn.Draw(spriteBatch); 
      } 
     } 
    } 
} 

그리고 내 블래스터 클래스의 :

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Storage; 
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; 
using Microsoft.Xna.Framework.Net; 

namespace SpaceInvaders 
{ 
    public class blasterLasers 
    { 
     public Rectangle boundingBox; 
     public Texture2D spriteTex; 
     public Vector2 spriteOrigin, spritePos; 
     public bool isVisible; 
     public float spriteSpeed; 

     public blasterLasers(Texture2D newSpriteTex) 
     { 
      spriteSpeed = 10f; 
      spriteTex = newSpriteTex; 
      isVisible = false; 
     } 

     public void Draw(SpriteBatch spriteBatch) 
     { 
      spriteBatch.Draw(spriteTex, spritePos, Color.White); 
     } 
    } 
} 

그리고 마지막으로 나는 playerShip 클래스를 initalize 내 GAME1 클래스의 Update()/Draw()가 여기 내 선박 클래스입니다.

+1

이 줄'if (bulletsList.Count()> 20)'를 이해하지 못합니다. 그것은 무엇을합니까? 이미 현장에 총알이 없다면 새로 발사 할 수 없습니까? –

+0

@Aleksandar Toplek 전 세계에서 가장 큰 바보입니다! 'if (bulletsList.Count() <20) '여야합니다. 그래서 총알이 나타납니다. 질문이 해결되었습니다. – PowerUser

답변

1

(bulletsList.Count()> 20) 인 경우이 행을 이해할 수 없습니다. 그것은 무엇을합니까? 이미 현장에 총알이 없다면 새로 발사 할 수 없습니까?

- 또 다른 문제는

은 그럼 당신은 if (bulletsSpawn.spritePos.Y == 0) 물어. 하지만 position.Y는 0이 아니며 0 이상이거나 0 미만이됩니다. 따라서이를 변경하십시오 <=

+0

기다리면 또 다른 문제가 있습니다. 총알 20 발을 쏘면 더 이상 쏘지 않을 것입니다. 그게 뭐야? – PowerUser

+0

@Charlie 나는 내 생각을 문제로 생각하여 나의 대답을 편집했다. –

+0

감사합니다. 당신이 정말로 나를 도왔습니다. 그것은 항상 잊어 버린 작은 단순한 것들입니다. – PowerUser

관련 문제