2014-01-21 104 views
0

我正在創建一種簡單的沙盒文本冒險。我一直在使用座標系統研究玩家動作,但是當我輸入要移動的距離時,移動的實際距離是輸入的兩倍。當我只有北移碼而不是南時,這種情況不會發生。 玩家移動代碼如下所示,如果看起來問題在別處,我會很樂意提供任何其他代碼。C++命令似乎執行兩次

void playerMove(string input){ 
    int dist; 
cout << "How far do you want to go? (North/south max 1000, east/west max 2200)" << endl; 
dist = getIntInput(); 

if(input=="up"||"north"){ 
location[1] = location[1] - dist; 
    if(location[1]<0){ 
    location[1]=location[1]+1000; 
    } 
} 

if(input=="down"||"south"){ 
location[1] = location[1] - dist; 
    if(location[1]>1000){ 
    location[1]=location[1]-1000; 
    } 
} 

if(input=="left"||"east"){ 
//This will run the same code but for horizontal axis 
} 

if(input=="right"||"west"){ 
//This will run the same code but for horizontal axis 
} 
} 

地圖是here

,如果你需要得到你的頭一輪的座標系。謝謝。

編輯: 我也很想知道任何明顯的新手的錯誤是在那裏:)

+4

'if(input ==「up」||「north」)'請閱讀基本教程並修復此問題。 – Maroun

回答

2

我知道:

if(input=="left"||"east") 

在E中讀取時有意義簡體中文。但C++不是英語。在C++中,每個謂詞都需要單獨比較:

if(input == "left" || input == "east") 
+1

謝謝,解決了這個問題並解釋了原因。 – user2483876

2
if(input=="left"||"east"){ 

是錯誤的任何一般的反饋,使用

if(input=="left"||input=="east"){