Xna运动不工作

本文关键字:工作 运动 Xna | 更新日期: 2023-09-27 17:53:32

嘿,自从1周以来,我一直在编写c# Xna,我遇到了一个问题我希望你能帮助我。问题是,我做了一个主类,我画的一切,我有一个类的控制但是现在控制不起作用了。

主类:

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;
namespace Pong_0._0._0._1
{
    public class Main : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public Texture2D paddel1;
        public Texture2D paddel2;
        public Texture2D border1;
        public Texture2D border2;
        public Vector2 paddel1Pos;
        public Vector2 paddel2Pos;
        public Vector2 border1Pos;
        public Vector2 border2Pos;
        public static int ScreenWidth = 1024;
        public static int ScreenHeight = 768;
        Paddels pads;
        public Main()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = ScreenWidth;
            graphics.PreferredBackBufferHeight = ScreenHeight;
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            pads = new Paddels();
            pads.Initialize();
            base.Initialize();
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            paddel1 = Content.Load<Texture2D>("BOP");
            paddel2 = Content.Load<Texture2D>("BOP");
            border1 = Content.Load<Texture2D>("BOB");
            border2 = Content.Load<Texture2D>("BOB");
            paddel1Pos = new Vector2(ScreenWidth / 16, ScreenHeight / 2 - paddel1.Height / 2);
            paddel2Pos = new Vector2((ScreenWidth - paddel2.Width) - ScreenWidth / 16 , ScreenHeight / 2 - paddel2.Height / 2);
            border1Pos = new Vector2(ScreenWidth / 2 - (border1.Width / 2) , ScreenHeight / 12);
            border2Pos = new Vector2(ScreenWidth / 2 - (border2.Width / 2), (ScreenHeight - border2.Height) - ScreenHeight / 12);
        }
        protected override void UnloadContent()
        {
        }
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            pads.Update(gameTime);
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Tomato);
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            spriteBatch.Draw(paddel1, paddel1Pos, Color.White);
            spriteBatch.Draw(paddel2, paddel2Pos, Color.White);
            spriteBatch.Draw(border1, border1Pos, Color.White);
            spriteBatch.Draw(border2, border2Pos, Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

控制类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Pong_0._0._0._1
{
    public class Paddels
    {
        public Main main;
        public int SPEED = 2;
        public Paddels()
        { 
        }
        public void Initialize()
        {
            main = new Main();
        }
        public void Update(GameTime gameTime)
        {
            KeyboardState KBS = Keyboard.GetState();
            if (KBS.IsKeyDown(Keys.Up))
            {
                main.paddel1Pos.Y = main.paddel1Pos.Y + SPEED;
            }
            else if (KBS.IsKeyDown(Keys.Down))
            { 
            }
            if (KBS.IsKeyDown(Keys.W))
            { 
            }
            else if (KBS.IsKeyDown(Keys.S))
            { 
            }
        }
    }
}

我可以在一堂课上全部完成,但我想学会使用多个类,而这行不通。

谢谢,希望尽快收到回信。

Xna运动不工作

你正在你的" paddle "类中创建一个新的"main"。这个新的"Main"对象不知道实际的对象。

要解决这个问题,只需在创建桨时传递现有的桨:

pads.Initialize(this);

初始化函数更改为:

public void Initialize(Main parent)
{
  main = parent;
}

一般来说,这样做会破坏封装。"paddle"类应该包含所有的纹理、更新逻辑和绘制划桨游戏对象的逻辑。main类不需要这些,因为它正在将这些信息传递给新类,并且"paddle"类不需要任何关于"main"类的知识(至少在您的示例中是这样)。

如果您需要任何澄清或额外信息,请告诉我。

你需要将桨的位置和桨的纹理放入桨类中。

然后你需要使用这些值使桨自己绘制。