Xna运动不起作用

时间:2014-03-26 16:38:18

标签: c# xna

嘿,自从1周以来,我正在编写C#Xna,我遇到了一个问题 我希望你能帮助我。 问题是我创建了一个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))
            { 
            }
        }
    }
}

我可以在一个课程中完成所有课程,但我想学习使用多个课程而且从不工作。

感谢并希望尽快收到任何人的回复。

2 个答案:

答案 0 :(得分:0)

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

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

pads.Initialize(this);

将初始化功能更改为:

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

总的来说,你通过这样做打破了封装。 “Paddles”类应该保存所有纹理,更新逻辑,并为桨式游戏对象绘制逻辑。主类不应该需要其中任何一个,因为它将它传递给新类,而“Paddles”类不需要任何“Main”类知识(至少在你的例子中)。

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

答案 1 :(得分:0)

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

然后你需要使用这些值来绘制自己的画笔。