0
\$\begingroup\$

So I've been making a Space Invaders game in XNA. I have my ship spritesheet (sprites are left-to-right - horizontal). The spritesheet has 6 sprites. Here's my class:

playerShip.cs

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 playerShip()
        {

        }

        public static Texture2D g_PlayerShipTex;
        Rectangle m_PlayerShipHitBox;
        public static Vector2 g_PlayerShipPos = new Vector2(400, 546);
        Vector2 m_PlayerShipOrigin;

        int m_PlayerShipCurrentFrame = 1;
        int m_PlayerShipFrameWidth = 62;
        int m_PlayerShipFrameHeight = 64;

        float m_Timer = 0f;
        float m_Interval = 100;

        int m_GameStart = 1000;

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

        public void Update(GameTime gameTime)
        {
            m_PlayerShipHitBox = new Rectangle(m_PlayerShipCurrentFrame * m_PlayerShipFrameWidth, 0, m_PlayerShipFrameWidth, m_PlayerShipFrameHeight);
            m_PlayerShipOrigin = new Vector2(m_PlayerShipHitBox.X / 2, m_PlayerShipHitBox.Y / 2);

            MouseState m_MouseState = Mouse.GetState();
            KeyboardState m_KeyboardState = Keyboard.GetState();

            m_Timer += (float)gameTime.ElapsedGameTime.Milliseconds;


            m_GameStart--;

            if (m_GameStart == 800)
            {
                m_PlayerShipFrameHeight = 86;
                m_PlayerShipCurrentFrame = 3;
            }

            if (m_GameStart == 600)
            {
                m_PlayerShipFrameHeight = 86;
                m_PlayerShipCurrentFrame = 4;
            }

            if (m_GameStart == 400)
            {
                m_PlayerShipFrameHeight = 86;
                m_PlayerShipCurrentFrame = 5;
            }

            if (m_GameStart == 200)
            {
                m_PlayerShipFrameHeight = 86;
                m_PlayerShipCurrentFrame = 6;
                g_PlayerShipPos.Y -= 10;
            }

            if (m_GameStart == 0)
            {
                m_PlayerShipFrameHeight = 64;
                m_PlayerShipCurrentFrame = 0;
            }

            if (m_Timer > m_Interval)
            {
                m_PlayerShipCurrentFrame++;
                m_Timer = 0f;
            }

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

            m_PlayerShipHitBox = new Rectangle(m_PlayerShipCurrentFrame * m_PlayerShipFrameWidth, 0, m_PlayerShipFrameWidth, m_PlayerShipFrameHeight);
            m_PlayerShipOrigin = new Vector2(m_PlayerShipHitBox.Width / 2, m_PlayerShipHitBox.Height / 2);

            if (m_KeyboardState.IsKeyDown(Keys.Right))
            {
                g_PlayerShipPos.X += 3;
            }

            if (m_KeyboardState.IsKeyDown(Keys.Left))
            {
                g_PlayerShipPos.X -= 3;
            }

            if (m_KeyboardState.IsKeyDown(Keys.Down))
            {
                g_PlayerShipPos.Y += 3;
            }

            if (m_KeyboardState.IsKeyDown(Keys.Up))
            {
                g_PlayerShipPos.Y -= 3;
            }

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

            if (g_PlayerShipPos.X + g_PlayerShipTex.Width >= 1110)
            {
                g_PlayerShipPos.X = 1110 - g_PlayerShipTex.Width;
            }

            if (g_PlayerShipPos.Y <= 0)
            {
                g_PlayerShipPos.Y = 0;
            }

            if (g_PlayerShipPos.Y + g_PlayerShipTex.Height >= 600)
            {
                g_PlayerShipPos.Y = 600 - g_PlayerShipTex.Height;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(g_PlayerShipTex, g_PlayerShipPos, m_PlayerShipHitBox, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
        }
    }
}

So the code in the Update() method handles the entire player ship logic. From sprite animation and user input movement to non-user input movement (or at least, it should but it doesn't). Everything else works. Except the "start movement animation". Basically I'm looking for a way to show sprite #3 for let's say 3 seconds, then change it to #4 (3sec), change to #5 3sec, #6 3sec and then start looping #1 and #2.

The results I get:

The looping #1 and #2 sprites show first. Then #3, #4, #5 and #6 sprites do as desired BUT (here's my issue) they blink to transparent and a black horizontal line replaces them (I heard that one's related to spriteBatch.Draw(), is it?). After a short delay I can see #1 and #2 looping again. Any help is appreciated!

\$\endgroup\$
  • \$\begingroup\$ What is the size of your sprite sheet? What size are your sprites (full square clipping)? It sounds like the problem your program is pulling a currentframe out of the wrong spot in your sprite sheet. each sprite should have identical height/width to ensure that they are centered. If this isn't the case, then the sprite sheet should be reworked to ensure that each sprites center is directly in the center of the frame you are pulling it from. \$\endgroup\$ – Shroeder Oct 24 '14 at 18:49
  • \$\begingroup\$ The spritesheet is 372x86, with every sprite being 62x86 I don't know what could the problem be... \$\endgroup\$ – PowerUser Oct 25 '14 at 17:28
  • \$\begingroup\$ Can you upload a screenshot of the problem. \$\endgroup\$ – Shroeder Oct 27 '14 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.