【问题标题】:Xna Movement not workingXna 运动不工作
【发布时间】:2014-03-27 00:49:08
【问题描述】:

嘿,自从我编写 C# Xna 代码 1 周以来,我遇到了一个问题 我希望你能帮助我。 问题是我创建了一个 Main 类,我在其中绘制了所有内容,并且我有一个 Control 类 但是现在控制不起作用。

主类:

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))
            { 
            }
        }
    }
}

我可以在一堂课上做所有的事情,但我想学习使用多个课程并且永远不会工作。

谢谢,希望很快能收到任何人的来信。

【问题讨论】:

    标签: c# xna


    【解决方案1】:

    您正在“Paddles”类中创建一个新的“main”。这个新的“主要”对象不知道实际的对象。

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

    pads.Initialize(this);
    

    将初始化函数更改为:

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

    但总的来说,这样做会破坏封装。 “Paddles”类应该包含所有的纹理、更新逻辑和画桨游戏对象的绘制逻辑。主类不应该需要任何这些,因为它将它传递给新类,并且“Paddles”类不应该需要“Main”类的任何知识(至少在您的示例中)。

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

    【讨论】:

      【解决方案2】:

      您需要将您的桨位置和桨纹理放在您的桨类中。

      然后您需要使用这些值让桨自行绘制。

      【讨论】:

        猜你喜欢
        • 2013-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多