0
\$\begingroup\$

I'm developing a space invaders game in C# using XNA Game Studio. So I got some of the invader logic complete: An array holds the invaders and draws them on the screen, animated and moving.But I can't get the array to draw itself on a coordinate I spcify. I drew it with a Vector2 and whatever values I put to it the drawn position never changes. Here is my invader class:

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 botInvaders
    {

        public botInvaders()
        {

        }

        public static Texture2D botInvaderTex;
        public static Rectangle botInvaderHitBox;
        public static Vector2 botInvaderPos = new Vector2(0, 24), botInvaderOrigin;

        int botInvaderCurrentFrame = 1, botInvaderFrameWidth = 52, botInvaderFrameHeight = 88;

        float spriteTimer = 0f, spriteInterval = 100;

        public static Rectangle[,] botInvadersRect;
        int invaderRows = 5, invaderCollumns = 10;
        String botInvadersDirection = "RIGHT";
        public static Color invadersColor = Color.White;

        public void LoadContent(ContentManager Content)
        {
            botInvaderTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\botInvaders\\normalInvaders\\invaderShip1"); // invaderShip1
            botInvadersRect = new Rectangle[invaderRows, invaderCollumns];
            for (int r = 0; r < invaderRows; r++)
            {
                for (int c = 0; c < invaderCollumns; c++)
                {
                    botInvadersRect[r, c].Width = botInvaderFrameWidth;
                    botInvadersRect[r, c].Height = botInvaderTex.Height;
                    botInvadersRect[r, c].X = 70 * c;
                    botInvadersRect[r, c].Y = 70 * r;
                }
            }
        }

        public void Update(GameTime gameTime)
        {
            botInvaderHitBox = new Rectangle(botInvaderCurrentFrame * botInvaderFrameWidth, 0, botInvaderFrameWidth, botInvaderFrameHeight);
            botInvaderOrigin = new Vector2(botInvaderHitBox.X / 2, botInvaderHitBox.Y / 2);

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

            if (spriteTimer > spriteInterval)
            {
                botInvaderCurrentFrame++;
                spriteTimer = 0f;
            }

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

            botInvaderHitBox = new Rectangle(botInvaderCurrentFrame * botInvaderFrameWidth, 0, botInvaderFrameWidth, botInvaderFrameHeight);
            botInvaderOrigin = new Vector2(botInvaderHitBox.Width / 2, botInvaderHitBox.Height / 2);

            int m_RightSide = 800;
            int m_LeftSide = 0;

            for (int r = 0; r < invaderRows; r++)
            {
                for (int c = 0; c < invaderCollumns; c++)
                {
                    if (botInvadersDirection.Equals("RIGHT"))
                    {
                        botInvadersRect[r, c].X = botInvadersRect[r, c].X + 1;
                    }

                    if (botInvadersDirection.Equals("LEFT"))
                    {
                        botInvadersRect[r, c].X = botInvadersRect[r, c].X - 1;
                    }
                }
            }

            String botInvadersChangeDirection = "N";

            for (int r = 0; r < invaderRows; r++)
            {
                for (int c = 0; c < invaderCollumns; c++)
                {
                    if (botInvadersRect[r, c].X + botInvadersRect[r, c].Width > m_RightSide)
                    {
                        botInvadersDirection = "LEFT";
                        botInvadersChangeDirection = "Y";
                    }

                    if (botInvadersRect[r, c].X < m_LeftSide)
                    {
                        botInvadersDirection = "RIGHT";
                        botInvadersChangeDirection = "Y";
                    }
                }

                if (botInvadersChangeDirection.Equals("Y"))
                {
                    for (int c = 0; c < invaderCollumns; c++)
                    {
                        botInvadersRect[r, c].Y = botInvadersRect[r, c].Y + 3;
                    }
                }
            }
        }

        public void Draw(Texture2D texture, Vector2 position, Rectangle[,] destinationRect, Nullable<Rectangle> sourceRect, Color color, float rotation, Vector2 origin, SpriteEffects effects, float scale, SpriteBatch spriteBatch)
        {
            for (int r = 0; r < invaderRows; r++)
            {
                for (int c = 0; c < invaderCollumns; c++)
                {
                    // spriteBatch.Draw(botInvaderTex, botInvaderPos, botInvadersRect[r, c], botInvaderHitBox, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
                    spriteBatch.Draw(botInvaderTex, botInvadersRect[r, c], botInvaderHitBox, Color.White);
                }
            }
        }
    }
}

I initialized the class and drew it like this in Game1:

botInvader.Draw(botInvaders.botInvaderTex, botInvaders.botInvaderPos, botInvaders.botInvadersRect, botInvaders.botInvaderHitBox, Color.White, 0f, botInvaders.botInvaderOrigin, SpriteEffects.None, 1.0f, spriteBatch);

What should I change so that the array draws at 0, 24, since it doesn't...

\$\endgroup\$
0
\$\begingroup\$

Should have been: botInvadersRect[r, c].Y = (70 * r) + 24;

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.