其實小弟有個習慣把當日每天工作的想法、做法、問題、解決方法、奇想等等記錄下來的開日誌,現在也有60頁了!當中還把一些臭蟲記錄了,因為是本身比較可能只有自己看得懂,整理到這個網誌需時.......
無論如何,今日為大家介紹本系列一個臭蟲:
not all code paths return a value
事源是小弟弄了一個新的Function把兩點計算然後reutrn一個方向的值好讓敵方知道該怎樣跟隨玩家(有點有機會再跟大家說,我覺得廷有趣的),但是建立了之後就出Error了!
就是"not all code paths return a value",先讓大家看看代碼猜猜是那裏問題
public FlatHexPoint DirectionPoint(FlatHexPoint targetPoint, FlatHexPoint currentPoint)
{
//NorthEast, North, NorthWest, SouthWest, South, SouthEast
FlatHexPoint point = currentPoint - targetPoint;
//N, NE, E, S
if(point.X >= 0)
{
if(point.Y > 0)
return FlatHexPoint.North;
else if (point.Y == 0)
return FlatHexPoint.NorthEast;
else
{
if(point.X == 0)
return FlatHexPoint.South;
else
return FlatHexPoint.SouthEast;
}
}
//NW, SW
else if(point.X < 0)
{
if(point.Y > 0)
return FlatHexPoint.NorthWest;
else
return FlatHexPoint.SouthWest;
}
}
你們看得出來嗎?
在Google大神的幫助下,我終於知道答案了!
因為這裏不是把所有可能性(branches)都reutrn一個值!從這組代碼來看,就是else if(point.X < 0) 之後,還有如下的可能性else呢?雖然這裏 if-else-if 看似包含所有可能性可是系統不會知道呀~
所以把else if 改為 else 或是else if 之後再加一個else 就可以了!
public FlatHexPoint DirectionPoint(FlatHexPoint targetPoint, FlatHexPoint currentPoint)
{
//NorthEast, North, NorthWest, SouthWest, South, SouthEast
FlatHexPoint point = currentPoint - targetPoint;
//N, NE, E, S
if(point.X >= 0)
{
if(point.Y > 0)
return FlatHexPoint.North;
else if (point.Y == 0)
return FlatHexPoint.NorthEast;
else
{
if(point.X == 0)
return FlatHexPoint.South;
else
return FlatHexPoint.SouthEast;
}
}
//NW, SW
else
{
if(point.Y > 0)
return FlatHexPoint.NorthWest;
else
return FlatHexPoint.SouthWest;
}
}
白目了~人生就是許多大大小小或是很無聊的臭蟲,但它們就是臭蟲呀,還是要消滅~
沒有留言:
發佈留言