2015-09-04 93 views
-2

我正尝试在Windows上的cmd上运行的C#中编写游戏,我需要能够写入该框的任何部分要做到这一点。我发现这个目的广泛使用WriteAt,但它似乎并没有在VS 2010中工作,我得到的错误:错误名称'WriteAt'在当前上下文中不存在

WriteAt不会在当前的背景下存在的名字”我有默认:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

位于我的代码顶部。那么为什么我不能使用WriteAt

这里是我的代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace GamePCL 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.Clear(); 

      for (int x = 0; x < 24; x += 2) 
      { 
       WriteAt("█", x, 0); 
       WriteAt("█", x, 30); 
      } 
     } 
    } 
} 
+0

你在哪里使用'WriteAt'? –

+0

我想你需要使用'Console.WriteLine()'。 –

+1

您确定它不是一个在内部调用SetCursorPosition的自定义方法吗? – ken2k

回答

2

当你调用一个方法没有对象或类型的前缀,在这种情况下WriteAt()(而不是例如Console.WriteLine(),这就是所谓的Console型)中,方法必须存在于当前上下文中,即当前类中。

你复制的代码from MSDN而不复制相关的方法:

protected static void WriteAt(string s, int x, int y) 
{ 
    try 
    { 
     Console.SetCursorPosition(origCol+x, origRow+y); 
     Console.Write(s); 
    } 
    catch (ArgumentOutOfRangeException e) 
    { 
     Console.Clear(); 
     Console.WriteLine(e.Message); 
    } 
} 
相关问题