精灵变得非常规模

时间:2014-11-24 18:32:30

标签: c# xna scale

我已经在XNA中制作了太空入侵者游戏一段时间了。我让入侵者动画并包含在一个数组中。但是当我打电话给spriteBatch.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 botInvaders
    {

        public botInvaders()
        {

        }

        public static Texture2D g_BotInvaderTex;
        public static Rectangle g_BotInvaderHitBox;
        public static Vector2 g_BotInvaderPos = new Vector2(0, 24);
        public static Vector2 g_BotInvaderOrigin;

        int m_BotInvaderCurrentFrame = 1;
        int m_BotInvaderFrameWidth = 52;
        int m_BotInvaderFrameHeight = 88;

        float m_Timer = 0f;
        float m_Interval = 100;

        public static Rectangle[,] g_BotInvadersRect;
        int m_InvaderRows = 5;
        int m_InvaderCollumns = 10;
        String m_BotInvadersDirection = "RIGHT";
        public static Color InvadersColor = Color.White;

        public void Initialize()
        {

        }

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

        public void Update(GameTime gameTime)
        {
            g_BotInvaderHitBox = new Rectangle(m_BotInvaderCurrentFrame * m_BotInvaderFrameWidth, 0, m_BotInvaderFrameWidth, m_BotInvaderFrameHeight);
            g_BotInvaderOrigin = new Vector2(g_BotInvaderHitBox.X / 2, g_BotInvaderHitBox.Y / 2);

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

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

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

            g_BotInvaderHitBox = new Rectangle(m_BotInvaderCurrentFrame * m_BotInvaderFrameWidth, 0, m_BotInvaderFrameWidth, m_BotInvaderFrameHeight);
            g_BotInvaderOrigin = new Vector2(g_BotInvaderHitBox.Width / 2, g_BotInvaderHitBox.Height / 2);

            int m_RightSide = 800;
            int m_LeftSide = 0;

            for (int r = 0; r < m_InvaderRows; r++)
            {
                for (int c = 0; c < m_InvaderCollumns; c++)
                {
                    if (m_BotInvadersDirection.Equals("RIGHT"))
                    {
                        g_BotInvadersRect[r, c].X = g_BotInvadersRect[r, c].X + 1;
                    }

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

            String m_BotInvadersChangeDirection = "N";

            for (int r = 0; r < m_InvaderRows; r++)
            {
                for (int c = 0; c < m_InvaderCollumns; c++)
                {
                    if (g_BotInvadersRect[r, c].X + g_BotInvadersRect[r, c].Width > m_RightSide)
                    {
                        m_BotInvadersDirection = "LEFT";
                        m_BotInvadersChangeDirection = "Y";
                    }

                    if (g_BotInvadersRect[r, c].X < m_LeftSide)
                    {
                        m_BotInvadersDirection = "RIGHT";
                        m_BotInvadersChangeDirection = "Y";
                    }
                }

                if (m_BotInvadersChangeDirection.Equals("Y"))
                {
                    for (int c = 0; c < m_InvaderCollumns; c++)
                    {
                        g_BotInvadersRect[r, c].Y = g_BotInvadersRect[r, c].Y + 3;
                    }
                }
            }
        }

        public void Draw(Texture2D texture, Rectangle[,] destinationRect, Nullable<Rectangle> sourceRect, Color color, SpriteBatch spriteBatch)
        {
            for (int r = 0; r < m_InvaderRows; r++)
            {
                for (int c = 0; c < m_InvaderCollumns; c++)
                {
                    spriteBatch.Draw(g_BotInvaderTex, g_BotInvadersRect[r, c], g_BotInvaderHitBox, Color.White);
                }
            }
        }
    }
}

在我的Game1中初始化入侵者类并调用Draw:

之后
botInvader.Draw(botInvaders.g_BotInvaderTex, botInvaders.g_BotInvaderPos, botInvaders.g_BotInvadersRect, botInvaders.g_BotInvaderHitBox, Color.White, 0f, botInvaders.g_BotInvaderOrigin, SpriteEffects.None, 1.0f, spriteBatch);

我不认为精灵应该按比例放大,毕竟我已经给了1.0f比例参数???

0 个答案:

没有答案