-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 を条件として使用します (したがって、bool として扱われます)。おそらくあなたが望んでいたのは

 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)

あなたがゲームを書いていて、タイマーを使っていることに気づきました。従来のタイマーが通常ゲームで使用されない理由については、こちらを参照してください。

フォローアップコメント用に編集:

あなたのフォームは公開されています。List of Balls という public プロパティを持っています。これで、あなたの Ball クラスは公開されていない可能性が高くなります。それは矛盾しています。フォームで不明な型を公開しないでください。Ball を public にするか、Ball 型を公開するメソッドとプロパティを private にします。

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

する必要があります

foreach(Ball ball in ballList)

ステートメントの 2 番目の部分は、for(ループを実行し続けるための) テスト条件です。ballListしたがって、式として渡そうとしていboolます。

于 2013-05-22T07:16:54.927 に答える