0

我最近遇到了这个问题——在我的主类 Game1.cs 中,我调用了我的 loot.cs 中的一个方法来确定应该给玩家哪个战利品。该方法基本上在调用时添加了 5 个硬币。我的问题是,每当调用该方法并继续执行和更新我的 Game1.cs 中的内容时,通过该方法获得的所有硬币都消失了。

我一直运行调试器,所以我知道玩家实际上得到了硬币,但后来硬币被移除了。

完整的 Game1.cs(警告,文字墙)

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;
using System.IO;

namespace Innovationally 
{
enum GameState
{
    TITLESCREEN,
    HELPSCREEN,
    PLAYING,
    WON,
    LOST
}
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    GameState gameState = GameState.PLAYING;

    //PLAYER STATS
    Player player;
    KeyboardState currentKeyboardState;
    KeyboardState previousKeyboardState;
    float playerMoveSpeed;

    //COLLISION STATS
    Rectangle kollision;
    int bHit;

    //LEVEL STATS
    int level_number = 0;
    int loadlevel = 0;
    Texture2D hud, level0, level1, level2, level3, level4, level5;
    Vector2 levelPos;

    //ROOM STATS
    List<int> tile_life = new List<int>();
    Texture2D tile_gfx, stairsUp, stairsDown;
    List<Vector2> tile_position = new List<Vector2>();
    List<int> tile_type = new List<int>();
    int antlabb = 0;
    int antvapen = 0;
    int antpolis = 0;
    int antwavers = 0;
    int researchSpan;

    //MISC
    SpriteFont font;
    Loot loot;

