2014-01-18 79 views
1

我正在嘗試將鼠標輸入放置在我正在製作的塔防遊戲中。一切正常,除了當我放下塔時,它不會放在我的指針所在的位置。我跑了一個小測試,讓遊戲畫出了一個精靈,它認爲我的指針是它的,它將它拉遠離我的指針。這是我使用來獲取鼠標座標代碼:爲什麼我的MouseState不正確?

mouseState = Mouse.GetState(); //mouseState is of type MouseState 

cellX = (int)(mouseState.X/80); 
cellY = (int)(mouseState.Y/80); 

tileX = cellX * 80; 
tileY = cellY * 80; 

而且這裏是塔採用共同ORDS代碼:

Tower tower = new Tower(BlueberryBushTexture, new Vector2(tileX, tileY)); 

我唯一能想到的的是,我必須使用MouseState coords錯誤。謝謝!!!

+0

對我來說,數學看起來像是將'tileX'和'tileY'設置爲鼠標座標的整數部分,因爲您正在分割然後乘以80.「cell」和「tile」之間的區別是什麼? ?在前兩次計算之後,不要'cellX'和'cellY'已經代表你點擊的地方的瓦片座標? – esel

+0

我試過了,它沒有工作,它只是在左上角產生了我所有的塔樓 – Detinator10

+0

我使用了來自該部分教程的代碼,這就是他所說的 – Detinator10

回答

0

而不是分裂和相乘,你可以簡單地利用模量!

mouseState = Mouse.GetState(); 

tileX = (int)(mouseState.X - (mouseState.X % 80)); 
tileY = (int)(mouseState.Y - (mouseState.Y % 80)); 

/* 
This way it will always be highest multiple of 80 lower the the Mouse's position 

E.g. mouseState = (243, 712) 
tileX = (int)(243 - (243 % 80)); 
     = (int)(243 - (3)); 
     = 240; 

tileY = (int)(712 - (712 % 80)); 
     = (int)(712 - (72)); 
     = 640; 

Doing check sum's like this can be really helpful when later modifying 
(Say you make the center of the tile its origin, instead of the top-left corner) 
*/ 

這應該會幫助你解決你的難題,並希望能夠與你未來的遊戲項目一起工作。

Mona。

+0

它沒有工作。它仍然關閉,我甚至不能用這個放下塔。不過謝謝你的建議。 – Detinator10

0

這是舊的,但我看到它...... 你除以80,然後乘以80,你可以使用Math.Floor()。 這並不給你一個瓷磚索引它給你的鼠標位置。然後你需要弄清楚這一點,如果你有移動相機,你也需要考慮相機的位置。

相關問題