-4

我有这个错误

for (Ball ball; ballList)

错误信息是

"Cannot impliciltly convert type 'System.Collections.Generic.List<BallSeparation.Ball>' to 'bool'" 

我可以知道如何解决吗?

public partial class StartGame : Form
{
    public List<Ball> ballList { get; private set; }

    /**int bBA1; //The x axis from the upper left corner
    int bBA2; //The y axis from the upper left corner 
    int spdBBA1; //The change of x
    int spdBBA2; //The change of y
    **/

    public StartGame()
    {
        this.ballList = new List<Ball>();
        InitializeComponent();
    }

    private void StartGame_Load(object sender, EventArgs e)
    {
        ballList.add(new Ball(5, 10, 1, 1));
        /**
        //Loads the ball on the screen at bottom of the window
        bBA1 = this.ClientSize.Width / 5; //The x axis the ball is loaded at
        bBA2 = this.ClientSize.Height - 10; //The y axis the ball is loaded at
        spdBBA1 = 1; //The speed of the ball of y
        spdBBA2 = 1; //The speed of the ball of x
        **/
    }

    private void StartGame_Paint_1(object sender, PaintEventArgs e)
    {
        //This foreach loop will run through all the balls in ballList
        for (Ball ball; ballList)
        {
            e.Graphics.FillEllipse(Brushes.Blue, ball.positionX, ball.positionY, 10, 10);
        }
        /**
        //This is the inner paint color of the circle that is 10 by 10
        e.Graphics.FillEllipse(Brushes.Blue, bBA1, bBA2, 10, 10);
        //This is the outline paint color of the circle that is 10 by 10
        e.Graphics.DrawEllipse(Pens.Blue, bBA1, bBA2, 10, 10);
        **/
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        /**
        bBA2 = bBA2 + spdBBA2;
        bBA1 = bBA1 + spdBBA1;

        if (bBA2 < 0)
        {
            spdBBA2 = -spdBBA2; //If y is less than 0 then it changes direction
        }
        else if (bBA1 < -5)
        {
            spdBBA1 = -spdBBA1;
        }
        else if (bBA2 + 10 > this.ClientSize.Height)
        {
            spdBBA2 = -spdBBA2; //If y + 10, the radius of the circle is greater than the form width then we change direction
        }
        else if (bBA1 + 10 > this.ClientSize.Width)
        {
            spdBBA1 = -spdBBA1;
        }

        this.Invalidate();
        **/
    }
4

5 回答 5

4

该行for (Ball ball; ballList)实际上不是 foreach 循环。这是一个 for 循环,以 ballList 作为条件(因此被视为布尔值)。你可能想要的是

 foreach (Ball ball in ballList)
于 2013-05-22T07:16:41.393 回答
2

您的问题是以下行:

for (Ball ball; ballList)

for循环的语法是:

for (some assignment; some condition; some statement(s))

要查看ballList您想要的内容,请使用foreach循环:

foreach (Ball ball in ballList)
{
    // ...
}
于 2013-05-22T07:16:56.537 回答
1

改变这个:

for (Ball ball; ballList)

对此:

foreach (Ball ball in ballList)
于 2013-05-22T07:16:30.623 回答
1

C# 不是 Java。这有点像Java:

for (Ball ball; ballList)

正确的 C#:

foreach ( Ball ball in ballList)

我注意到您正在编写游戏并使用计时器。您可能想看一下这里,了解为什么游戏中通常不使用传统计时器。

编辑后续评论:

您的表格是公开的。它具有球列表的公共属性。现在你的 Ball 类很可能是不公开的。那是不一致的。您的表单不应公开未知类型。要么将 Ball 设为 public,要么将公开 Ball 类型的方法和属性设为私有。

于 2013-05-22T07:17:59.863 回答
0
for (Ball ball; ballList)

应该

foreach(Ball ball in ballList)

语句的第二部分for是测试条件(保持循环运行);因此,您试图将ballList其作为bool表达式传递。

于 2013-05-22T07:16:54.927 回答