    //GAMEPLAY STATS
    TimeSpan timeElapsed;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferHeight = 800;
        graphics.PreferredBackBufferWidth = 900;
    } 
    protected override void Initialize()
    {
        player = new Player();
        playerMoveSpeed = 4.0f;
        levelPos.X = 0;
        levelPos.Y = 0;
        loot = new Loot();
        researchSpan = 120;
        base.Initialize();
    }
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        Vector2 playerPosition = new Vector2(430, 450);
        //LEVEL STATS
        LaddaLevel(level_number);
        level0 = Content.Load<Texture2D>("level0");
        level1 = Content.Load<Texture2D>("level1");
        level2 = Content.Load<Texture2D>("level2");
        level3 = Content.Load<Texture2D>("level3");
        level4 = Content.Load<Texture2D>("level4");
        level5 = Content.Load<Texture2D>("level5");
        hud = Content.Load<Texture2D>("hud");

        //ROOM STATS
        tile_gfx = Content.Load<Texture2D>("tile");
        stairsUp = Content.Load<Texture2D>("stairsUp");
        stairsDown = Content.Load<Texture2D>("stairsDown");

        font = Content.Load<SpriteFont>("SpriteFont1");
        player.Initialize(Content.Load<Texture2D>("Leftplayer"), playerPosition);
    }
    public void LaddaLevel(int nummer)
    {
        StreamReader SR = new StreamReader(nummer.ToString());

        string bana = SR.ReadToEnd();
        SR.Close();
        int temp_positionY = 0;
        int temp_positionX = 0;

        tile_position.Clear(); 
        tile_type.Clear();    
        tile_life.Clear(); 

        for (int i = 0; i < bana.Length; i++)
        {
            switch (bana[i])
            {
                case ' ':
                    temp_positionX++;
                    break;
                case '0':
                    tile_life.Add(loot.myRnd.Next(8));
                    tile_position.Add(new Vector2((temp_positionX * 100), (temp_positionY * 100)));
                    temp_positionX++;
                    tile_type.Add(int.Parse(bana[i].ToString()));
                    break;
                case '8': 
                    tile_position.Add(new Vector2((temp_positionX * 100), (temp_positionY * 100)));
                    temp_positionX++;
                    tile_type.Add(int.Parse(bana[i].ToString()));
                    tile_life.Add(8);
                    break;
                case '9': 
                    tile_position.Add(new Vector2((temp_positionX * 100), (temp_positionY * 100)));
                    temp_positionX++;
                    tile_type.Add(int.Parse(bana[i].ToString()));
                    tile_life.Add(9);
                    break;
                case '\n':
                    temp_positionY++;
                    temp_positionX = 0;
                    break;
            }
        }
    }
    protected override void UnloadContent()
    {
    }
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        previousKeyboardState = currentKeyboardState;
        currentKeyboardState = Keyboard.GetState();
        if (currentKeyboardState.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape))
            this.Exit();

        switch (gameState)
        {
            case GameState.TITLESCREEN:
                if (currentKeyboardState.IsKeyDown(Keys.S) && previousKeyboardState.IsKeyUp(Keys.S))
                    gameState = GameState.PLAYING;
                if (currentKeyboardState.IsKeyDown(Keys.H) && previousKeyboardState.IsKeyUp(Keys.H))
                    gameState = GameState.HELPSCREEN;
                break;
            case GameState.HELPSCREEN:
                if (currentKeyboardState.IsKeyDown(Keys.B) && previousKeyboardState.IsKeyUp(Keys.B))
                    gameState = GameState.TITLESCREEN;
                break;
            case GameState.PLAYING:
                timeElapsed += gameTime.ElapsedGameTime;
                UpdatePlayer(gameTime);
                UpdateCollisions(gameTime);
                break;
            case GameState.LOST:
                if (currentKeyboardState.IsKeyDown(Keys.S) && previousKeyboardState.IsKeyUp(Keys.S))
                {
                    loadlevel = 0;
                    gameState = GameState.PLAYING;
                }
                break;
        }
        base.Update(gameTime);
    }
    private void UpdatePlayer(GameTime gameTime)
    {

        if (currentKeyboardState.IsKeyDown(Keys.Left))
        {
            player.angle = (float)Math.PI * 1.5f;
            player.Position.X -= playerMoveSpeed;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Right))
        {
            player.angle = (float)Math.PI / 2;
            player.Position.X += playerMoveSpeed;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Up))
        {
            player.angle = (float)Math.PI * 2;
            player.Position.Y -= playerMoveSpeed;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Down))
        {
            player.angle = (float)Math.PI;
            player.Position.Y += playerMoveSpeed;
        }
        if (player.health <= 0)
            gameState = GameState.LOST;
    }
    public void UpdateResearchCenters(GameTime gameTime)
    {
        if (antlabb >= 1)
            researchSpan -= (gameTime.ElapsedGameTime.Seconds * antlabb);

        //if (researchSpan <= 0)
          //  d
    }
    public void UpdateCollisions(GameTime gameTime)
    {
        Rectangle playerBox = new Rectangle((int)player.Position.X - 20, (int)player.Position.Y - 20, 40, 37);
        Rectangle levelBox = new Rectangle(0, 0, 900, 800);

        for (int i = 0; i < tile_position.Count; i++)
        {
            Rectangle tileBox = new Rectangle((int)tile_position[i].X, (int)tile_position[i].Y, 100, 100);
            if (playerBox.Intersects(tileBox))
            {
                if (tile_life[i] <= 9)
                {
                    if (tile_life[i] == 9 && currentKeyboardState.IsKeyDown(Keys.Space)) 
                    {
                        loadlevel += 1;
                        LaddaLevel(loadlevel);
                    }
                    else if (tile_life[i] == 8 && currentKeyboardState.IsKeyDown(Keys.Space))
                    {
                        loadlevel -= 1;
                        LaddaLevel(loadlevel);
                    }
                    else if (tile_life[i] == 7 && currentKeyboardState.IsKeyDown(Keys.Space))
                    {
                        loot.RandomLoot();
                        tile_life[i] = 70; 
                    }
                    else if (tile_life[i] == 6 && currentKeyboardState.IsKeyDown(Keys.Space))
                    {
                        tile_life[i] = 60;
                    }
                    else if (tile_life[i] == 60 && currentKeyboardState.IsKeyDown(Keys.Space) && (player.mvgelever >= 1 || player.problemelever >= 1 || player.normalaelever >= 1))
                    {
                        if (player.mvgelever >= 1)
                        {
                            player.mvgelever -= 1;
                            tile_life[i] = 600;
                        }
                        else if (player.normalaelever >= 1)
                        {
                            player.normalaelever -= 1;
                            tile_life[i] = 6000;
                        }
                        else if (player.problemelever >= 1)
                        {
                            player.problemelever -= 1;
                            tile_life[i] = 60000;
                        }

                        antlabb += 1;
                    }
                    else if ((tile_life[i] == 600 || tile_life[i] == 6000 || tile_life[i] == 60000) && currentKeyboardState.IsKeyDown(Keys.Space) && previousKeyboardState.IsKeyUp(Keys.Space))
                    {
                        antlabb -= 1;
                        if (tile_life[i] == 600)
                            player.mvgelever += 1;
                        if (tile_life[i] == 6000)
                            player.mvgelever += 1;
                        if (tile_life[i] == 60000)
                            player.mvgelever += 1;
                        tile_life[i] = 60;
                    }
                }
            }
        }
        //Överlappar vi?
        kollision = Intersection(playerBox, levelBox);

        if (kollision.Width > 0 && kollision.Height > 0)
        {
            Rectangle r1 = Normalize(playerBox, kollision);
            Rectangle r2 = Normalize(levelBox, kollision);
            if (loadlevel == 0)
                bHit = TestCollision(player.PlayerTexture, r1, level0, r2);
            if (loadlevel == 1)
                bHit = TestCollision(player.PlayerTexture, r1, level1, r2);
            if (loadlevel == 2)
                bHit = TestCollision(player.PlayerTexture, r1, level2, r2);
            if (loadlevel == 3)
                bHit = TestCollision(player.PlayerTexture, r1, level3, r2);
            if (loadlevel == 4)
                bHit = TestCollision(player.PlayerTexture, r1, level4, r2);
            if (loadlevel == 5)
                bHit = TestCollision(player.PlayerTexture, r1, level5, r2);
        }
        else
        {
            bHit = 0;
        }

        if (bHit == 1 || bHit == 2)
        {
            if (player.angle == (float)Math.PI)
                player.Position.Y -= playerMoveSpeed;
            if (player.angle == (float)Math.PI * 2)
                player.Position.Y += playerMoveSpeed;
            if (player.angle == (float)Math.PI / 2)
                player.Position.X -= playerMoveSpeed;
            if (player.angle == (float)Math.PI * 1.5f)
                player.Position.X += playerMoveSpeed;
        }
    }
    public static Rectangle Intersection(Rectangle r1, Rectangle r2)
    {
        int x1 = Math.Max(r1.Left, r2.Left);
        int y1 = Math.Max(r1.Top, r2.Top);
        int x2 = Math.Min(r1.Right, r2.Right);
        int y2 = Math.Min(r1.Bottom, r2.Bottom);

        if ((x2 >= x1) && (y2 >= y1))
        {
            return new Rectangle(x1, y1, x2 - x1, y2 - y1);
        }
        return Rectangle.Empty;
    }

    public static Rectangle Normalize(Rectangle reference, Rectangle overlap)
    {
        //Räkna ut en rektangel som kan användas relativt till referensrektangeln
        return new Rectangle(
          overlap.X - reference.X,
          overlap.Y - reference.Y,
          overlap.Width,
          overlap.Height);
    }

    public static int TestCollision(Texture2D t1, Rectangle r1, Texture2D t2, Rectangle r2)
    {
        //Beräkna hur många pixlar som finns i området som ska undersökas
        int pixelCount = r1.Width * r1.Height;
        uint[] texture1Pixels = new uint[pixelCount];
        uint[] texture2Pixels = new uint[pixelCount];

        //Kopiera ut pixlarna från båda områdena
        t1.GetData(0, r1, texture1Pixels, 0, pixelCount);
        t2.GetData(0, r2, texture2Pixels, 0, pixelCount);

        //Jämför om vi har några pixlar som överlappar varandra i områdena
        for (int i = 0; i < pixelCount; ++i)
        {
            if (((texture1Pixels[i] & 0xff000000) > 0) && (texture2Pixels[i] == 0xffC3C3C3))
            {
                return 1;
            }
            if (((texture1Pixels[i] & 0xff000000) > 0) && (texture2Pixels[i] == 0xff000000))
            {
                return 2;
            }
            if (((texture1Pixels[i] & 0xff000000) > 0) && (texture2Pixels[i] == 0xff000000))
            {
                return 1;
            }

        }
        return 0;
    }
    private void DrawHud()
    {
        string timeString = "TIME: " + timeElapsed.Minutes.ToString("00") + ":" + timeElapsed.Seconds.ToString("00");

        spriteBatch.Draw(hud, new Vector2(0, 0), Color.White);
        spriteBatch.DrawString(font, timeString, new Vector2(15, 35), Color.White);
        spriteBatch.DrawString(font, "Level " + (loadlevel + 1), new Vector2(15, 10), Color.White);
        spriteBatch.DrawString(font, "" + player.mvgelever, new Vector2(739, 55), Color.White);
        spriteBatch.DrawString(font, "" + player.problemelever, new Vector2(799, 55), Color.White);
        spriteBatch.DrawString(font, "" + player.normalaelever, new Vector2(859, 55), Color.White);
        spriteBatch.DrawString(font, "" + antwavers, new Vector2(454, 55), Color.White);
        spriteBatch.DrawString(font, "" + antpolis, new Vector2(514, 55), Color.White);
        spriteBatch.DrawString(font, "" + antvapen, new Vector2(574, 55), Color.White);
        spriteBatch.DrawString(font, "" + antlabb, new Vector2(633, 55), Color.White);
        spriteBatch.DrawString(font, "" + player.antkills, new Vector2(348, 55), Color.White);
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        switch (gameState)
        {
            case GameState.TITLESCREEN:
                break;
            case GameState.PLAYING:
                if (loadlevel == 0)
                    spriteBatch.Draw(level0, new Vector2(0, 0), Color.White);
                if (loadlevel == 1)
                    spriteBatch.Draw(level1, new Vector2(0, 0), Color.White);
                if (loadlevel == 2)
                    spriteBatch.Draw(level2, new Vector2(0, 0), Color.White);
                if (loadlevel == 3)
                    spriteBatch.Draw(level3, new Vector2(0, 0), Color.White);
                if (loadlevel == 4)
                    spriteBatch.Draw(level4, new Vector2(0, 0), Color.White);
                if (loadlevel == 5)
                    spriteBatch.Draw(level5, new Vector2(0, 0), Color.White);
                for (int i = 0; i < tile_position.Count; i++)
                {
                    switch (tile_life[i])
                    {
                        case 0: spriteBatch.Draw(tile_gfx, tile_position[i], Color.White);
                            break;
                        case 1: spriteBatch.Draw(tile_gfx, tile_position[i], Color.HotPink);
                            break;
                        case 2: spriteBatch.Draw(tile_gfx, tile_position[i], Color.YellowGreen);
                            break;
                        case 3: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Purple);
                            break;
                        case 4: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Yellow);
                            break;
                        case 5: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Silver);
                            break;
                        case 6: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Gold);
                            break;
                        case 7: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Orange);
                            break;
                        case 70: spriteBatch.Draw(tile_gfx, tile_position[i], Color.DarkOrange);
                            break;
                        case 8: spriteBatch.Draw(stairsDown, tile_position[i], Color.White);
                            break;
                        case 9: spriteBatch.Draw(stairsUp, tile_position[i], Color.White);
                            break;
                    }
                }
                DrawHud();
                player.Draw(spriteBatch);
                break;
        }
        spriteBatch.End();
        base.Draw(gameTime);
    }
}
}

