2014-03-04 82 views
0

我想使用this website作为帮助,但代码似乎不工作了,所以我正在修复它,现在我得到一个C#标识符预期错误C#XNA标识符预计

public Point PickRandomCellAndMarkItVisited() 
{ 
    Random rnd = new Random(); 
    Point randomLocation = new Point(rnd.Next(1, 10), rnd.Next(1, 10)); 
    this.[randomLocation] = true; 
    return randomLocation; 
} 
+3

'this。[randomLocation] = true;'是无效的代码。你可能的意思是'this [randomLocation] = true;'请注意,在你连接的代码中,'PickRandomCellAndMarkItVisited'方法在'this'之后没有'.' ... –

回答

0

根据你的代码和tutorial,它看起来像你试图将随机单元格标记为已访问。我想你应该试试这个:

public class Map 
{ 

    private readonly bool[, ] cells;  
    public Point PickRandomCellAndMarkItVisited() 
    { 
     Random rnd = new Random(); 
     Point randomLocation = new Point(rnd.Next(1, 10), rnd.Next(1, 10)); 
     this.cells[randomLocation.X, randomLocation.Y] = true; 
     return randomLocation; 
    } 
} 

这将从网格中选取一个随机单元格,并将相应的布尔值标记为true。

+0

那么为什么会有类索引器呢? :) – Krekkon

+0

不再有任何错误,所以可能已经奏效,谢谢! –

+0

当然!很高兴帮助。 @Krekkon,我不确定为什么本教程的作者补充说。国际海事组织,只要使用'this.cells'甚至'cells [,]''会更容易。 – davidsbro