首页 文章

我该如何解决这个错误?C#[复制]

提问于
浏览
-2

这个问题在这里已有答案:

我有一个私有方法是ball.cs类在女巫我想从margins.cs类修改bool类型,但我得到错误非静态字段,方法或属性'PingPong.Margins需要对象引用 . 赢得' . 有人可以帮我弄这个吗?

这是球类代码:

namespace PingPong
{
 public class Ball
{

     private PictureBox ball;
     Random rand= new Random();
     Player leftSidePlayer, rightSidePlayer;

     int xSpeed, ySpeed;

     public Ball(PictureBox aBall, Player leftSidePlayer, Player rightSidePlayer)
     {   

         this.ball = aBall;
         this.leftSidePlayer = leftSidePlayer;
         this.rightSidePlayer = rightSidePlayer;
         xSpeed = 1;
         ySpeed = 2;
         resetBall();
     } 

     internal void processmove()
     {
         var bottom = Margins.bottomOfWorld - ball.Height;
         DoMove();

         if(ball.Location.Y >= bottom || ball.Location.Y <= Margins.topOfWorld)
         {
             ySpeed *= -1;
         }

         if (ball.Location.X <= Margins.leftOfWorld)
         {
            Score(leftSidePlayer);
         }
         else if (ball.Location.X >= Margins.rightOfWorld - ball.Width) 
         {
             Score(rightSidePlayer); 
         }

         if ((leftSidePlayer.paddle.Bounds.IntersectsWith(ball.Bounds)) || (rightSidePlayer.paddle.Bounds.IntersectsWith(ball.Bounds)))
         {
             xSpeed *= -1;
             if ((ySpeed <= 6 && ySpeed >=-6) && (xSpeed <= 5 && xSpeed >=-5) )
             {
                 if(ySpeed < 0)
                 {
                     ySpeed -= 1;
                 }else
                 {
                     ySpeed += 1;
                 } 

                 if (xSpeed < 0)
                 {
                     xSpeed -= 1;

                 }
                 else
                 {
                     xSpeed += 1;
                 }
             }
         }


     }

     private int DoMove()
     {
         var bottom = Margins.bottomOfWorld - ball.Height;
         ball.Location = new Point(ball.Location.X + xSpeed, Math.Max(Margins.topOfWorld, Math.Min(bottom, ball.Location.Y + ySpeed)));
         return bottom;
     }

     private void Score(Player winningPlayer)
     {
         winningPlayer.scoreNumber++;

         if(winningPlayer.scoreNumber == 7)
         {
             if(winningPlayer == leftSidePlayer  )
             {
                 Margins.win = true;
             }else if(winningPlayer == rightSidePlayer)
             {
                 Margins.win = false;
             }


         }


         resetBall();
     }

     private void resetBall()
     {
         ball.Location = new Point((Margins.leftOfWorld + Margins.rightOfWorld) / 2, (Margins.bottomOfWorld + Margins.topOfWorld) / 2);
         do
         {
             xSpeed = rand.Next(-3, 3);
             ySpeed = rand.Next(-3, 3);
         } while(Math.Abs(xSpeed) + Math.Abs(ySpeed) <= 3);

    }

}

}

这是边距类代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PingPong
{
    public class Margins
    {   //Marimea ferestrei 1000; 600
        public const int topOfWorld = 0, bottomOfWorld = 560, leftOfWorld =0, rightOfWorld = 1000;
        public bool? win = null;
    }
}

当我使用margins.win = true和margins.win = false时,我得到的错误是私有空分 .

1 回答

  • 0

    你现在有一个带有一些静态(或常量)变量的类 . 接下来是实例变量 win . 这意味着您必须创建 Margins 的实例才能获取或设置它 .

    如果你真的需要一个静态类,因为总是只有一个 Margins 实例,那么使类和它的变量保持静态:

    public static class Margins
    {   //Marimea ferestrei 1000; 600
        public const int topOfWorld = 0, bottomOfWorld = 560, leftOfWorld =0, rightOfWorld = 1000;
        public static bool? win = null;
    }
    

    如果没有,请创建 Margins 的正确实例:

    private Margins margins = new Margins();
    
    this.margins.win = true;
    

相关问题