對(duì)我想要做的事情的簡(jiǎn)短描述:我正在構(gòu)建一個(gè)簡(jiǎn)單的游戲,其中用戶控制車輛,一段時(shí)間后越來越多的鬼開始跟隨玩家,它們遵循與玩家相同的軌跡,但有延遲。為了實(shí)現(xiàn)這一點(diǎn),我創(chuàng)建了一個(gè)數(shù)組,其中包含玩家位置的歷史記錄作為點(diǎn)序列。然而,問題是,當(dāng)我查看存儲(chǔ)在該數(shù)組中的數(shù)據(jù)時(shí),我發(fā)現(xiàn)在所有索引上只存儲(chǔ)了最近的位置。首先,我在 botManager 類中創(chuàng)建數(shù)組:public class BotManager {private ArrayList<Bots> bots;private List<Point> history;BotManager() { history = new ArrayList<>(); bots = new ArrayList<>();}然后在管理器類的更新方法中,我將玩家的當(dāng)前位置添加到數(shù)組中public void update(Point currLoc) { history.add(currLoc); for (Bots bot : bots) { bot.setLocationData(history); bot.update(); }}看看主 GameView 類中的更新方法,以防我在這里忘記了一些東西public void update() { player.update(playerPoint); botManager.update(playerPoint);}在 bots 類的構(gòu)造函數(shù)中,我傳遞歷史列表 (locationData) 并確定其長(zhǎng)度以找出定位延遲。之后以下代碼處理機(jī)器人的位置。@Overridepublic void update() { loc = locationData.get(delay - 1); this.rectangle = new Rect(loc.x - Constants.BOTSIZE/2, loc.y - Constants.BOTSIZE/2, loc.x + Constants.BOTSIZE/2, loc.y + Constants.BOTSIZE/2);}回到問題,每當(dāng)我檢查歷史數(shù)組的內(nèi)容時(shí),我發(fā)現(xiàn)它只包含所有索引上的一個(gè)點(diǎn),并且即使我移動(dòng)播放器也是最新的,導(dǎo)致鬼總是留在頂部我。所以我的問題是,我在這里做錯(cuò)了什么?
1 回答

守著星空守著你
TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊
從您發(fā)布的代碼不明確的,但會(huì)不會(huì)是,你只是修改了Point其中添加,而不是更新的對(duì)象的點(diǎn)對(duì)象?
嘗試
public void update(Point currLoc) {
history.add(new Point(currLoc)); // new Point object added here
for (Bots bot : bots) {
bot.setLocationData(history);
bot.update();
}
}
添加回答
舉報(bào)
0/150
提交
取消