2016-11-25 37 views
0

你好我使用C#和opentk,我有這樣的代碼OpenTK塊必須被完全分配

using System; 
using System.Drawing; 
using System.IO; 
using OpenTK; 


namespace PlatformGame 
{ 
    struct Level 
    { 
     private Block[,] grid; 
     public Block this[int X, int Y] 
     { 
      get 
      { 
       return grid[X, Y]; 
      } 
      set 
      { 
       grid[X, Y] = value; 
      } 
     } 

     public Point PlayerStartPos; 

     private string filename; 
     public string FileName 
     { 
      get 
      { 
       return filename; 
      } 
     } 


     public int Width 
     { 
      get 
      { 
       return grid.GetLength(0); 
      } 
     } 
     public int Height 
     { 
      get 
      { 
       return grid.GetLength(1); 
      } 
     } 

     public Level(int Width, int Height) 
     { 
      grid = new Block[Width, Height]; 
      filename = "none"; 
      PlayerStartPos = new Point(1, 1); 

      for (int x=0; x< Width; x++) 
      { 
       for (int y = 0; y < Height; y++) 
       { 
        if (x == 0 || y == 0 || x == Width - 1 || y == Height - 1) 
        { 
         grid[x, y] = new Block(BlockType.Water, x, y); 
        } 
        else 
        { 
         grid[x, y] = new Block(BlockType.Empty, x, y); 
        } 
       } 
      } 
     } 
    } 

    public enum BlockType 
    { 
     Empty, 
     Water, 
     Grass, 
     Dirt, 
     Stone, 
     Platform, 
     Ladder, 
     LadderPlatform 
    } 

    struct Block 
    { 
     private BlockType type; 

     private int PosX, PosY; 

     private bool Water, Grass, Dirt, Stone, Platform, Ladder, LadderPlatform; 

     public BlockType Type 
     { 
      get { return type; } 
     } 

     public int X 
     { 
      get { return PosX; } 
     } 
     public int Y 
     { 
      get { return PosY; } 
     } 
     public bool IsWater 
     { 
      get { return Water; } 
     } 
     public bool IsGrass 
     { 
      get { return Grass; } 
     } 
     public bool IsDirt 
     { 
      get { return Dirt; } 
     } 
     public bool IsStone 
     { 
      get { return Stone; } 
     } 
     public bool IsPlatform 
     { 
      get { return Platform; } 
     } 
     public bool IsLadder 
     { 
      get { return Ladder; } 
     } 

     public Block(BlockType type,int x,int y) 
     { 
      this.PosX = x; 
      this.PosY = y; 
      this.type = type; 

      this.Water = false; 
      this.Grass = false; 
      this.Dirt = false; 
      this.Stone = false; 
      this.Ladder = false; 
      this.Platform = false; 

      switch (type) 
      { 
       case BlockType.Ladder: 
        Ladder = true; 
        break; 
       case BlockType.LadderPlatform: 
        Ladder = true; 
        Platform = true; 
        break; 
       case BlockType.Platform: 
        Platform = true; 
        break; 
       case BlockType.Water: 
        Water = true; 
        break; 
       case BlockType.Grass: 
        Grass = true; 
        break; 
       case BlockType.Dirt: 
        Dirt = true; 
        break; 
       case BlockType.Stone: 
        Stone = true; 
        break; 
      } 
     } 

    } 
} 

出於某種原因,Visual Studio是給我這個錯誤在public Block

場「阻止。 LadderPlatform'必須完全分配,然後才能將控制返回給調用者PlatformGame

我試圖理解這個問題,但我無法解決e it。

回答

1

您在初始化時錯過了this.LadderPlatform = false;

如果您在public Block(BlockType type,int x,int y) switch語句前加上這一點,將工作

像這樣:

public Block(BlockType type, int x, int y) 
{ 
    this.PosX = x; 
    this.PosY = y; 
    this.type = type; 

    this.Water = false; 
    this.Grass = false; 
    this.Dirt = false; 
    this.Stone = false; 
    this.Ladder = false; 
    this.Platform = false; 
    this.LadderPlatform = false; //<--- This is missing 

    switch (type) 
    { 
     case BlockType.Ladder: 
      Ladder = true; 
      break; 
     case BlockType.LadderPlatform: 
      Ladder = true; 
      Platform = true; 
      break; 
     case BlockType.Platform: 
      Platform = true; 
      break; 
     case BlockType.Water: 
      Water = true; 
      break; 
     case BlockType.Grass: 
      Grass = true; 
      break; 
     case BlockType.Dirt: 
      Dirt = true; 
      break; 
     case BlockType.Stone: 
      Stone = true; 
      break; 
     default: 
      break; 
    } 
} 
+0

對不起,浪費您的時間了一個愚蠢的錯誤。 tks的幫助,我只需要等待設置爲答案。 – Pedro

+0

不客氣,有時我們都盲目看我們自己的代碼;) – TripleEEE

相關問題