Loot.cs 的 RandomLoot() 方法:

 class Loot
{
    Player player = new Player();
    public Random myRnd = new Random();
    public int primarylootnumber;
    public int secondarylootnumber;
    public int tertiarylootnumber;
    public void RandomLoot()
    {
        primarylootnumber = myRnd.Next(1, 11);
        switch (primarylootnumber)
        {
            case 1:
                player.godis++;
                break;
            case 2:
                player.godis += 3;
                break;
            case 3:
                player.godis += 5;
                break;
            case 4:
                player.normalaelever += 1;
                break;
            case 5:
                player.normalaelever += 2;
                break;
            case 6:
                player.health += 10;
                break;
            case 7:
                player.health += 30;
                break;
            case 8:
                player.health += 50;
                break;
            case 9:
                player.damage += 5;
                break;
            case 10:
                player.damage += 10;
                break;
        }

最后,在 Player.cs 中:

class Player
{
    public int godis, normalaelever, health, damage, maximumhealth, normalaelever, problemelever;
}

我错过了什么?

4

2 回答 2

1

这里有一个非常简单的问题——Game1.cs 定义了一个玩家,每个 Loot 对象组成了自己的玩家。

这意味着您有大量玩家,其中一个是游戏使用和控制的,其余的是每个硬币正在使用的临时假玩家对象。

您只需要定义一个玩家对象:

在 Game1.cs 中,更改“Player player;” 到“公共静态播放器播放器”;

编辑:然后在 Player.cs 中,将“class Player”更改为“public class Player”

然后在 loot.cs 中,更改“Player player = new Player();” 到“玩家玩家= Game1.player;”

这将确保您只使用一个玩家对象。

(注意:有“更好”的方法来处理这个问题,例如将播放器对象引用传递给 Loot 对象的 init 函数或其他任何方法。这是一种快速而简单的方法。)

上面代码的解释:“public”的意思是“任何其他类都可以访问这个变量”。而“静态”的意思是“这里只有一个值,无论我创建多少次 Game1 类。” (在这种情况下,尽管只有一个 Game1,但使用静态来制作它,因此您可以从 classname.variable 访问变量,而不必首先传递当前的 Game1 对象。)

其他解释:每次执行“Class class = new Class();”时,您都在说“创建该类的全新对象”。一个类就像是对某物的定义,例如,您将“球”类定义为任何圆形和有弹性的东西。对象是此定义的一个实例。EG,一个实际的物理球。但是你也可以有第二个球、第三个、第四个等等,它们仍然只是同一个类别,但同一类别的不同对象。

于 2013-06-09T20:15:07.313 回答
0

您没有为 设置值coins,您所做的是为 godis、normalaelever、health 或 damage 设置值

更新

public class Player
{
    public int godis { get; set; }
    public int normalaelever { get; set; }
    public int health { get; set; }
    public int damage { get; set; }
    public int maximumhealth { get; set; }
    public int problemelever { get; set; }
}
于 2013-06-09T11:44:06.427 回答